Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this Java assignment you will write a subclass AdultStudent of the Person class. The Person class is a slightly modified version of the class

For this Java assignment you will write a subclass AdultStudent of the Person class. The Person class is a slightly modified version of the class we discussed, and the Person.java file you should use is included in the zip files uploaded on the class website. Also on the class website is a template for the AdultStudent class in the file AdultStudent.java. Please start the assignment by downloading those two files. (They can be found below the bold text at the bottom of the Chegg page)

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. You are not allowed to create any extra variables in the class, other than variables local to the methods.

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 2018 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.

I have provided a driver program Hw7.java to help you test your code which has been posted to the class website. Be aware that the grader may add to that program when grading, so if you feel that any of your methods haven't been tested effectively by the driver program, please modify as necessary. However, you should only submit the AdultStudent class.

Below is the sample output that the test program produces for my solutions to the AdultStudent class:

The students: Name: Joon, birth year: 1994, major: art history Name: default, birth year: 2000, major: computer science Name: Gertrude, birth year: 2000, major: digital cinema The largest element in the AdultStudent array is: Name: Joon, birth year: 1994, major: art history

AdultStudent.java :

package people;

import java.time.LocalDate;

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 :

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);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions