CS100 J Spring 2004
More Review/Practice Questions for Final Exam

Note:
Be sure to review the all the homework questions and their solutions as part of your preparation for the final exam. Review the three prelims and the prelim review questions.

Question 1
Solve Questions 2 and 3 from the "Applicable questions from old exam" file using Java.

Question 2
What is the output of the following program?
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

Question 3
Consider a class Creature for creatures that have a name, can speak, and can act. Class Zoo is a client of class Creature and has a main method that creates four Creatures and has each Creature act five times.
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:

  • Class Cat is a sub-class of Creature with members (variables and methods):
    • Integer field purrLength: the "purr length" of a Cat (see method purr below). Assume purr lengths are at least 2.
    • Method speak: "meow" instead of "speak", i.e. print the name of the Cat and then print "meows". E.g. Macavity meows.
    • Method purr: "purr" with a specified number of "r"s. E.g. Gus purrrrrs has purrLength 5.
    • Method act: randomly choose between the original action (act) of a Creature and the new action purr.
  • Class Dog is a sub-class of Creature with members:
    • Integer field wags: how many times the dog has wagged its tail.
    • Method speak: "woof" instead of "speak".
    • Method wag: "wag" and print how many times the dog has wagged its tail. E.g., Ginger wags its tail for the 1-th time.
    • Method act: randomly choose between the original action act of a Creature and the new action wag.
  • Class DogCow is a sub-class of Dog:
    • Every DogCow is named "Bessy". (Do not use a class variable here, instead handle this in the constructor.)
    • Method speak: randomly choose between how a dog (normally) speaks and saying "moof".
  • Modify method main to make a Dog "Ginger", a Cat "Macavity" with purr length 3, a Cat "Gus" with purr length 7, and a DogCow "Bessy". The method should still have each Creature act 5 times.
  • Use good style and use super appropriately! You should use super to invoke the constructor of super-class and, as much as possible, invoke the methods of super-class.

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