Your programming assignment in this Module has three parts. Follow the instructi
ID: 3762670 • 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.
Then add:
you created a program for a class Person and a class Student. Reuse those two classes for this assignment and build upon them as follows:
Add another class Professional that inherits the Person class and overrides the display() method.
Enhance all three classes to use Polymorphism on the display() method.
In the main() method, use a polymorphic pointer variable pIndividual of type Person to call the corresponding display()method of all the three classes.
Explanation / Answer
/**
* @author srinu
*
*/
public class Person {
String name, address, city, state, zip, phoneNumber;
/**
*
*/
public Person() {
// TODO Auto-generated constructor stub
name = address = city = state = zip = phoneNumber = "";
}
/**
* @return Returns the address.
*/
public String getAddress() {
return address;
}
/**
* @param address
* The address to set.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return Returns the city.
*/
public String getCity() {
return city;
}
/**
* @param city
* The city to set.
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the phoneNumber.
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* @param phoneNumber
* The phoneNumber to set.
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* @return Returns the state.
*/
public String getState() {
return state;
}
/**
* @param state
* The state to set.
*/
public void setState(String state) {
this.state = state;
}
/**
* @return Returns the zip.
*/
public String getZip() {
return zip;
}
public void display() {
System.out.println("name=" + name + ", address=" + address + ", city="
+ city + ", state=" + state + ", zip=" + zip + ",phoneNumber="
+ phoneNumber);
}
/**
* @param zip
* The zip to set.
*/
public void setZip(String zip) {
this.zip = zip;
}
}
public class Student extends Person {
String grade, course, GPA;
/**
*
*/
public Student() {
// TODO Auto-generated constructor stub
grade = course = GPA = "";
}
/**
* @return Returns the course.
*/
public String getCourse() {
return course;
}
/**
* @param course
* The course to set.
*/
public void setCourse(String course) {
this.course = course;
}
/**
* @return Returns the gPA.
*/
public String getGPA() {
return GPA;
}
/**
* @param gpa
* The gPA to set.
*/
public void setGPA(String gpa) {
GPA = gpa;
}
/**
* @return Returns the grade.
*/
public String getGrade() {
return grade;
}
/**
* @param grade
* The grade to set.
*/
public void setGrade(String grade) {
this.grade = grade;
}
// grade, course, GPA;
public void display() {
super.display();
System.out.println("grade=" + grade + ", course=" + course + ", GPA="
+ GPA);
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student student = new Student();
student.setAddress("Address");
student.setCity("City");
student.setCourse("Course");
student.setGPA("GPA");
student.setGrade("Grade");
student.setName("Name");
student.setPhoneNumber("phone number");
student.setState("state");
student.setZip("zip");
student.display();
}
}
OUTPUT:
name=Name, address=Address, city=City, state=state, zip=zip,phoneNumber=phone number
grade=Grade, course=Course, GPA=GPA
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.