60.2k views
5 votes
1. Create a class called Name that represents a person's name. The class should have fields named firstName representing the person's first name, lastName representing their last name, and middleInitial representing their middle initial (a single character). Your class should contain only fields for now.

2. Add two new methods to the Name class:

public String getNormalOrder()
Returns the person's name in normal order, with the first name followed by the middle initial and last name. For example, if the first name is "John", the middle initial is 'Q', and the last name is "Public", this method returns "John Q. Public".

public String getReverseOrder()
Returns the person's name in reverse order, with the last name preceding the first name and middle initial. For example, if the first name is "John", the middle initial is 'Q', and the last name is "Public", this method returns "Public, John Q.".

(You don't need to write the class header or declare the fields; assume that this is already done for you. Just write your two methods' complete code in the box provided.)

User Ultradoxx
by
5.3k points

2 Answers

7 votes

Answer: Jhon Greenheart

Step-by-step explanation:

User Merin
by
5.2k points
7 votes

ANSWER:

( 1 ).

public class Name{ //header declaration for the class Name

/*

Declare all necessary fields.

The first and last names of the person are string variables.

Hence they are of type String.

The middle initial of the person is a single character.

Hence it is of type char.

*/

String firstName; // person's first name called firstName

String lastName; // person's last name called lastName

char middleInitial; // person's middle initial called middleInitial

}

==========================================================

( 2 )

/*

Method getNormalOrder() is declared as follows.

It returns the person's name in normal order with first name

followed by the middle initial and last name.

*/

public String getNormalOrder(){

// concatenate the firstName, middleInitial and lastName and return the

// result.

return this.firstName + " " + this.middleInitial + ". " + this.lastName;

}

/*

Method getReverseOrder() is declared as follows.

It returns the person's name with the last name

followed by the first name and middle initial.

*/

public String getReverseOrder(){

// concatenate the lastName, lastName and middleInitial and return the

// result.

return this.lastName + " " + this.firstName + " " + this.middleInitial + ".";

}

EXPLANATION:

The above code has been written in Java.

The code contains comments that explain every part of the code. Please go through the comments carefully for a better understanding of the code.

Special note:

i. Concatenation which means joining strings together, is done in Java using the + operator.

ii. The this keyword used in the two methods is optional. It is just used to reference to the instance variables - firstName, lastName and middleInitial - of the object.

Hope this helps!

User JonathanN
by
5.1k points