9:26 PM 71% o AT&T; KAssn15 Assign15-2. docx Write an error-free Java program to
ID: 3819137 • Letter: 9
Question
9:26 PM 71% o AT&T; KAssn15 Assign15-2. docx Write an error-free Java program to do the following things. Implement a class called Person. A person has a name, gender and a birth year. Make a Class Student that inherits from Person. A student has a major, a gpa and a number of credit hours completed. Write the class declarations for Person and Student. The main0 module is shown below. You must write the class definitions, constructor(s) and method(s) to make the program work. The output from the given main0 is shown after main. Your output can be formatted differently be displayed. For example, when the Student information is displayed, the program show their name, gender, birth year as well as the major, gpa and credits till graduation. If the data has not been set via a method, then the default value is displayed. Remember to put the usual header at the top of the program and to submit via Canvas. Main code: 5 public class assign15 soln 7 public static void main (String arg) Person pl new Person ("Sally female 1998) 10 Person p2 new Person Student studs new Student 12 13 for (int i 0; iExplanation / Answer
Person.java
public class Person {
//Declaring variables
private String name;
private String gender;
private int birthYear;
//Zero argumented constructor
public Person() {
super();
this.name="Tom Hammond";
this.gender="??";
this.birthYear=1900;
}
//Parameterized constructor
public Person(String name, String gender, int birthYear) {
super();
this.name = name;
this.gender = gender;
this.birthYear = birthYear;
}
//Setters and getters
public void set(String name, String gender, int birthYear)
{
this.name = name;
this.gender = gender;
this.birthYear = birthYear;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
//This method will Display Person info
public void display() {
System.out.println("Name=" + name +" "+ gender + " born in "+ birthYear);
}
@Override
public String toString() {
System.out.println("Name=" + name +" "+ gender + " born in "+ birthYear);
return "";
}
}
___________________
Student.java
public class Student extends Person {
//Declaring variables
private String major;
private double gpa;
private int creditHours;
public Student() {
}
//Parameterized constructor
public Student(String name, String gender, int birthYear, String major,
double gpa, int creditHours) {
super(name, gender, birthYear);
this.major = major;
this.gpa = gpa;
this.creditHours = creditHours;
}
//Setters and getters
public void set(String name, String gender, int birthYear)
{
super.set(name, gender, birthYear);
}
public void setStudent(String major,
double gpa, int creditHours)
{
this.major = major;
this.gpa = gpa;
this.creditHours = creditHours;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
//This method will display Student class object info
public void display() {
super.display();
System.out.println( "major :" + major + " gpa=" + gpa + " "+ creditHours + " credit hours to graduate.");
}
}
_____________________
Test.java
public class Test {
public static void main(String[] args) {
Person p1=new Person("Sally","Female", 1998);
Person p2=new Person();
Student [] studs=new Student[2];
for(int i=0;i<2;i++)
studs[i]=new Student();
p2.set("Susan Jones", "female", 1989);
studs[0].set("Betsy","female",1996);
studs[0].setStudent("economics",3.5, 45);
studs[1].setStudent("science",2.7, 93);
p1.display();
System.out.println(p2);
System.out.println();
for(int j=0;j<2;j++)
{
studs[j].display();
}
}
}
______________________
Output:
Name=Sally Female born in 1998
Name=Susan Jones female born in 1989
Name=Betsy female born in 1996
major :economics gpa=3.5 45 credit hours to graduate.
Name=Tom Hammond ?? born in 1900
major :science gpa=2.7 93 credit hours to graduate.
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.