CS100 M
More Review/Practice Questions for Final Exam

Note:
Be sure to review the lecture examples and homework/exercise 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 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 the methods in class MatrixOps from the 5/4 lecture. In method flipLR the variable temp created for the swapping operations does not have to be a 1-d array--a scalar variable would work as well. Modify the method to use a scalar variable temp for swapping.

Question 5
Refer to classes Cubicle and CubicleWorld from the 5/4 lecture. Add a method showFloorPlan to show (print) the floor plan, including any internal space. See the diagram of the example floor plan in the lecture notes (just print the names, not the boxes around the names!). What parameters, if any, are necessary for the method? What should be the return type?

Question 6
Review class Room and its subclass Bathroom discussed in the 4/27 lecture. Review also class House. 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. In class House, what is the operator instanceOf used for? Why is it necessary to use a cast before the method call rooms[i].majorCleanUp()? Write more code in class House to access various Room and Bathroom methods.
  4. For practice, design another subclass of Room. Then add a new client class MyHouse to use your new subclass.

Question 7
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 8
Write a Java program fragment to count the number of occurences of all vowels in a given char array called (referenced by variable) text. Store the counts in an array counts of length 5 such that counts[i] is the number of occurences of the ith vowel. Do not convert the char array into a String and do not use any String methods. For example, the text
  I loVE prOgramming
has the following vowel counts:
  a  e  i  o  u
  1  1  2  2  0

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