Your programming assignment in this Module has three parts. Follow the instructi
ID: 3836047 • Letter: Y
Question
Your programming assignment in this Module has three parts. Follow the instructions below:
Create a class Person, which has a name, address, city, state, zip, and phone number. Create a default constructor to initialize the variables to some default values. DO NOT get them as an input from the user. If you prefer non-default constructors, then you can have them, but that is optional. Have a "display()" function to display the values.
Create a class Student, which inherits from Person, and has additionally grade, course, and GPA. Create a default constructor to initialize the three variables to some default values. DO NOT get them as an input from the user. If you prefer non-default constructors, then you can have them, but that is optional. Have a "display()" function to display the values. From the Student "display()" function call the Person "display()" function.
In the "main()", create an object of Person. Display its values. Then, create an object of Student. Display its values. This would display all the variables from both Person and Student.
Add plenty of narrative comments.
Your program must be compilable and executable.
Explanation / Answer
Here is your code: -
Person.java
/*Name: -
* Class Name: - Person
* Date:-
* Concept: - To hold the details of the Person and way to access them
*/
public class Person {
// private access modifier so that they can be accessed in this class only
// also.
private String name;
private String address;
private String city;
private String state;
private String zip;
private Long phoneNum;
// Default constructor
Person() {
this.name = "";
this.address = "";
this.city = "";
this.state = "";
this.zip = "";
this.phoneNum = (long) 0;
}
// Parametrized Constructor
public Person(String name, String address, String city, String state, String zip, Long phoneNum) {
this.name = name;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
this.phoneNum = phoneNum;
}
//Copy Constructor
public Person(Person p) {
this.name = p.getName();
this.address = p.getAddress();
this.city = p.getCity();
this.state = p.getState();
this.zip = p.getZip();
this.phoneNum = p.getPhoneNum();
}
// Setters and Getters.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Long getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(Long phoneNum) {
this.phoneNum = phoneNum;
}
// Method to display the values
public void display() {
System.out.println("Details:");
System.out.println("Name:" + this.name);
System.out.println("Address:" + this.address);
System.out.println("City:" + this.city);
System.out.println("State:" + this.state);
System.out.println("Zip:" + this.zip);
System.out.println("Phone Number:" + this.phoneNum);
}
}
Student.java
/*Name: -
* Class Name: - Student
* Date:-
* Concept: - To hold the details of the Student grade,course and GPA and way to access them
*/
public class Student extends Person {
private String grade;
private String course;
private String GPA;
// Non -Default Constructors
public Student(Person per, String grade, String course, String gPA) {
super(per);// Calling parent copy constructor using super keyword
this.grade = grade;
this.course = course;
GPA = gPA;
}
// Default Constructor
Student() {
this.grade = "";
this.course = "";
this.GPA = "";
}
// Getters and Setters
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getGPA() {
return GPA;
}
public void setGPA(String gPA) {
GPA = gPA;
}
// method to print Student details
public void display() {
super.display();// Calling display method of Person class
System.out.println("Grade:" + this.grade);
System.out.println("Course:" + this.course);
System.out.println("GPA:" + this.GPA);
}
}
Test.java
/*Name: -
* Class Name: - Test
* Date:-
* Concept: - To Test the classes
*/
public class Test {
public static void main(String[] args) {
// Creating Person object using non default constructor
Person per = new Person("Sherlock", "221 B", "London", "United Kingdom", "182100",
Long.parseLong("9898762345"));
per.display();// Display Person Details
System.out.println();
// Creating Student Object using non-default Constructor
Student stu = new Student(per, "A+", "Psychology", "9.4");
// Displaying the Student details
stu.display();
}
}
Sample Run : -
Details:
Name:Sherlock
Address:221 B
City:London
State:United Kingdom
Zip:182100
Phone Number:9898762345
Details:
Name:Sherlock
Address:221 B
City:London
State:United Kingdom
Zip:182100
Phone Number:9898762345
Grade:A+
Course:Psychology
GPA:9.4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.