Tuesday, September 28, 2010

Test 1 Solution

  1. (10%) The following program fails to compile. Why does it fail to compile?
    public class Broken {
    
      public static void main(String[] args) {
        for (int i = 0; i < 10; i++){
          int result = 0;
          result = result + 1;
        }
        System.out.println(result);
      }
    
    }
    
    Answer: The variable result is used outside its scope, by the println statement.
  2. (10%) The following program also fails to compile. Why does it fail to compile?
    import java.util.Scanner;
    
    public class BrokenA {
      static final double RATE = 0.5;
    
      public double nextYear(double x) {
        return x * RATE;
      }
    
      private static void main(String[] args) {
        int x = 0;
        String name = "Steve";
        Scanner keyboard = new Scanner(System.in);
        String value = keyboard.next();
        BrokenA  ba = new BrokenA();  
        double r = ba.nextYear(value);
        System.out.println(r);
      }
    }
    
    
    Answer:The method nextYear takes a double as an arugment but we are giving it a String.
  3. (30%) What does the following program print out:
    public class RPS {
    
      public static void main(String[] args) {
        String p1 = "PPPSSSRRRPPP"; //notice, only 3 letters used
        String p2 = "RSPPSRPSRPSP";
        for (int i=0; i < p1.length(); i++){
          char c1 = p1.charAt(i);
          char c2 = p2.charAt(i);
          System.out.print (c1 + " " + c2 + " ");
          if (c1 == c2) {
            System.out.println("=");
          }
          else switch (c1) {
          case 'R':
            System.out.println((c2 == 'S') ? 1 : 2);
            break;
          case 'P':
            System.out.println((c2 == 'S') ? 2 : 1);
            break;
          case 'S':
            System.out.println((c2 == 'P') ? 1 : 2);
            break;
          };
        }
      }
    }
    
    Answer:
    P R 1
    P S 2
    P P =
    S P 1
    S S =
    S R 2
    R P 2
    R S 1
    R R =
    P P =
    P S 2
    P P =
    

    Its the game of rock-paper-scissors (RPS).

  4. (50%) Write a program that:
    1. Asks the user for an integer. Assume he does enter an integer. If he enters 0 then the program stops.
    2. Otherwise, if it is a negative integer then the program converts to its positive counterpart (for example, -5 turns into 5).
    3. Otherwise, determine if there are two positive integers X and Y such that the number the user typed is equal to 3*X + 7*Y. If there are, print them, if not then print NONE FOUND. You will do this by brute-force: examine all realistic combinations of possible values of X and Y. (HINT: both of these will always be positive and smaller than...)
    Below is a sample interaction with the program. The users' inputs are in boldface:
    Enter number:3
    3= 3*1 + 7*0
    Enter number:-3
    3= 3*1 + 7*0
    Enter number:5
    NOT FOUND
    Enter number:7
    7= 3*0 + 7*1
    Enter number:11
    NOT FOUND
    Enter number:12
    12= 3*4 + 7*0
    Enter number:21
    21= 3*0 + 7*3
    Enter number:22
    22= 3*5 + 7*1
    Enter number:123456
    123456= 3*6 + 7*17634
    
    Answer:
    import java.util.Scanner;
    
    public class Legal {
    
      public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        while (true) {
          System.out.print("Enter number:");
          int number = keyboard.nextInt();
          if (number == 0) break;
          if (number < 0) number = number * -1;
          int x =0;
          int y =0;
          boolean found = false; //a flag
          for (x = 0; x < number; x++){ 
            for (y=0; y < number; y++){ //number is a bit high here, what should we use?
              if (number == 3*x + 7*y) {
                found = true;
                break;}
            }
            if (found) break;
          }
          if (found) {
            System.out.println(number + "= 3*" + x + " + 7*" + y);
          }
          else {
            System.out.println("NOT FOUND");
          };
        }
      }
    }
    

No comments: