Tuesday, October 26, 2010

Test 2

  1. (20%) Implement a method called determineWinner which takes two integer arrays as parameters. The arrays represent the number of points each team scored in a set of games that the teams played against each other. Your method should print out the name of the team that won the most games, or whether the tournament was a tie. Remember that if both teams score the same number of points in a game then that game is a tie (it is not a win for either team). Below is an example of how your method will be called:
    public class Tournament {
      
      public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
        int[] b = {8, 7, 6, 5, 4, 3, 2, 1};  //in game 0 a scored 1 and b scored 8 
        Tournament.determineWinner(a, b); //it was a tie
        a[0] = 10;
        Tournament.determineWinner(a, b); //a wins
        a[0] = 1;
        b[7] = 10;
        Tournament.determineWinner(a, b); //b wins
      }
    
    }
    
    Answer:
      public static void determineWinner (int [] a, int[] b) {
        int teamaLead =0; //positive is in a's favor, negative in b
        for (int i = 0; i < a.length; i++) { 
          if (a[i] > b[i]) {
            teamaLead++;
          }
          else if (a[i] < b[i]) {
            teamaLead--;
          }
        }
        if (teamaLead > 0) {
          System.out.println("Team A wins!");
        } 
        else if (teamaLead < 0) {
          System.out.println("Team B wins!");
        }
        else {
          System.out.println("It was a tie!");
        }
        
      }
    
  2. (10%) What is wrong with the following program?
    public interface Talkative {
      public void talk();
      
      public String toString() {
        return "";
      }
    }
    
    public class Bobby implements Talkative {
    
      public void talk() {
        System.out.println("Hello friend!");
      }
      
      public String toString() {
        return "My name is Bobby";
      }
    
    }
    
    Answer: An interface cannot implement a method; it can only declare it.
  3. (30%) What does the following program print out?
    public class A {
      private String name;
      
      public int age;
      
      protected double salary;
      
      public static int count = 0;
      
      public A() {
        age = 11;
        count++;
      }
      
      public A(String name, double salary) {
        this.name = name;
        this.salary = salary;
      }
      
      public String toString() {
        return name + " " + age + " " + salary;
      }
    
    }
    
    public class B extends A {
      
      public int score;
    
      public B(String name, double salary, int score) {
        super(name,salary);
        this.score = score;
      }
      
      public String toString(int version) {
        return version + ":" + super.toString() + " score=" + score; 
      }
      
      public static void main(String[] args) {
        A a = new A();
        System.out.println(a);
        A a2 = new A("a2", 10000);
        System.out.println(a2);
        B b = new B("b",20000,33);
        b.score = 22;
        System.out.println(b);
        B b2 = b;
        b2.score = 55;
        System.out.println(b2.toString(1));
        System.out.println(b.toString(2));
        System.out.println("count= " + b.count); 
      }
    }
    
    Answer:
    null 11 0.0
    a2 0 10000.0
    b 0 20000.0
    1:b 0 20000.0 score=55
    2:b 0 20000.0 score=55
    count= 1
    
  4. (40%) Implement a method called check which takes as a parameter a two-dimensional array of integers, you can assume the array is a square of size n, and returns true if the array contains every single integer in the set 1...n^2, false otherwise. Remember that a square array of size n has exactly n^2 elements. (Yes, this is like one of the Sudoku rules).
      /**
       * Returns true if the board contains all the integers from 1...board.length^2
       * Assumes board is a square array.
       * @param board
       * @return
       */
      public static boolean check (int[][] board) {
        boolean[] isIn = new boolean[board.length * board.length]; //all false by default
        for (int rowi = 0; rowi < board.length; rowi++) {
          for (int coli = 0; coli < board.length; coli++) {
            isIn[board[rowi][coli]-1] = true;
          }
        }
        //now return true if they are all true
        for (boolean val : isIn) {
          if (!val) return false;
        }
        return true;
      }
    

No comments: