Tuesday, October 27, 2009

Test 2


Here is our second test along with the answers to each question:
  1. (20%) The following program

      public static void main (String[] args){
        //create animal with name "bunny" and weight of 10
        Animal bunny = new Animal("bunny", 10);
        Animal fox = new Animal();
        System.out.println(fox);
        System.out.println(bunny);
        bunny.feed(4);
        System.out.println(bunny);
        
      }
    
    prints out the following when run (every println in the code above corresponds to one line below):

    name: weight:-1
    name:bunny weight:10
    name:bunny weight:14
    
    Implement the missing Animal class so that it works as shown.



    Answer:
    public class Animal {
    
      private String name;
    
      private int weight;
    
      public Animal (){
        name = "";
        weight = -1;
      };
    
      public Animal (String n, int w){
        name = n;
        weight = w;
      }
      
      public String toString(){
        String result = "name:" + name + " weight:" + weight;
        return result;
      }
    
      public void feed(int amount){
        weight += amount;
      }
    
    }
    


  2. (20%) What does the following program print out when run?

    public class Counter {
    
      public static int alpha;
    
      public int beta;
    
      public Counter (){
        alpha = 5;
        beta = 10;
      }
    
      public void increase() {
        alpha = alpha + 1;
        beta = beta + 1;
      }
    
      public String toString() {
        return "alpha=" + alpha +"  beta=" + beta;
      }
    
      public static void main(String[] args){
        Counter x = new Counter();
        Counter y = new Counter();
        System.out.println(x);
        System.out.println(y);
        x.increase();
        System.out.println(x);
        System.out.println(y);
        Counter z = new Counter();
        System.out.println(z);
        System.out.println(x);
      }
    
    }
    
    Answer:
    alpha=5  beta=10
    alpha=5  beta=10
    alpha=6  beta=11
    alpha=6  beta=10
    alpha=5  beta=10
    alpha=5  beta=11
    
    


  3. (20%) In the game of chess the rook can capture other pieces that are in his same row or in his same column. Implement a function which, given a 2-dimensional array of integers and a row and column position, returns true if there are any non-zero numbers on the same row or on the same column. For example, the following program:

    public class Rook {
    
      public static void main(String[] args){
        int[][] board = new int[8][8];
        for (int i=0; i < 8; i++){
          int[] col = {0,0,0,0,0,0,0,0}; 
          board[i] = col;
        }
    
        board[1][5] = 1;
        
        //root has a kill if there is a non-zero number anywhere in row 1, or in column 2.
        if (rookCanKill(board,1,2))
          System.out.println("Root has kill at 1,2");
        else
          System.out.println("Root cannot kill at 1,2");
    
    
        //root has a kill if there is a non-zero number anywhere in row 2, or in column 2.
        if (rookCanKill(board,2,2))
          System.out.println("Root has kill at 2,2");
        else
          System.out.println("Root cannot kill at 2,2");
      }
    
    
    }
    
    prints out:

    Root has kill at 1,2
    Root cannot kill at 2,2
    
    Implement the public static boolean rookCanKill(int[][] board, int row, int col) function.



    Answer:
      public static boolean rookCanKill(int[][] board, int x, int y){
        for (int i=0; i < board.length; i++)
          if (board[x][i] != 0) return true;
        for (int i=0; i < board.length; i++)
          if (board[i][y] != 0) return true;
        return false;
      }
    
    


  4. (20%) What does the following program print out when we run its main?

    public class A {
    
      public A (){
        System.out.println("Creating A");
      }
    
      public void talk (){
        System.out.println("I am A!");
      }
    
      public void whine(){
        System.out.println("I am Always last.");
      }
    
      public void whine(int x){
        System.out.println("Am I late?");
      }
    
    }
    
    public class B extends A{
    
      public B (){
        System.out.println("Creating B");
      }
    
      public void talk (){
        System.out.println("I am B!");
      }
    
      public void whine(){
        System.out.println("I am Bored.");
      }
    
    }
    
    public class C extends B{
    
      public C (){
        System.out.println("Creating C");
      }
    
      public void talk (){
        System.out.println("I am C!");
      }
    
      public void whine(){
        System.out.println("I am Cursed.");
        super.whine();
      }
    
      public void whine(int x){
        System.out.println("I am Crazy.");
        whine();
      }
    
      public static void main(String[] args){
        C c = new C();
        c.talk();
        c.whine();
        B b = (B) c;
        b.talk();
        b = new B();
        b.whine();
      }
    
    }
    
    Answer:
    Creating A
    Creating B
    Creating C
    I am C!
    I am Cursed.
    I am Bored.
    I am C!
    Creating A
    Creating B
    I am Bored.
    


  5. (20%) Write some code which creates an array and populates it with the day of the month for all the days of a non-leap year. That is, given that the months have the following number of days:


    Month
    Days
    January
    31
    February
    28
    March
    31
    April
    30
    May
    31
    June
    30
    July
    31
    August
    31
    September
    30
    October
    31
    November
    30
    December
    31

    you will create an array dayNumber which has the number of the day of the month for each day of the year, as such:

    dayNumber[0] = 1
    dayNumber[1] = 2
              :
    dayNumber[30] = 31
    dayNumber[31] = 1  //February first
    dayNumber[32] = 2
              :
    dayNumber[58] = 28
    dayNumber[59] = 1
              :
              :
    dayNumber[364] = 31
    
    You will not receive credit for writing down 365 assignment statements, or for writing an array literal with each of the 365 values in it. You should use a for-loop. Hint: use an array literal to store the number of days in each month.

    Answer:
    public class Days{
    
      public static void main(String[] args){
        int[] dayNumber = new int[365];
        int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
        int day = 1;
        int month = 0;
        int i = 0;
        while (month < daysInMonth.length){
          dayNumber[i] = day;
          i++;
          day++;
          if (day > daysInMonth[month]){
            day = 1;
            month++;
          }
        }
        //print it out
        for (i=0;i<dayNumber.length;i++)
          System.out.println(i + " " + dayNumber[i]);
      }
    
    }
    


The grade distribution for this test is shown in the graph below.




No comments: