DESCRIPTION: Write a class that identifies an object Person. The name of this cl
ID: 3623114 • Letter: D
Question
DESCRIPTION: Write a class that identifies an object Person. The name of this class will be Person. Each Person object will have the following data:First Name – String
Middle Name – String
Last Name – String
Include the following methods in the class
A constructor – that receives the first name, middle name, and the last name and creates a Person object.
toString() – This method will create a string containing the person information and will return the String.
Write a driver class that will create three person objects, prompt the user for the names, and display the person information using the toString method.
Explanation / Answer
public class Person
{
private String firstname, middlename, lastname;
public Person(String first, String middle, String last)
{
firstname = first;
middlename = middle;
lastname = last;
}
public String toString()
{
return firstname+" "+middlename+" "+lastname;
}
}
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter the name of the first person: ");
Person p1 = new Person(kb.next(), kb.next(), kb.next());
System.out.print("Enter the name of the second person: ");
Person p2 = new Person(kb.next(), kb.next(), kb.next());
System.out.print("Enter the name of the third person: ");
Person p3 = new Person(kb.next(), kb.next(), kb.next());
System.out.println(" "+p1+" "+p2+" "+p3);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.