Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Some extensions by T W Bennet
Goals for this problem set:
public class ClassName extends SuperClass {
...
}
super keyword:super.methodName(parameters);
Car and Truck
public class Car {
public void m1() {
System.out.println("car 1");
}
public void m2() {
System.out.println("car 2");
}
public String toString() {
return "vroom";
}
}
|
public class Truck extends Car {
public void m1() {
System.out.println("truck 1");
}
}
|
Truck mycar = new Truck(); System.out.println(mycar); // vroom mycar.m1(); // truck 1 mycar.m2(); // car 2
Car and Truck revisited
public class Car {
public void m1() {
System.out.println("car 1");
}
public void m2() {
System.out.println("car 2");
}
public String toString() {
return "vroom";
}
}
|
public class Truck extends Car {
public void m1() {
System.out.println("truck 1");
}
public void m2() {
super.m1();
}
public String toString() {
return super.toString() + super.toString();
}
}
|
Truck code changes as shown above.
What is the output now?
Truck mycar = new Truck(); System.out.println(mycar); // vroomvroom mycar.m1(); // truck 1 mycar.m2(); // car 1
MonsterTruck
MonsterTruck that has the behavior below.
Test by running AutoMain.
/.
MonsterTruck bigfoot = new MonsterTruck(); bigfoot.m1(); // monster 1 bigfoot.m2(); // truck 1 / car 1 System.out.println(bigfoot); // monster vroomvroom
Employee and has the methods
getHours,
getSalary,
getVacationDays,
getVacationForm,
work.
Marketer
Marketer to accompany the other employees.
Marketers make $50,000 ($10,000 more than general employees),
and they have an additional method named advertise that prints "Act now, while supplies last!"
super keyword to interact with the Employee superclass as appropriate.
work method which calls advertise. See
the existing classes for examples.
Janitor
Janitor to accompany the other employees.
Janitors work twice as many hours (80 hours/week),
they make $30,000 ($10,000 less than others),
they get half as much vacation (only 5 days),
and they have an additional method named clean that prints "Workin' for the man."
super keyword to interact with the Employee superclass as appropriate.
work method which calls clean.
HarvardLawyer
HarvardLawyer to accompany the other employees.
Harvard lawyers are like normal lawyers, but they make 20% more money than a normal lawyer,
they get 3 days more vacation, and they have to fill out four of the lawyer's forms to go on vacation.
That is, the getVacationForm method should return "pinkpinkpinkpink".
Lawyer's vacation form ever changed, the HarvardLawyer's should as well.
For example, if Lawyer's vacation form changed to "red",
the HarvardLawyer's should return "redredredred".)
super keyword to interact with the Employee superclass as appropriate.
Write a small Java program in which you
work
method, and print out the hours, salary, vaction days and form color.The output from your program should look something like this:
Taking dictation of text: blah blah blah Hours: 40 Paid: 40000.0 Vac: 10 days on yellow form. I'll see you in court! Hours: 40 Paid: 40000.0 Vac: 15 days on pink form. I could file all day! Hours: 40 Paid: 45000.0 Vac: 10 days on yellow form. Workin' for the man. Hours: 80 Paid: 30000.0 Vac: 5 days on yellow form. I'll see you in court! Hours: 40 Paid: 48000.0 Vac: 18 days on pinkpinkpinkpink form. Act now, while supplies last! Hours: 40 Paid: 50000.0 Vac: 10 days on yellow form.