ADD a bit to these??
These labs will be due by 11:59pm on Wednesday, February 7th. As always, submit all files to your named google drive folder.
Here is a video version of this lesson: https://www.youtube.com/watch?v=tUXzNjCT_Mk
1. For this part, you will create a project with an expansive inheritance tree that includes several abstract classes. Be careful with super( ) and in general throughout your code!
a. Create an abstract Human class (Human.java). This class will have the following instance data and filled out methods:
-a private String representing the name of the Human.
-a constructor with one input (a String representing the name) that sets up the String variable
-getName(), an accessor method that returns the name.
It should also include the following abstract methods:
-boolean canVote()
-String toString()
b. Create an abstract Student class (Student.java). This abstract class will inherit from Human with the following new variables and filled out methods:
-an additional int instance variable representing the grade the student is in
-an additional double instance variable representing the GPA of the student
-a constructor which takes 1 input for the name of the student (note: this constructor will NOT set up the two new instance variables, only the name variable)
-getGrade() – accessor method returning the int representing the grade
-getGPA() – accessor method returning a double
-setGPA(double input) - mutator methods for GPA and grade
-setGrade(int input)
-fill out toString() so it returns a combination of the name, grade and GPA of the student (notice how you can do that even though they have never been filled out in this class!)
It should also contain the following abstract method:
-boolean enjoySchool()
c. Create a class named Freshman.java. This class will inherit from Student with the following additions:
-a constructor which takes 2 inputs for the name and GPA of the student (in that order: String, double) and sets the variables up properly. It should set grade to be 9. To write this constructor, you need to call some methods in Student.
-enjoySchool() should return false
-canVote() should return false
d. Create a class named Senior.java. This class will inherit from Student with the following additions:
-a constructor which takes 2 inputs for the name and GPA of the student (in that order: String, double) and sets the variables up properly. It should set grade to be 12.
-enjoySchool() should always return true if the students GPA is over 4.0, otherwise, it should return true 25% of the time and false the other 75% (use a random number generator).
-canVote() will return true unless the student’s GPA is under 1.0 in which case, it returns false (felons can’t vote).
-create a void method called graduate(); This method takes no inputs, sets the grade variable to be 0, then prints a celebratory message (keep it clean).
e. Create a class called StudentClub.java. This class will not extend any of your other classes. It will contain two instance variables: an ArrayList<Student> called "members" that represents the students in the club, and a String called "president".
The constructor will input an array (NOT ARRAYLIST) of students, Student[] peeps. It will then copy over the elements from that array into the members ArrayList instance variable. It should also set the president instance variable to be null (you haven't picked a president yet!). So to set up this class you will write code in a driver like this:
Student[] p = new Student[3];
p[0] = new Freshman(...);
p[1] = new Freshman(...);
p[2] = new Senior(...);
StudentClub chess = new StudentClub(p);
Your StudentClub method will contain the following methods:
-countMembers() - this method with no inputs returns an int representing the amount of students in the club (the size of the members ArrayList instance variable).
-newMember() - this void method should input a Student object and add them to the end of the members instance variable.
-getPresident() - this method should take no inputs and return a String. It is an accessor for the president instance variable.
-removeSeniors() - this void method has no input. This method should look through the instance variable and remove all Senior objects from the list. So if your ArrayList contains [a Freshman, a Senior, a Senior, a Freshman, a Senior], after calling the method it should only contain the two Freshman objects.
-shamElection() - this method will take no inputs and return a Student. This method should pick a random Student in the members instance variable, set the president variable to equal that person's name, and return that Student object.
Submit Human.java, Student.java, Freshman.java, Senior.java, StudentClub.java
2. Create a class called HumanDriver with the following methods. ALL METHODS IN THIS CLASS SHOULD BE static!!!
a. averageGPA( ) – this method inputs an array of Students (Student[]) and returns a double representing their average GPA. (Note that this will work even though Student is abstract because the getGPA() method is defined in Student, which Freshmen and Seniors will both inherit from. This method would NOT work for an array of Humans)
b. countBobs( ) – this method inputs an array of humans (Human[]) and counts how many of them have the name “Bob” (note that it is capitalized) and returns that int.
c. countVoters() – this method inputs an ArrayList of Humans (ArrayList<Human>), then returns an int representing the number of voters (people for whom canVote() is true) in the ArrayList.
d. valedictorian( ) – this method inputs an array of Seniors (Senior[]) and returns the name (a String) of the student with the highest GPA.
e. honorRoll() - this method inputs an array of Students (Student[]) and returns an array of Students (Student[]). The array it returns should consist ONLY of Seniors with a GPA greater than or equal to 4.0 and Freshman with a GPA greater than or equal to 3.5. This if you send it {senior1 - 3.9, senior2 - 4.3, freshman1 - 2.2, freshman2 - 3.7} it should return an array containing only senior2 and freshman2. Make sure the size of the array you return is correct!
*HINT: when you are testing this method, remember that arrays do not print out nicely. Call the method and save it as a variable (like Student[] ans = honorRoll(...);) and then run a for loop to print out each element of ans.
f. endOfYear() - this void method inputs an ArrayList of Students (ArrayList<Student>) and removes (careful!) all of the seniors with a GPA above 1.0 from the list. It should then print a congratulatory message/speech to the screen. So for instance, if you send it an ArrayList with two Freshman and three Seniors with GPAs above 1.0, after this method is called, that ArrayList should only contain the two Freshman.
g. removeAliens() - this method inputs an ArrayList of Object variables (ArrayList<Object>) and returns an int. It should remove all of the non-Human objects in the list and return the number of objects removed. For example, if you send this method an ArrayList containing [a Freshman, a Senior, a String, an array of ints, a Senior, a Dog], after this method executes, it should contain [a Freshman, a Senior, a Senior] and then return 3.
h. froshRun() - this method inputs an array of Human objects (Human[]) and returns an int. Specifically, it returns the length of the longest consecutive run of Freshman objects in a row. For example, if you send the method an array containing the following objects: {Freshman, Freshman, Adult, Senior, Freshman, Freshman, Freshman, Senior, Senior, Senior, Senior, Adult, Freshman), it should return 3 because it found 3 Freshman objects in a row [from locations 4 to 6].
i. TEST THIS LAB THOROUGHLY! It is easy to make some small mistakes in your HumanDriver class. Write a main method and call each of these methods.
Submit HumanDriver.java
EXTRA CREDIT: Want an extra challenge and some extra credit? On codingbat.com, complete the following assignments under the AP section:
-bigHeights
-scoresClump
This will be worth from 1-2 extra credit points depending on how many you complete. As always, email me once you finish if you decide to do the extra credit (and make sure that you have shared with me through the preferences on that site).