CS 140 - Test 1 Study Guide Answers

Make sure you can answer all of the questions from the first quiz we had. None of the questions below ask you to name types or classes or object methods or class methods, but you should be prepared to answer those.

Also review all of the six homeworks assigned to date.

I put all of the answers in blue. Please try and do all of the problems without looking at the answers. After you are done check your answers against mine. For the programming problems there are many different correct answers, I just give one.

  1. Write a complete Java program in a class named ExamOne that prints Hello World.
         public class ExamOne {
            public static void main(String [] args) {
               System.out.println("Hello World");
            }
         }
         
  2. Write some Java code that calculates and prints the number of minutes you have been alive up to yesterday at midnight. Don't do any calculations on the side, let Java do them all.
          int age = 43;
          int minutes_in_day = 24 * 60;
          int minutes_in_year = 365 * minutes_in_day;
                        
          // Birthday is Aug 19, 12 days left in August 
          // then add in all the days to today for Sep to Feb 21. 
          int days_since_last_birthday = 
              12 + 30 + 31 + 30 + 31 + 31 + 21;            
    
          int minutes_alive =  
              age * minutes_in_year + (days_since_last_birthday * minutes_in_day);
               
          System.out.println(minutes_alive);
    		

    To be really accurate I would have to include leap days.

  3. Consider the following Java variable declarations. Which are okay as is and which contain an error? Explain why.
         int count = 0;                Okay 
         int age;                      Okay 
         Double pi = 3.14159;          Wrong, double should not be capitalized
         String myname = "Poindexter;  Wrong, missing end quote 
         boolean the answer = true;    Wrong, spaces not allowed in variable name 
      
  4. Write a few lines of Java code that calculates and prints to the console the number of bytes needed to represent the picture in the file image.jpg.
          Picture p = new Picture("image.jpg");
          int num_bytes = p.getWidth() * p.getHeight() * 3;
          System.out.println(num_bytes);
    			
  5. Write a complete Java program that darkens a picture in the file image.jpg by 25%. Your program should have a main class that calls a method darken. Write the darken method as well and pretend that you put it in the file Picture.java.
         // Here is the method that goes in the Picture 
         // class in the file Picture.java next to all of the other Picture
         // methods you have developed.
         public void darken() {
            Pixel[] pixelArray = this.getPixels();
    
            for (Pixel pixelObj : pixelArray) {
              int r = (int) (pixelObj.getRed() * .75);    // reduce each color by 25%
              int g = (int) (pixelObj.getGreen() * .75);
              int b = (int) (pixelObj.getBlue() * .75);
              pixelObj.setColor(new Color(r,g,b));
           }
         }
    

    Here is the main program class

        public class ExamOne {
          public static void main(String [] args) {
            Picture p = new Picture("T:Harcourt/Spring2008/CS140/gallery/bug.jpg");
            p.darken();
            p.show();            
          }
        }
    
  6. Write a complete Java program that declares a turtle and a world object and uses a for-loop to make the turtle go in a circle. Do everything in the main method.
      public class CircleTurtle {
         public static void main(String [] args) {
           World w = new World();
           Turtle t = new Turtle(w);     
          
           // Have turtle go in circle
           for (int i = 0; i < 12; ++i) {
              t.forward(50);
              t.turn(30);
           }
         }
      }
    
  7. What does the following Java statement do?
       System.out.println("What do" + 1 + (2 + 3) + 4 + " I print?");
    

    Type this into DrJava and see for yourself.

  8. Write two Java nested for-loops that prints out the following pattern of stars.
     *
     **
     ***
     ****
     *****
    
     for (int i = 1; i <= 5; i++) {    // print 5 rows, why do we start i at 1?
       for (int j = 0; j < i; j++) {   // print i stars in a row 
          System.out.print("*");
       }
       System.out.println();           // print an end-of-line character after a row
     }
    
  9. Write a Java method grayScale (assume it goes in the Picture class) that converts an image to black-and-white by using the average of the red, green, and blue values.
      public void grayScale() {
         Pixel[] pixelArray = this.getPixels();
    
         for (Pixel pixelObj : pixelArray) {
           int r = pixelObj.getRed();
           int g = pixelObj.getGreen();
           int b = pixelObj.getBlue();
           int val = (r + g + b) / 3;
           pixelObj.setColor(new Color(val,val,val));
         }
      }
      
  10. What is printed by the following Java code.
      int x;
      x = 1/3;
      System.out.println(x);
      

    It prints a 0 because 1/3 uses integer division.

  11. Write a Java code segment that lets the user choose a file to open and then displays that file.
       public class ExamOne {
         public static void main(String [] args) {
           Picture p = new Picture(FileChooser.pickAFile());
           p.show();            
         }
       }
    
  12. Is FileChooser.pickAFile() a class method or an object method?

    It is a class method

    Be prepared to answer this question about many of the methods we have used this semester.

  13. Find all of the syntax errors in the code below. Write a brief explanation next to a line if there is an error that explains what the error is.

      public class Exam1  {                          missing }
      
        public static void main (String [] args) {   missing []  
         
          int XXXX = 7;
          System.out.println(XXXX);
          World w = new World();
          Turtle tommy = new Turtle(w);              missing parameter to Turtle constructor
          tommy.forward(200);                        missing ;
         
          int sum = 0;
          for (int i = 0; i < 10; i++) {             missing )
              sum = sum + i;                         "I" should be "i"
          }
          System.out.println(sum);
        }                                            missing }      
      }
    
  14. How do we get the height of a picture object? getHeight()
  15. How do we get the width of a picture object? getHeight()
  16. Assume we have a picture object pic already declared. Write a statement that sets the redness of the pixel at coordinate (40,70) to zero.
      pic.getPixel(40,70).setRed(0);
    
  17. Assume we have a picture object pic already declared. Write a Java statement that sets the color of the pixel at coordinate (40,70) to black.
      pic.getPixel(40,70).setColor(new Color(0,0,0));
    
  18. Assume we were to execute the following code segment. What would it print?
        int sum = 0;
        for (int i = 4; i < 9; i++) {
           sum = sum + i;
        }
        System.out.println(sum);
    

    It would print 30.

  19. Hard. Write a Java Picture method named copyRegion that given a picture object to modify and two sets of coordinates copies a region of the current picture from the original picture object. The first coordinate represents the upper left hand coordinate of the source picture and the second coordinate represents the lower right coordinate of the source picture.

    Here is how the method should be declared in the Picture class. The parameters ux and uy is the coordinate of the upper left part of the region. The parameters lx and ly is the lower right coordinate of the region to be copied.

          public void copyRegion(Picture p, 
                                 int ux, int uy,   // upper left coordinate
                                 int lx, int ly) { // lower right coordinate
          
             // your code goes here
    									
          }
       

    Here is an example of how the method should be used. The example below copies the just eye of the bug to the new picture dest.

         public class Exam1Hard {
           public static void main(String [] args) {
              Picture orig = 
                new Picture("T:/Harcourt/Spring2008/cs140/gallery/bug.jpg");
              Picture dest = new Picture(141,141);
              orig.copyRegion(dest,476,29,617,170);
              dest.show();
           }
         }
       

    Here is the copyRegion method that would go in the picture class.

        // copy a region of the current picture to 
        // to the picture p.
        public void copyRegion(Picture p, 
                               int ux, int uy,   // upper left coordinate 
                               int lx, int ly) { // lower right coordinate
    
          // keep track of destination x coordinate
          int destx = 0;  
          int desty = 0;
          
          // for each column i in the region
          for (int i = ux; i < lx; i++) {
            desty = 0;
    
            // for each row j in column i of the region 
            for (int j = uy; j < ly; j++) {
              Color c = getPixel(i,j).getColor();
              p.getPixel(destx,desty).setColor(c);
              desty++;
            }
            destx++;
          }
        }