Asked by Lisa
Hi,
I have to write a method that returns my first name, middle initial, and last name as a single String and call the method in java. So far, I have:
public class ReturnName {
static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
}
}
I don't know where to go from there.
I have to write a method that returns my first name, middle initial, and last name as a single String and call the method in java. So far, I have:
public class ReturnName {
static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
}
}
I don't know where to go from there.
Answers
Answered by
MathMate
You only need a static main method. When Java compiles and executes, it will look for a static method called main inside of the public class. If found, it will be executed.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String args)
{
System.out.println("Lisa","A.","Smith");
} // main
} // public class
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String args)
{
System.out.println("Lisa","A.","Smith");
} // main
} // public class
Answered by
MathMate
I come around to test the above program, and found two corrections,
- String[] args replaces String args in the declaration of the main method.
- forgot to invoke the fullname method in the System.out.println() call.
The private declaration of fullname is optional, but helps to keep a clean namespace.
Here's the corrected version.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String[] args)
{
System.out.println(fullname("Lisa","A.","Smith"));
} // main
} // public class
- String[] args replaces String args in the declaration of the main method.
- forgot to invoke the fullname method in the System.out.println() call.
The private declaration of fullname is optional, but helps to keep a clean namespace.
Here's the corrected version.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String[] args)
{
System.out.println(fullname("Lisa","A.","Smith"));
} // main
} // public class
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.