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.

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

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

Hi,

To call the method in Java, you can follow these steps:

1. Create an instance of the `ReturnName` class in your `main` method.
2. Call the `fullname` method on the created instance, passing the first name, middle initial, and last name as arguments.
3. Store the returned value in a variable of type `String`.
4. Use `System.out.println` to print the value stored in the variable.

Here's an example of how your code would look like:

```java
public class ReturnName {
static String fullname(String firstname, String middleinitial, String lastname) {
return firstname + " " + middleinitial + ". " + lastname;
}

public static void main(String[] args) {
ReturnName rn = new ReturnName(); // Step 1. Creating instance of ReturnName class
String myName = rn.fullname("John", "D", "Doe"); // Step 2. Calling fullname method

System.out.println(myName); // Step 4. Printing the returned value
}
}
```

In this example, when the `main` method is executed, it creates an instance of the `ReturnName` class and assigns it to the `rn` variable (Step 1). Then, the `fullname` method is called on the `rn` instance, passing the first name "John", middle initial "D", and last name "Doe" as arguments (Step 2). The returned value is stored in the `myName` variable. Finally, the `myName` variable is printed using `System.out.println` (Step 4).

When you run the program, it should output: `John D. Doe`, which is the concatenation of the first name, middle initial, and last name as a single string.