Grid
holds instances of Actor
subclassesRock
, Bug
, Critter
act
.act
calls move
. Sometimes it calls turn
.public class BoxBugRunner { public static void main(String[] args) { ActorWorld world = new ActorWorld(); BoxBug bob = new BoxBug(3); world.add(new Location(5, 5), bob); world.show(); } }
BoxBug
class.act
method.ArrayList<Actor> getActors() void processActors(ArrayList<Actor> actors) ArrayList<Location> getMoveLocations() Location selectMoveLocation(ArrayList<Location> locs) void makeMove(Location loc)
getActors
gets neighborsprocessActors
eats any actors that aren't rocks or critters.getMoveLocations
gets empty neighbor locationsselectMoveLocation
selects location at randommakeMove
moves to the given locationWhat is wrong?
public class Account { private double balance; public Account() { balance = 0; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } } public class CheckingAccount extends Account // Charges $1 per withdrawal { private double balance; public void withdraw(double amount) { balance = balance - amount - 1; } }
public class Account { private double balance; public Account() { balance = 0; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } } public class CheckingAccount extends Account // Charges $1 per withdrawal { private double balance; public void withdraw(double amount) { balance = balance - amount - 1; } }
What is wrong?
public class Account { private double balance; public Account() { balance = 0; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } } public class CheckingAccount extends Account // Charges $1 per withdrawal { private double balance; public void withdraw(double amount) { withdraw(amount + 1); } }
class Account { private double balance; public String type = "Generic"; public Account() { balance = 0; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } } class CheckingAccount extends Account // Charges $1 per withdrawal { public String type = "Checking"; public void withdraw(double amount) { super.withdraw(amount + 1); } }
public class Demo { public static void main(String[] args) { Account acct = new CheckingAccount(); System.out.println(acct.type); } }
type
into methods?
public String type() { return "Generic" ;}; public String type() { return "Checking" ;};