CS 140 - Quiz 2 Study Guide Answers

On Wednesday March 12 we're going to have a 30 minute quiz. Here are some study questions.
  1. Make sure that you can write methods to do all of the following operations on pictures: flipHorizontal, flipVertical, rotateLeft, rotateRight, shrink, and enlarge. Make sure that you can open a picture and call these from the main method.
      // Shrink a picture cutting in half the number of rows
      // and columns. This just takes every other pixel in 
      // every other row.
      public void shrink(Picture p) {
        for (int destX = 0; destX < p.getWidth(); destX++) {
          for (int destY = 0; destY < p.getHeight(); destY++) {
            Pixel src_pixel = getPixel(destX*2,destY*2);
            Pixel dest_pixel = p.getPixel(destX, destY);
            dest_pixel.setColor(src_pixel.getColor());
          }  
        }
      }
    
      // enlarge a picture by doubling the number of c9olumns and rows.
      public void enlarge(Picture p) {
    
        // for every pixel in the bigger (destination) picture
        for (int destX = 0; destX < p.getWidth(); destX++) {
          for (int destY = 0; destY < p.getHeight(); destY++) {
            Pixel src_pixel = getPixel(destX/2,destY/2);
            Pixel dest_pixel = p.getPixel(destX, destY);
            dest_pixel.setColor(src_pixel.getColor());
            
          }  
        }
      }  
      
      //   column i becomes row i
      //   row i becomes column width - i - 1
      public void rotateRight(Picture p) {
        for (int col = 0; col < getWidth(); col++) {
          for (int row = 0; row < getHeight(); row++) {
            Color c = getPixel(col,row).getColor();
            p.getPixel(p.getWidth() - row - 1, col).setColor(c);
          }
        }
      }
      
      //    column i becomes row height - i - 1
      //    row i becomes column i 
      public void rotateLeft(Picture dest) {
        for (int col = 0; col < getWidth(); col++) {    // col x
          for (int row = 0; row < getHeight(); row++) { // row y
            Color c = getPixel(col,row).getColor();
            dest.getPixel(row, dest.getHeight() - col - 1).setColor(c);
          }
        }
      }
      
       // switch pixel (x,y) with pixel (width - x - 1, y)
       public void flipHorizontal(Picture p) {
        for (int destX = 0; destX < p.getWidth(); destX++) {
          for (int destY = 0; destY < p.getHeight(); destY++) {
            Pixel src_pixel = getPixel(destX,destY);
            Pixel dest_pixel = p.getPixel(getWidth() - destX - 1, destY);
            dest_pixel.setColor(src_pixel.getColor());        
          }  
        }
      }      
    
      // switch pixel (x,y) with pixel (x, height - y - 1)
      public void flipVertical(Picture p) {
        for (int destX = 0; destX < p.getWidth(); destX++) {
          for (int destY = 0; destY < p.getHeight(); destY++) {
            Pixel src_pixel = getPixel(destX,destY);
            Pixel dest_pixel = p.getPixel(destX, getHeight() - destY - 1);
            dest_pixel.setColor(src_pixel.getColor());        
          }  
        }
      }  
    
  2. Write a Java code segment that prompts for and reads an integer from the user and prints yes if the number is divisible by 173 or not.
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter an number: ");
      int num = keyboard.nextInt();
        
      if (num % 173 == 0) {
         System.out.println("yes");
      }
    
  3. Write a Java code segment that prompts for and reads an integer from the user and prints yes if the number is between 70 and 100, and no if it is not.
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter an number: ");
      int num = keyboard.nextInt();
       
      if (num > 70 && num < 100) {
        System.out.println("yes");
      }
      else {
        System.out.println("no");
      }
    
  4. Write a Java code segment that prompts for and reads an integer x from the user and prints x asterisks on the console.
      Scanner keyboard = new Scanner(System.in);
      System.out.print("Enter an number: ");
      int num = keyboard.nextInt();
        
      for (int i = 0; i < num; i++) {
         System.out.print("*");
      }
    
  5. Write a Java code segment that determines the largest value of three integer variables, x, y, and z.
      if (x > y && x > z) {
        System.out.println("x is the largest");
      }
         
      if (y > x && y > z) {
        System.out.println("y is the largest");
      }
         
      if (z > x && z > y) {
        System.out.println("z is the largest");
      }
    
  6. Assume a, b, and c are integer variables. If there is something wrong with the statements below indicate what the problem is.
       a) if a > b {      missing parentheses around the conditional expression
             c = b;
          }
           
       b) if (a > b) {
             c = b        missing semicolon
          } 
          
       c) if (a > b {     missing ')' parentheses
             c = b;
             System.out.println("a is bigger than b");
          }
          else            missing else part {  } 
          
       d) if (a > b) { c = b; } okay, all on one line is fine but ugly
    
  7. The operator ! is logical negation. So !true is the same as false and !false is true. The precedence of &&, ||, and ! is that && has the highest precedence, then ||, and then !. As an example consider the expression below:
      int x = 5;
      int y = 7;
      System.out.println( x > y || !(3 > 4) && 5 <= 6 );
    

    The first condition checked is if x > y. This is false. Next we check if !(3 > 4). This is true, 3 is not greater than 4. Now we check if 5 is less than 6. This is true. Both sides of && are true so it evaluates to true.

    Assuming that x is 1 What is the output of the following:

        int x = 1;
        System.out.println((true) && (3 > 4));
        System.out.println(!(x > 0) && (x > 0));
        System.out.println((x > 0) || (x < 0));
        System.out.println(x != 0 || x == 0);
        System.out.println(x >= 0 || x < 0);
        System.out.println((x != 1) == !(x == 1));
    	

    Cut and paste these into DrJava and check the output

  8. What is the output of the following code? Try and figure it out by thinking it through. Verify your answer by cutting and pasting it into a program.
      int x = 3;
      int y = 2;
      int z = 0;
      
      if (x > 2) {
        if (y > 2) {
           z = x + y;
           System.out.println("z is " + z);
        }
      }
      else {
        System.out.println("x is " + x);
      }
    

    Cut and paste into DrJava and check the output

  9. In the above question what is the output if x is 3 and y is 4?

    Cut and paste into DrJava and check the output

  10. In the above question what is the output if x is 2 and y is 2?

    Cut and paste into DrJava and check the output

  11. Print the numbers from 1 to 100 that are divisible by 3 or 11.
      for (int i = 0; i <= 100; i++) {
        if (i % 7 == 0 || i % 11 == 0) {
           System.out.println(i);
        }
      }