class Data0 {
private int m;
public Data0 d0;
public Data0(int num) {
m = num;
}
public int get_val() {
return m+d0.m;
}
} //class Data0
class Data1 extends Data0 {
private int n;
public Data1(int x, int y) {
super(x);
n = y;
}
public int get_val() {
return n+super.get_val();
}
} //class Data1
public class Review {
public static void main(String[] args) {
Data0 dat0 = new Data0(0);
Data1 dat1 = new Data1(1,2);
dat0.d0 = new Data0(3);
dat1.d0 = new Data1(4,5);
dat1.d0.d0 = new Data0(6);
System.out.println(dat0.get_val()); //________
System.out.println(dat1.get_val()); //________
System.out.println(dat1.d0.get_val()); //________
}
} //class Review
class Creature {
protected String name; // Creature's name
// constructor: initialize name to n
public Creature(String n) { name = n; }
// speak, i.e. vocalize
protected void speak() { System.out.println(name + " speaks");}
// perform an action
public void act() { speak(); }
}
// create 4 Creatures and have each act 5 times
public class Zoo {
public static void main(String[] args) {
int times = 5; // number of times to act
Creature[] c = { // 4 Creatures
new Creature("Ginger"),
new Creature("Macavity"),
new Creature("Gus"),
new Creature("Bessy")
};
// repeat 5 times: each Creature acts
for (; times>0; times--)
for (int i = 0; i<c.length; i++)
c[i].act();
}
}
Define the following sub-classes and define or override methods as specified:
To get the most out of this question, first solve it by hand! Do not hack (on the computer) until you get a correct solution. Remember that you will write the final exam by hand! Example output is shown below:
Ginger woofs Macavity purrrs Gus purrrrrrrs Bessy woofs Ginger wags its tail for the 1-th time Macavity purrrs Gus purrrrrrrs Bessy moofs Ginger wags its tail for the 2-th time Macavity meows Gus meows Bessy woofs Ginger wags its tail for the 3-th time Macavity purrrs Gus purrrrrrrs Bessy moofs Ginger wags its tail for the 4-th time Macavity purrrs Gus meows Bessy wags its tail for the 1-th time