Orientation: You will implement a system of classes to represent a TotemPole for
ID: 3675367 • Letter: O
Question
Orientation:
You will implement a system of classes to represent a TotemPole for the fictional Gashunonga
Tribe. A TotemPole is comprised of different animal heads (Bear, Snake, and Eagle).
Every TotemPole has an Eagle at the top. Eagles can only be at the top of the pole. Each animal head adds a different amount of power to the TotemPole. Bears are the most sacred to the Gashunongan people and add 5 power points to the TotemPole. Snakes add 3 power points. Eagles add 2 power
points. Since Bears are the most sacred, a Gashunongan Chief's TotemPole must have at least 3 Bears in a row at some place in the TotemPole.
Specification:
Develop the following interface and classes:
Develop an interface called TotemPole. A TotemPole has the following methods:
int power(); // the total power of the pole
int height(); // the number of heads in the pole
boolean chiefPole(int bearCount); // is this pole worthy of a chief?
Develop the class Bear that implements the TotemPole interface. Bears have the following constructor: public Bear(TotemPole rest);
Develop the class Snake that implements the TotemPole interface. Snakes have the following constructor: public Snake(TotemPole rest);
Develop the class Eagle that implements the TotemPole interface. Eagles have the following constructor: public Eagle();
Explanation / Answer
TotemTest.java
public class TotemTest
{
TotemPole p1, p2, p3;
public static void main(String[] args)
{
new TotemTest().run();
}
public TotemTest()
{
p1 = new Bear(
new Bear(
new Snake(
new Snake(
new Snake(
new Bear(
new Eagle()))))));
p2 = new Snake(
new Bear(
new Bear(
new Bear(
new Snake(
new Eagle())))));
p3 = new Eagle();
}
public void run()
{
boolean pass = true;
pass &= testHeight();
pass &= testPower();
pass &= testChiefPole();
if (pass)
System.out.println("Congrats, you pass. The Gashunongan people rejoice!");
else
System.out.println("Not done yet. I hope the Gashunongan people aren't head hunters!");
}
public boolean testHeight()
{
boolean pass = true;
pass &= test(p1.height(), 7, 1);
pass &= test(p2.height(), 6, 2);
pass &= test(p3.height(), 1, 3);
return pass;
}
public boolean testPower()
{
boolean pass = true;
pass &= test(p1.power(), 26, 4);
pass &= test(p2.power(), 23, 5);
pass &= test(p3.power(), 2, 6);
return pass;
}
public boolean testChiefPole()
{
boolean pass = true;
pass &= test(p1.chiefPole(0), false, 7);
pass &= test(p2.chiefPole(0), true, 8);
pass &= test(p3.chiefPole(0), false, 9);
return pass;
}
private static boolean test(int test, int expect, int testNum)
{
if (test != expect)
{
System.out.println(" FAILED test #" + testNum);
return false;
}
return true;
}
private static boolean test(boolean test, boolean expect, int testNum)
{
if (test != expect)
{
System.out.println(" FAILED test #" + testNum);
return false;
}
return true;
}
}
TotemPole.java
public interface TotemPole {
public int power(); // the total power of the pole
public int height(); // the number of heads in the pole
public boolean chiefPole(int bearCount); // is this pole worthy of a chief?
// bearCount is how many bears encountered in a row so far
}
Snake.java
public class Snake implements TotemPole {
private TotemPole r;
public Snake(TotemPole rest){
r = rest;
}
public int power() {
return r.power()+3;
}
public int height() {
return r.height()+1;
}
public boolean chiefPole(int bearCount) {
return r.chiefPole(0);
}
}
Eagle.java
public class Eagle implements TotemPole{
public int power() {
return 2;
}
public int height() {
return 1;
}
public boolean chiefPole(int bearCount) {
return false;
}
}
Bear.java
public class Bear implements TotemPole {
private TotemPole r;
public Bear(TotemPole rest){
r = rest;
}
public int power() {
return r.power()+5;
}
public int height() {
return r.height()+1;
}
public boolean chiefPole(int bearCount) {
if(bearCount==2)
return true;
return r.chiefPole(bearCount+1);
}
}
output
Congrats, you pass. The Gashunongan people rejoice!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.