212k views
0 votes
Create a class called Name that will have three fields first name, middle initial, and last name as parameters. Add a constructor to the Name class that accepts a first name, middle initial, and last name as parameters (in that order) and initializes the Name object's state with those values. Write a complete program (i.e. with client class) to check your work. Please submit Name and client class .java files. [10 points]

User Aifuwa
by
5.5k points

1 Answer

5 votes

Answer:

The program to the given question can be given as:

Program:

import java.io.*; //import package for client class.

class Name //class-name Name.

{

// data members of the class.

String firstname, middleinitial, lastname;

// define parameterized constructor that would initialize data members

Name(String Fname,String Mname,String Lname)

{

this.firstname= Fname; //Using this key-word for holding value.

this.middleinitial= Mname;

this.lastname= Lname;

}

void showtime() //method showname with no return-type.

{

//define variable name for strore value.

String name;

name = firstname + " " + middleinitial + " " + lastname;

System.out.print(name); //print value.

}

}

class Main //main class

{

public static void main (String[] args) //main method.

{

Name ob =new Name("AAA","BBB","CCC");

//creating class object.and passing value to parameterized constructor.

ob.showtime();

//calling showtime function.

}

}

Output:

AAA BBB CCC.

Step-by-step explanation:

The explanation of the program can be given as:

In this program, we create a class i.e. (Name). In class, we declare a variable that's data type is String because It is a sequence of characters that hold a string value. Then we define the parameterized constructor. It is used for holding string values in it. We define a function i.e. showtime. This function is used for string value that is passed in the constructor. Then we create the main class that has the main method. It is used for calling the Name class by creating its object. After creating a name class object we pass the value to the parameterized constructor. and call the function, Where it is a function that doesn't have any return type.

User Saurabh Palatkar
by
6.3k points