CS 140 - Test 1 Study Guide

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.

  1. Write a complete Java program in a class named ExamOne that prints 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.
  3. Consider the following Java variable declarations. Which are okay as is and and which contain an error. Explain why.
         int count = 0;
         int age;
         Double pi = 3.14159;
         String myname = "Poindexter;
         boolean the answer = true;
      
  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.
  5. Write a complete Java program that darkens a picture in the file image.jpgby 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.
  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.
  7. What does the following Java statement do?
       System.out.println("What do" + 1 + (2 + 3) + 4 + " I print?");
    
  8. Write two Java nested for-loops that prints out the following pattern of stars.
     *
     **
     ***
     ****
     *****
    
  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.
  10. What is printed by the following Java code.
      int x;
      x = 1/3;
      System.out.println(x);
      
  11. Write a Java code segment that lets the user choose a file to open and then displays that file.
  12. Is FileChooser.pickAFile() a class method or an object 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 
      
        public static void main (String args) {
         
          int XXXX = 7;
          System.out.println(XXXX);
          World w = new World();
          Turtle tommy = new Turtle();
          tommy.forward(200)
         
          int sum = 0;
          for (int i = 0; i < 10; i++ {
              sum = sum + I;
          }
          System.out.println(sum);
          
      }
    
  14. How do we get the height of a picture object?
  15. How do we get the width of a picture object?
  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.
  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.
  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);
    
  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();
           }
         }