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.

Respuesta :

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.    

ACCESS MORE