Write a Java program to declare and implement 2 classes: Person class and Student class…
- User
Write a Java program to declare and implement 2 classes: Person class and Student class based on the following requirements. The main program is given to you (please find it in the Example input & output session).
The Person class:
Person class is an immutable class It has 2 properties (first_name, last_name): “first_name” and “last_name” are first name (string) and last name (string) of a person When creating an instance of Person, “first_name” and “last_name” are all required It has a print() method to output all the 2 properties on to the screen (see the example for the output format)
The Student class:
Student class is an immutable class Student class is a subclass of Person class It has the 3 properties, including the 2 properties of Person class (first_name, last_name) and additional property (studentId): “studentId” is the ID of a student (string) Provide 2 different ways to create an instance of Student: Pass all the 3 properties: “first_name”, “last_name” and “studentId” Pass an instance of Person and a “studentId” It has a print() method to output the all the 3 properties on to the screen (see the example for the output format) Example input & output:
An example main class and its output are given as follow:
public class Assign2_pXXXXXXX {
public static void main(String[] args) { Person p1 = new Person("Tim", "Johnson"); p1.print(); System.out.println(); // print a blank line Student s1 = new Student(p1, "p1234567"); s1.print(); System.out.println(); // print a blank line Student s2 = new Student("Mary", "Brown", "p7654321"); s2.print(); }}
Output:
First name: Tim
Last name: Johnson
Student ID: p123456
Student Name: Tim Johnson
Student ID: p654321
Student Name: Mary Brown
- Assistant
Here is the Java program that implements the Person class and Student class:
class Person { private String first_name; private String last_name;
public Person(String first_name, String last_name) { this.first_name = first_name; this.last_name = last_name; } public void print() { System.out.println("First name: " + first_name); System.out.println("Last name: " + last_name); }}
class Student extends Person { private String studentId;
public Student(String first_name, String last_name, String studentId) { super(first_name, last_name); this.studentId = studentId; } public Student(Person person, String studentId) { super(person.first_name, person.last_name); this.studentId = studentId; } public void print() { System.out.println("Student ID: " + studentId); System.out.println("Student Name: " + super.first_name + " " + super.last_name); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.