18.7k views
4 votes
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. In order for Practice-It to properly test your class, make sure to use exactly the class name and field names described previously. Also make sure to declare your fields using appropriate types.

User MohanKumar
by
8.4k points

2 Answers

6 votes

Final answer:

A 'Name' class in Java is created with three fields: 'firstName' and 'lastName' as String types, and 'middleInitial' as a char type. The correct naming conventions must be used to match the specification, which is crucial for testing in Practice-It.

Step-by-step explanation:

To create a class called Name that represents a person's first name, last name, and middle initial, you will define a class in Java with three fields. Each field will have its own data type appropriate to the information it is meant to represent.

Here's an example of how the class may look in Java:

public class Name {
String firstName; // The person's first name
String lastName; // The person's last name
char middleInitial; // The person's middle initial
}

Make sure to follow the exact naming conventions as specified in the task description to ensure that Practice-It can properly test your class. In this case, firstName and lastName are both Strings because names can have more than one character, while middleInitial is a char since an initial is a single character.

User Jason Angel
by
8.0k points
0 votes

Answer:

The program to this question can be given as:

Program:

class Name //define class

{

//define variable.

String firstName="XXX";

String lastName="YYY";

char[] middleInitial;

}

public class Main //define main class.

{

public static void main(String a[]) //define main method

{

Name ob = new Name(); //creating name class object.

String FirstName = ob.firstName;

String LastName = ob.lastName; //calling

char[] MiddleInitial = ob.middleInitial;

System.out.println("First name "+FirstName); //print values.

System.out.println("Middle name "+MiddleInitial);

System.out.println("Last name "+LastName);

}

}

Output:

First name XXX

Middle name null

Last name YYY

Explanation:

In the above program firstly we define class Name that is given in the question. In this class, we define a variable that's datatype is string because it stores the string values. In this variable, we assign the value in the first and last name variables. In the middle name, we do not assign any value so it will print null. In the last we define the main class in this class we define the main method. In this method, we create a name class object and call the variable and print its value.

User Sebsemillia
by
9.8k points