next year: staff rename .add to .hire or something? so it is more differentiated from arraylist
NOTE: No revisions will be accepted on these labs. Check your work thoroughly.
These labs are due on Tuesday, February 13th at 11:59pm. There is also a short written part due in class the next day (<3).
You can view a video version of this lesson here: https://www.youtube.com/watch?v=Gx3H-xqzhYE
1. You will create an inheritance setup to describe a workplace. Write each of the following classes:
a. StaffMember.java (ABSTRACT). This abstract class should have the following instance variables and methods:
-a String representing the person’s name
-an int representing the ID number
-an int representing the person’s salary
-StaffMember(String name, int ID) – this constructor inputs a String and an int in that order and sets up the instance variables. It does NOT set up the salary variable
-getName()//accessor methods
-getSalary()
-getID()
-changeID(int newID) //this void mutator method should input an int and change the ID number
-changeSalary(int newSalary) //void mutator that changes the salary
-toString() - have it return the name and ID number of the staff member
-equals(Object other) – write this method so it inputs an Object [of any type] and make sure that this method returns FALSE if the object is not a StaffMember, and TRUE if the object is a StaffMember AND has the same ID number.
b. Volunteer.java. This class extends StaffMember with the following changes:
-it is not abstract
-constructor has the same inputs (String name, int ID) and sets salary = 0;
c. Employee.java. This class extends StaffMember with the following changes:
-it is not abstract
-it should have a new private vacationDays variable that is an int.
-constructor now inputs a String, and two ints in that order representing (String name, int IDNumber, int Salary) and sets them up. It also sets the number of vacation days to be 10.
-getVacationDays() //returns the number of vacation days left.
-changeVacationDays(int x) //a void method that lets you change the instance variable
//write both of the following vacation methods (it is overloaded):
-vacation() //this void method takes no inputs and subtracts one vacation day if the Employee has at least 1 day left. If they are out of vacation days, it should throw an exception (an ArithmeticException or NullPointerException or anything else is fine).
-vacation(int n) //this overloaded void method takes an int input (n) and subtracts n vacation days if the Employee has at least n days left. If they do not have enough vacation days, it should throw an exception. Any type of exception is fine (ArithmeticException, NullPointerException, whatever you want).
d. Boss.java. This class extends Employee with the following changes:
-vacation days is set to 20 in the constructor (the constructor will have the same inputs a String and two ints)
-a void method called awardBonus(int n) which inputs an int n and adds it to the salary of the boss.
2. Now you will write a Staff.java class. The main function of this class is to keep track of all the Staff Members at a company in an ArrayList instance variable. It also has other features. ALL METHODS IN THIS CLASS SHOULD BE NON-STATIC. It has the following methods and instance variables:
-an ArrayList of type <StaffMember> called workers representing the staff.
-a constructor with no inputs that sets up the ArrayList as a new, empty ArrayList
-add( ) – this void method inputs a StaffMember and adds it to the workers ArrayList
[Think carefully about casting in these methods!]
-allSalary() – this method takes no inputs and returns an int representing the sum of all salaries in workers.
-countVolunteers() – this method takes no inputs and returns how many volunteers are in workers.
-getBoss(String name) – this method inputs a String name and outputs a Boss - it searches through workers for a boss of that name. If one is found, it returns that Boss object (if there are multiple it can return any of them). Otherwise it returns null.
-richest() - this method takes no input and returns a StaffMember object. Specifically, it should return the object in workers with the highest salary. If more than one StaffMember is tied, you can return either one.
-downsize()- this void method, which takes no input, will go through workers, remove all of the volunteers, and then, for each Employee who is not a boss, subtract 5 vacation days. If this would bring them below 0, set their number of vacation days to be 0. The vacation days for each Boss should not be changed. Since this is a punishment not a break, call changeVacationDays() but DO NOT call the vacation() method.
-countDupes() – this method inputs a separate ArrayList of type <StaffMember>, and, through the magic of for loops, returns an int representing the number of StaffMember objects who appear in both lists (using .equals may be smart). You may assume each of the two lists will have no duplicates when the method is called.
For example, if workers has 4 members with ID numbers (2,12,5,3) and you send this an ArrayList of Employee objects with ID numbers of (5,3,1) it should return 2 because 2 employees appear on both lists (the ones with IDs 5 and 3).
****Test this Staff class and all the methods thoroughly in a Driver class before you proceed!****
3. (this part of the lab is WRITTEN/TYPED - to be turned in the day after the code is due) Here is some code I have written in a separate driver class using the above code. Go through this code. Which lines do not compile? For every line that does not compile, write a brief sentence in your own words on why that line does not compile.
public static void main()
{
1. StaffMember dude = new StaffMember();
2. StaffMember katie = new Boss("Katie",2,14);
3. Employee jane = new Boss("Jane",35,1235135);
4. Volunteer alex = new Boss("alexia",3,135);
5. Boss dautremont = new Boss("GreatestEver",315,15135);
6. dautremont.awardBonus(30);
7. katie.awardBonus(20);
8. ((Boss) jane).awardBonus(315);
9. Staff a = new Staff();
10. a.add(new Boss("billy",215,125));
11. a.add(new StaffMember("steve",230));
12. a.add(new Employee("whitney",345,135));
13. a.add(new Volunteer("jimmy",3564));
14. a.remove(2);
15. StaffMember cool = a.getBoss("Hagman");
16. StaffMember briana = new Boss("batman",35,153);
17. ((Employee)briana).vacation();
18. StaffMember jace = new Volunteer("Jace",7);
19. ((Employee)jace).vacation();
}
4. (back to coding which you will submit in your usual folder) Now you will play with Polymorphism and Abstract classes dealing with objects that allow you to watch movies.
a. Write an abstract class called MovieWatcher with the following public abstract methods, all of which take no inputs:
-int getRewindTime()
-void play()
-boolean skipPreviews()
Now write the following classes, which must EXTEND MovieWatcher.
b. VCR.java – When your teacher was a kid, DVDs had not been invented yet and we had to watch movies on VHS tapes. You had to rewind it after each time you watched it and the picture quality sucked. This class represents a VCR, a type of MovieWatcher, and contains the following variables and methods:
-an int instance variable representing a rewind time [the amount of minutes it takes for the VCR to rewind a movie. Old VCRs usually 3-5 minutes usually]
-a constructor which takes one int input representing the rewind time and sets that up
-getRewindTime() is an accessor method
-play() prints the name of your favorite movie. Probably Star Wars or The Dark Knight or Happy Gilmore or something.
-skipPreviews() returns false //you could not just skip the previews on a tape. There was no "menu" button. It was like being at a movie theater except you could fast forward (slowly).
c. DVDPlayer.java – this simple class (which extends MovieWatcher) contains the following:
-a no input constructor that does nothing
-play() prints the name of your favorite movie
-getRewindTime() returns 0
-skipPreviews() returns true
d. Computer.java – this class extends DVDPlayer and contains the following:
-a String representing the computer name
-an int representing the number of processors
-a constructor taking in two inputs – a String for the name, and an int for the number of processors, in that order. (so I can call something like MovieWatcher mine = new Computer("WorkComp",2); )
-getName() – an accessor method
-getProcs() - an accessor number of processors
-play() prints the name of your favorite movie
-getRewindTime() returns 0
-skipPreviews() returns true
5. Now you will write a MovieDriver.java class. ALL METHODS IN THIS CLASS WILL BE STATIC. Most of these methods will input a MovieWatcher object (recall that your previous classes all implement MovieWatcher) and be fun, provided you remember how to cast these things and how to use instanceof. Write the following methods:
//reminder: ALL OF THESE METHODS ARE STATIC!
-playN( ) – this void method inputs a MovieWatcher and an int n in that order(so if you have MovieWatcher p = new DVDPlayer(), you would call playN(p,20) ), then calls the play() method on that MovieWatcher object n times. If n is less than or equal to 0, this method does nothing.
-timeToWatch( ) – this method inputs a MovieWatcher and an int n (representing the length of a movie) in that order and returns an int. It then adds the MovieWatcher’s getRewindTime() to n, subtracts 5 minutes if that Object’s skipPreviews() returns true, and returns that int.
-canPlayGames( ) – this method inputs a MovieWatcher object and returns a boolean. If the input is a Computer with at least two processors, it returns true, otherwise it returns false.
-upgrade( ) – this method inputs a MovieWatcher and returns a Computer object. If the input is a Computer with 2 or more processors, this method returns that Computer. Otherwise, it creates a new Computer("John",4) and returns that.
-removeOld( ) - this method inputs an ArrayList of MovieWatcher objects and removes all VCRs, returning an ArrayList of MovieWatcher objects. Be careful!
-anyType(String desiredOutput) - this method inputs a String and returns an Object variable. It should return an object of the type that matches the input (at least, for a few possibilities). If the input is a String saying "VCR", "DVDPlayer", "Computer" or "Volunteer", it should return that type of object (using any kind of constructor inputs, I don't care). For any other input besides those four, it should return a String representing an error message. So if you call anyType("DVDPlayer"), this should return a DVDPlayer object. If you call anyType("Banana") it should return a String saying something like "this method is not designed to return Banana objects".
Phew! Submit StaffMember.java, Employee.java, Volunteer.java, Boss.java, Staff.java, MovieWatcher.java, VCR.java, Computer.java, DVDPlayer.java and MovieDriver.java. Remove any other files in your folder and make sure your method names are spelled correctly! You will turn in the written part 3 in class.