Question
Download the following files: InheritanceDemo.java Person.java Student.java Undergrad.java package l05; import java.util.Scanner; /** * A program to demonstrate inheritance using a simple hierarchy. * *
Download the following files:
- InheritanceDemo.java
- Person.java
- Student.java
- Undergrad.java
-
package l05; import java.util.Scanner; /** * A program to demonstrate inheritance using a simple hierarchy. * * @author Mark Young (A00000000) */ public class InheritanceDemo { private static final Scanner KBD = new Scanner(System.in); public static void main (String [] args) { System.out.println(); // basic person doing personny things Person p = new Person("Pat"); System.out.println("p == " + p); p.setName("Pat Patterson"); System.out.println("p == " + p); pause(); // basic student doing studenty things // (including personny things) Student s = new Student("Warren"); System.out.println("s == " + s); s.setName("Warren Peace"); s.setGrade(90); System.out.println("s == " + s); System.out.println(s.getName() + "'s grade is " + s.getGrade()); pause(); // basic undergrad doing undergraddy things // (including studenty & personny things) Undergrad u = new Undergrad("Stu"); System.out.println("u == " + u); u.setName("Stu Dent"); u.setGrade(100); u.setYear(2); System.out.println("u == " + u); System.out.println(u.getName() + "'s grade is " + u.getGrade()); System.out.println(u.getName() + " is in year " + u.getYear()); pause(); // Activity 3 /* -- delete this line (and the one marked below) for activity 3 -- Lecturer l1 = new Lecturer("Zachary"); Lecturer l2 = new Lecturer("Wilhelmina", 11017.00); l1.writeOutput(); l2.writeOutput(); pause(); l1.setName("Zack"); l1.setStipend(10800.00); l1.writeOutput(); pause(); System.out.printf("%s's stipend is $%,4.2f. ", l1.getName(), l1.getStipend()); System.out.printf("%s's stipend is $%,4.2f. ", l2.getName(), l2.getStipend()); pause(); -- delete this line (and the one marked above) for activity 3 -- */ } /** * Prompt the user and wait for them to press the enter key. */ private static void pause() { System.out.print(" press enter..."); KBD.nextLine(); System.out.println(); }
-
/** * Root class for a simple inheritance hierarchy. * This class has both public and private methods. * Student extends this class. * * @author Mark Young (A00000000) */ public class Person { /** Every Person has a name */ private String name; /** * Person requires a name * * @param name the Person's name */ public Person(String name) { this.name = name; } /** * Return this Person's name * * @return this Person's name */ public String getName() { return this.name; } /** * Change this Person's name * * @param newName this Person's new name */ public void setName(String newName) { this.name = newName; } /** * A method for children classes to inherit */ public void publicPersonMethod() { System.out.println("\tin publicPersonMethod for " + this.name); } /** * A method for children classes to inherit */ public void callingPrivatePersonMethod() { System.out.println("\tin callingPrivatePersonMethod for " + this.name); privatePersonMethod(); System.out.println("\tcallingPrivatePersonMethod done"); } /** * A method children classes can't call *directly* */ private void privatePersonMethod() { System.out.println("\t\tin privatePersonMethod for " + this.name); } /** * A method for children classes to replace entirely. */ public void replacedMethod() { System.out.println("\tin replacedMethod for Person " + this.name); } /** * A method for children classes to add to. */ public void revisedMethod() { System.out.println("\tin revisedMethod for Person " + this.name); } /** * A method to represent this Person using a String * * @return a String representing this Person */ @Override public String toString() { return name; }
/** * A simplified Student class to demonstrate inheritance from Person. * This class has public and private methods. * Undergrad and GradStudent extend this class. * * @author Mark Young (A00000000) */ public class Student extends Person { public final String A_NUMBER; private int grade; public static final int MAX_GRADE = 100; private static int numStudents = 0; /** * Create a Student. A-number generated automatically. * * @param name this Student's name */ public Student(String name) { super(name); // set my name AS A Person grade = 0; A_NUMBER = nextANumber(); } /** * Change this Student's grade. * * @param newGrade the new grade */ public void setGrade(int newGrade) { if (isValidGrade(newGrade)) { this.grade = newGrade; } } /** * Return this Student's grade. * * @return this Student's grade */ public int getGrade() { return this.grade; } /** * A method for Undergrad to inherit. */ public void publicStudentMethod() { System.out.println("\tin publicStudentMethod for " + this); publicPersonMethod(); privateStudentMethod(); System.out.println("\tpublicStudentMethod done"); } /** * A method NOT to be inherited. */ private void privateStudentMethod() { System.out.println("\tin privateStudentMethod for " + this); } /** * Replacement for Person method */ @Override public void replacedMethod() { System.out.println("\tin replacedMethod for the Student " + this); } /** * Replacement for Person method that incorporates the inherited method */ @Override public void revisedMethod() { System.out.println("\tin revisedMethod for the Student " + this); super.revisedMethod(); System.out.println("\trevisedMethod for Student done"); } /** * Return a String representation of this Student * * @return a String representing this Student */ @Override public String toString() { return this.getName() + " (" + this.A_NUMBER + ")"; } /** * Check whether the given number is a valid grade. * Valid grades are between 0 and the maximum grade (inclusive). * * @param value the value to check */ private static boolean isValidGrade(int value) { return 0 <= value && value <= MAX_GRADE; } /** * Return the next available Student number. * * @return a previously unused Student number */ private static String nextANumber() { ++numStudents; return String.format("A%08d", numStudents); }
** * A class that extends Student. * * @author Mark Young (A00000000) */ public class Undergrad extends Student { /** * Undergrads have a year-of-study (usually 1 to 4) */ private int year; /** * Undergrads need a name and a year * * @param name this Undergrad's name * @param year this Undergrad's year */ public Undergrad(String name, int year) { super(name); this.year = year; } /** * Create a first year Undergrad. * * @param name this Undergrad's name */ public Undergrad(String name) { this(name, 1); } /** * return this student's year of study * * @return this student's year of study */ public int getYear() { return year; } /** * Change this student's year of study * * @param newYear this Student's new year of study */ public void setYear(int newYear) { this.year = newYear; } }
- package l05; import java.util.Scanner; /** * A program to demonstrate inheritance using a simple hierarchy. * * @author Mark Young (A00000000) */ public class InheritanceDemo { private static final Scanner KBD = new Scanner(System.in); public static void main (String [] args) { System.out.println(); // basic person doing personny things Person p = new Person("Pat"); System.out.println("p == " + p); p.setName("Pat Patterson"); System.out.println("p == " + p); pause(); // basic student doing studenty things // (including personny things) Student s = new Student("Warren"); System.out.println("s == " + s); s.setName("Warren Peace"); s.setGrade(90); System.out.println("s == " + s); System.out.println(s.getName() + "'s grade is " + s.getGrade()); pause(); // basic undergrad doing undergraddy things // (including studenty & personny things) Undergrad u = new Undergrad("Stu"); System.out.println("u == " + u); u.setName("Stu Dent"); u.setGrade(100); u.setYear(2); System.out.println("u == " + u); System.out.println(u.getName() + "'s grade is " + u.getGrade()); System.out.println(u.getName() + " is in year " + u.getYear()); pause(); // Activity 3 /* -- delete this line (and the one marked below) for activity 3 -- Lecturer l1 = new Lecturer("Zachary"); Lecturer l2 = new Lecturer("Wilhelmina", 11017.00); l1.writeOutput(); l2.writeOutput(); pause(); l1.setName("Zack"); l1.setStipend(10800.00); l1.writeOutput(); pause(); System.out.printf("%s's stipend is $%,4.2f. ", l1.getName(), l1.getStipend()); System.out.printf("%s's stipend is $%,4.2f. ", l2.getName(), l2.getStipend()); pause(); -- delete this line (and the one marked above) for activity 3 -- */ } /** * Prompt the user and wait for them to press the enter key. */ private static void pause() { System.out.print(" press enter..."); KBD.nextLine(); System.out.println(); } }
Examine the code and run the program (InheritanceDemo) to see how it works.
Programming Activity 1
Create a new class, Lecturer, that extends Person. A Lecturer is a Person with a stipend (a double value representing how much that lecturer will be paid for teaching a course). Create a constant for the default stipend ($10,165). Create two constructors for the class: one accepts a name and stipend amount; the other accepts just a name and uses the default stipend. Include any appropriate getters/setters.
Programming Activity 2
Give your Lecturer class a writeOutput method, which produces output like this:
Name: (name) Stipend: $(stipend)
Programming Activity 3
Test your Lecturer class by adding the following code to main in InheritanceDemo:
Lecturer l1 = new Lecturer("Zachary"); Lecturer l1 = new Lecturer("Wilhelmina", 11017.00); l1.writeOutput(); l2.writeOutput(); pause(); l1.setName("Zack"); l1.setStipend(10800.00); l1.writeOutput(); pause(); System.out.printf("%s's stipend is $%4.2f. ", g2.getName(), g2.getStipend());
Note: You do not need to submit your modified file InheritanceDemo. I know what it's supposed to look like!
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