CS100 M
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
Write a Java static method sum2dArray to perform an operation similar to sum(matrix') in MATLAB. Method sum2dArray takes as input argument (a reference to) a 2-d array of type double, sum up each "row" of the array, and return (the reference to) a 1-d array that contains all the row sums. The 2-d array may be ragged. Write a method call to use method sum2dArray.

Question 3
Part A:   Given a 2-d array x of floating point numbers, write a Java program fragment to re-order the rows such that the row sums are in ascending order. You may assume that all rows exist. You may write and call a method selectSort 5 to do the sorting.
Part B:   Given a 2-d array x of floating point numbers, write a Java program fragment to re-order the columns such that the column sums appear in ascending order. Recall that 2-d array x is in row-major order. You may assume here that x is a rectangular array.

Question 4
Review class Room and its subclass Bathroom discussed during Lecture 26 (see notes on 11/30). Assume that the client code is written in a class called Building in the main method. Answer the following questions:
  1. Which variables and methods are inherited from Room?
  2. Write an overloaded method majorCleanUp in class Bathroom that takes as the input argument the number of times to callmethod clean in class Room.
  3. Write a client class MyHouse that performs similar operations as shown in the client code given in Lecture 26. Instead of creating three Room objects with separate reference variables r1, r2, and r3, use an array of objects.
  4. For practice, design another subclass of Room. Then modify your client class MyHouse to use your new subclass.
If you haven't already played with classes Room and Bathroom as I suggested in lecture, do it now! Consider the client code given in lecture, first predict the output and then run the code to confirm your predictions. Furthermore, make changes to the code, predict the output, and run the code again to confirm.

Question 5
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 6
Write a Java program fragment to find the frequencies of all vowels in a given String (referenced by variable) str. Store the frequencies in a vector of length 5. For example, the string
  I loVE prOgramming
has the following vowel frequencies:
  a  e  i  o  u
  1  1  2  2  0

Question 7
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