Question
PYTHON: 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
PYTHON:
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. Solutions that do not call the toString method of the Person class will earn very little credit.
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. Solutions that do not call the age method will earn very little credit.
AdultStudent.java:
public class AdultStudent extends Person implements Comparable
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 ""; } }
Person.java
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); } }
I can't get the last three methods right for the AdultStudent class.
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