Question
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.
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 main() 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 main() is shown after main. Your output can be formatted differently (hopefully a better format) but the same information must 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 6 { 7 public static void main(String [] arg) 8 { 9 Person p1 = new Person("Sally", "female", 1998); 10 Person p2 = new Person(); 11 Student [] studs = new Student[2]; 12 13 for (int i = 0; i <2; i++) 14 studs[i] = new Student(); 15 16 p2.set("Susan Jones", "female", 1989); 17 18 studs[0].set("Betsy", "female", 1996); 19 studs[0].setStudent("economics", 3.5, 45); 20 studs[1].setStudent("science", 2.7, 93); 21 22 p1.newName("Sally Smith"); 23 studs[1].newName("Tom Hammond"); 24 25 p1.display(); 26 System.out.println(p2); 27 System.out.println(); 28 for (int j = 0; j < 2; j++) 29 studs[j].display(); 30 } 31 }
Output:
MMMName: Sally Smith female born in 1998 MMMSusan Jones is female and born in 1989 MMM MMMName: Betsy female born in 1996 MMMmajor: economics gpa = 3.5 75 credit hours to graduate. MMMName: Tom Hammond ?? born in 1900 MMMmajor: science gpa = 2.7 27 credit hours to graduate. MMM
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started