CS 1302 – HW 5
This homework has 2 problems which deal with abstract classes, interfaces, Cloneable and Comparable (Ch 13)
Problem 1 – 20 Points
- Create a Java Project in Eclipse with the name: hw5_lastName
- Create a package named prob1 and write the classes indicated below.
- (Read – no action required) For this problem, you will start with the incomplete class below. You will modify itand then write several subclasses as described below.
publicabstractclass Cake {
private String cakeMix;
public Cake(String cakeMix) {
this.cakeMix = cakeMix;
}
public String makeCake() {
String cake = "";
cake += blend();
cake += pour();
cake += bake();
returncake;
}
public String bake() {
return"Bake at 350 degrees F 30 minutes";
}
public String getCakeMix() {
returncakeMix;
}
private String blend() {
String cake = "Blend:(";
cake += getCakeMix() + ", ";
cake += getLiquid() + ", ";
cake += getOil() + ", ";
cake += getEggs() + ")\n";
returncake;
}
public String pour() {
return"Pour mix into pan\n";
}
}
- Create a Cake class in your prob1 folder and copy the code above. It will have compile errors.
- Modify the Cake class as follows:
- The makeCake and blend methods can not be overridden. Thus, modify them appropriately.
- Add three abstract methods: getLiquid, getOil, getEggs. These methods do not accept an arguments and theyall return a string. The Cake class should now compile.
- Create a ClassicYellowCake class that is a subclass of Cake that accepts a cake mix and use 1 cup tap water for the liquid, 1/3 cup vegetable oil for the oil, and 3 large eggs for the eggs. For example, getLiquid should be overridden to return, “1 cup tap water”. Similar for the other methods.
- Create a CakeDriver class and use this code in main to test ClassicYellowCake:
Cake classicYellow = new ClassicYellowCake("Duncan Hines Classic Yellow Cake Mix");
System.out.println("Classic Yellow Cake");
System.out.println("------");
System.out.println(classicYellow.makeCake());
Which should produce this output:
Classic Yellow Cake
------
Blend:(Duncan Hines Classic Yellow Cake Mix, 1 cup tap water, 1/3 cup vegetable oil, 3 large eggs)
Pour mix into pan
Bake at 350 degrees F 30 minutes
- Create a HighAltitudeClassicYellowCakeclass that is a subclass of ClassicYellowCake that accepts a cake mix. This cake uses the same liquid, oil, and eggs as a ClassicYellowCake; however, you add 2 tbs flour to the cake mix (Hint: override getCakeMix which should call the superclass getCakeMix and add 2 tbs flour to it). Finally, it needs to bake at 375 degrees F for 26 minutes (Hint: override the bake method).
- Add this test code to main in CakeDriver:
Cake highAltitudeClassicYellow = new HighAltitudeClassicYellowCake("Duncan Hines Classic Yellow Cake Mix");
System.out.println("\nHigh Altitude Classic Yellow Cake");
System.out.println("------");
System.out.println(highAltitudeClassicYellow.makeCake());
Which should produce this output:
High Altitude Classic Yellow Cake
------
Blend:(Duncan Hines Classic Yellow Cake Mix + 2 tbs flour, 1 cup tap water, 1/3 cup vegetable oil, 3 large eggs)
Pour mix into pan
Bake at 375 degrees F 26 minutes
- Create an OrganicCakeclass that is a subclass of Cake that accepts a cake mix and use 1 ¼ cup milk for the liquid, 1/2 cup canola oil for the oil, and 2 large eggs for the eggs.
- Add this test code to main in CakeDriver:
Cake organicVanilla = new OrganicCake("Organic Vanilla Cake Mix");
System.out.println("\nOrganic Vanilla Cake");
System.out.println("------");
System.out.println(organicVanilla.makeCake());
Which should produce this output:
Organic Vanilla Cake
------
Blend:(Organic Vanilla Cake Mix, 1 1/4 cup milk, 1/2 cup canola oil, 2 large eggs)
Pour mix into pan
Bake at 350 degrees F 30 minutes
Problem 2 – 80 Points
- Create a package named prob2and write the classes indicated below.
- (Read – no action required) Consider the class diagram shown below. You will write all six classes/interfaces (Martian, GreenMartian, RedMartian, MartianManager, Teleporter, MartianTester) as described below.
- (Read – no action required) – I suggest you write the classes and methods in the order presented below. You should test each method as you complete it. You might also look ahead to see the test code you are required to write in MartianTester and possibly write that as you go along.
- Martian Class:
Member / Description
Martian(id:int) / Creates a Martian with an id and sets the volume to 1.
clone():object / Creates and returns a clone of the martian.
compareTo(Martian m):int / Implements Java’s compareTo so that Martians (either red or green) are compared based on their id’s which results in the usual ascending sorted order.
equals(o:Object):bool / Two martians are equal if their id’s are equal, regardless of whether they are green or 4ed.
getId():int / Returns the id.
getVolume():int / Returns the volume.
setVolume(level:int) / Sets the volume to level.
speak():string / Abstract.
toString():string / Returns a string like: “id=xxx, vol=yyy”, where xxx is the id and yyy is the volume.
- RedMartian Class:
Member / Description
speak():string / Returns a string like: “id=xxx, Rubldy Rock”, where xxx is the id.
toString():string / Returns a string like: “Red Martian - id=xxx, vol=yyy”, where xxx is the id and yyy is the volume.
- Write the Teleporter interface.
- GreenMartian Class:
Member / Description
speak():string / Returns a string like: “id=xxx, Grobldy Grock”, where xxx is the id.
teleport(dest:string):string / The GreenMartian implements the teleport method by returning a string like this: “id=xxx teleporting to dest”.
toString():string / Returns a string like: “Green martian - id=xxx, vol=yyy”, where xxx is the id and yyy is the volume.
- Add a MartianTester class and a main. Write code for the following tests as described.
// Test 0–Create MartianManager and create some red and green martians
// to be used in tests that follow.
// Test 1 - Martian.toString()
// Test 2 - Martian.equals()
// Test 3 - Martian.compareTo()
// Test 4 - Martian.clone()
- MartianManager Class:
Member / Description
martians / A list Martian objects.
teleporters / A list of Teleporter objects.
addMartian(m:Martian):boolean / Adds m(either red or green) to martians, only when it doesn’t already exist in the list of martians. Remember, martians are the same (no matter if red or green if they have the same id. Hint: use the contains method which will work because you overrode the equals method in the Martian class). If m is a GreenMartian(and unique, i.e. doesn’t already exist, as described above), then it is also added to the teleporters list. If the add is successful, then return true, otherwise return false.
Write code in MartianTester for this test:
// Test 5 - MartianManager.addMartian()
groupSpeak():String / Returns a string with each Martian in martians speaking (line break between each).Write code in MartianTester for this test:
// Test 6 - MartianManager.groupSpeak()
groupTeleport(dest:string):String / Returns a string showing the result of each Teleporter inteleporters teleporting to the dest (line break between each).Write code in MartianTester for this test:
// Test 7 - MartianManager.groupTeleport()
getNumMartians():int / Return the total number of martians.getNumTeleporters():int / Return the total number of teleporters.
getMartianAt(i:int):Martian / Returns the Martian in martians at location i or null if the location is invalid.
Write code in MartianTester for this test:
// Test 8 - MartianManager.getMartianAt()
getTeleporterAt(i:int):Teleporter / Return the Teleporter in teleporters at location ior null if the location is invalid.Write code in MartianTester for this test:
// Test 9 - MartianManager.getTeleporterAt()
getMartianWithId(id:int):Martian / Return the Martian in martians with id. Hint: create a dummy martian (either red or green) with id and then use indexOf to see if it exists. If it does exist, then use the index to get and return the martian. Return null if not found.Write code in MartianTester for this test:
// Test 10 - MartianManager.getMartianWithId()
getMartianClosestTo(id:int):Martian / Return the Martian that has id closest to the input id. Note: “closest” doesn’t mean the next martian. For example, if you have ids: 6, 9, 13, 18 and you call the method with 10, then the martian with id=9 should be returned.Write code in MartianTester for this test:
// Test 11 - MartianManager.getMartianClosestTo()
removeMartian(id:int):boolean / Remove the Martian in martians with id. Must also remove from teleporters if it is a GreenMartian. Martians are unique so there is at most 1 to remove. This method should return true if remove was successful, and false if not found. Hint: (1) Create a “dummy” GreenMartian with the id. (2) use indexOf to get index of Martian, (3) use get to get the Martian, save him for a minute, (4) use remove(index) to remove from martians, (5) if Martian from step 3 is a GreenMartian then (a) cast it to a Teleporter, (b) remove from teleportersusing remove(teleporter)Write code in MartianTester for this test:
// Test 12 - MartianManager.removeMartian()
sortedMartians():ArrayList<Martian> / Return a list of all the martians in martians sorted on their id’s. This should not sort martians. Hint: clone martians and then sort the clone.Write code in MartianTester for this test:
// Test 13 - MartianManager.sortedMartians()
obliterateTeleporters() / Remove all teleporters and the corresponding GreenMartians in martians.Write code in MartianTester for this test:
// Test 14 - MartianManager.obliterateTeleporters()
clone():Object / Creates a clone of the MartianManagerobject. Hint: this requires a deep copy.Write code in MartianTester for this test:
// Test 15 - MartianManager.clone()
Submission
Zip your prob1and prob2folders into a file named: hw5_lastname.zip.
1