For this assignment you will write a subclass AdultStudent of the Person class.
ID: 3841054 • Letter: F
Question
For this assignment you will write a subclass AdultStudent of the Person class.
The AdultStudent class has one private instance variable major which is a String. It also has a private constant AGE_MAJ which is set to 18 and represents the age of majority in the United States.
For the assignment, you will write the AdultStudent class by writing the following methods:
public AdultStudent(): The default constructor for the class. It sets the name to "default". It sets the year to the current year minus AGE_MAJ. It should obtain the current year by using the LocalDate class (or equivalent -- contact me by email if you are using Java 7 since LocalDate is only available starting with Java 8). Solutions that hard-code 2017 into the constructor will not earn full credit. It should also set the major to "computer science".
public AdultStudent(String n, int y, String m): The parameterized constructor for the class. It sets the major using m and the name using n. It checks if the birth year y is valid for a person who would need to be at least AGE_MAJ years old as of the current year. (Again note that the current year should be obtained using the LocalDate class or equivalent. Any solutions that hard-code particular years into this method will not earn full credit). If y is valid for an at-least AGE_MAJ-year-old person, then it uses y to set bYear. Otherwise it sets bYear to the current year minus AGE_MAJ (to make the person AGE_MAJ years old as of this year).
public toString(): This method returns a String that displays the AdultStudent's name, birth year, and major. See the output from the driver program below to see examples of this. Note that you must return a String that looks precisely like the examples shown below. Also note that the toString method of the Person class must be called to get the part of the String representing the name and birth year.
public int compareTo(AdultStudent other): This method allows the class to implement the Comparable interface. It compares AdultStudents via ages. If the calling AdultStudent is younger than other, then the method returns a value < 0. If the calling AdultStudent is older than other, then the method returns a value > 0. If the two AdultStudent objects have the same age, the method returns 0. The method must call the age method of the Person class.
package people;
import java.time.LocalDate;
public class AdultStudent extends Person implements Comparable<AdultStudent>{
private String major;
private final int AGE_MAJ = 18;
public AdultStudent() {
}
public AdultStudent(String n, int y, String m) {
}
public int compareTo(AdultStudent other) {
// A stub -- remove when you write the method
return 0;
}
public String toString() {
// A stub -- remove when you write the method
return "";
}
}
package people;
import java.time.LocalDate;
public class Person {
protected int bYear;
protected String name;
public Person() {
LocalDate today = LocalDate.now();
name = "default";
bYear = today.getYear();
}
public Person(String initName, int birthYear) {
name = initName;
bYear = birthYear;
}
public int age() {
LocalDate today = LocalDate.now();
return today.getYear() - bYear;
}
public String toString() {
return String.format("Name: %s, birth year: %d", name, bYear);
}
}
Explanation / Answer
Providing the AdultStudent class method implementation:
import java.time.LocalDate;
public class AdultStudent extends Person implements Comparable<AdultStudent>{
private String major;
private final static int AGE_MAJ = 18;
public AdultStudent() {
super("default",LocalDate.now().getYear()-AGE_MAJ); //Call the constructor of the super class with the required values
this.major="computer science";
}
public AdultStudent(String n, int y, String m) {
super(n,(LocalDate.now().getYear())-y >=18 ? y:(LocalDate.now().getYear())-AGE_MAJ);
// Making Decision as to what values need to be send as
//parameters in constructor call to the Person class
this.major=m;
}
public int compareTo(AdultStudent other) {
return new Integer(this.age()).compareTo((Integer)other.age()); // Using compareTo on the age values
}
public String toString() {
return super.toString()+String.format("Major: %s",major);
// Printing the Values employing the toString() of the Person class along with additional Major plus its paramter value
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.