Except where otherwise noted, the contents of this document are Copyright 2012 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp and Stuart Reges
Goals for today:
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
, and
getVacationForm
.
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.
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.
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.
import java.awt.*; public class ClassName extends Critter { fields, constructors... public boolean eat() { ... } public Attack fight(String opponent) { ... } public Color getColor() { ... } public Direction getMove() { ... } public String toString() { ... } }
Skunk
errors
The following critter
(
Skunk.java
)
is an attempt to make a critter that goes W, W, N and repeats, unless he eats food, in which case he will start going W, W, S.
But the file contains errors.
Download it and fix the errors so it compiles/runs properly.
Test it in CritterMain
and Practice-It.
public class Skunk extend Critter { private int moves; private boolean hungry; public void Skunk() { // constructor hungry = false; } public static boolean eat() { hungry = true; return true; } public Direction getmoves() { moves++; if (moves >= 3) { moves = 0; } if (moves == 1 && moves == 2) { return Direction.WEST; } else if (hungry) { return Direction.NORTH; } else if (!hungry) { return Direction.SOUTH; } } }
Butterfly
Write a class Butterfly
that extends the Critter
class, along with its movement behavior.
All unspecified aspects of Butterfly
use the default behavior.
A Butterfly
should be yellow in color.
Its toString
should alternate between being an x
character and a -
character each move.
A Butterfly
flies upward in the following pattern: N, W, N, E, repeat.
Solve this program in jGRASP using the CritterMain
simulator.
Test it in Practice-it.
Hyena
Write a class Hyena
that extends the Critter
class, along with its movement behavior.
All unspecified aspects of Hyena
use the default behavior.
A Hyena
object moves in a rectangular pattern looking for food, walking NORTH, then EAST, then SOUTH, then WEST.
Each time the hyena walks an entire rectangle, it starts the rectangle pattern over again but with a rectangle 1 step wider than before.
The general pattern is as follows:
Solve this program in jGRASP using the CritterMain
simulator.
Hyena
revisited
Modify your Hyena
class from the previous problem to add eating behavior.
If the hyena encounters food at any point during its movement pattern, it eats the food and starts the pattern over, lengthening the rectangular pattern by 1 in the process.
For example:
Solve this program in jGRASP with CritterMain
, then test it using the Practice-it link above.
Shark
Shark
objects should alternate between moving to the north and south as follows:
first move 1 step north, then 2 steps south, then 3 steps north, then 4 steps south, then 5 steps north, then 6 steps south, and so on, each time moving one farther than previously.
Solve this program in jGRASP with CritterMain
, then test it using the Practice-it link above.
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!