CS 140 - Lab 7 Instructions (Harcourt)

Lets take a step back and review what we have covered so far in this course. Its possible that you will not finish this lab today. If you don't finish it today, finish it for Monday and get checked off on Monday.

Objectives

  1. Review Java programming constructs
  2. More practice writing methods in Java.

Method

Part I - The Structure of a Java Program

  1. Write a complete Java program named Lab7 that writes "Hello World" to the console. Try and do this from memory. If you get stuck go ahead a look in the book.
  2. Put a comment before your println statement that says
      // Part I 

    This marks the part of your main method that pertains to part I.

  3. Comment out the println statement and proceed to the next section. We do this so we don't see the "Hello World" message every time we run the program in the remainder of the lab. But it still leaves the code in there so you can refer back to it.

Part II - variables and Expressions

  1. Put a new comment line in your code that says // Part II
  2. Modify the main method to declare a variable average that will hold the average of a list of integers. Initialize average to 0.0.
  3. Declare three integer variables x, y, z to hold the values 97, 63, and 34.
  4. Now write an expression that puts the average of x, y, z into average.
  5. Use a println statement to print the message The average is ______ . The correct average should show up where the ______ is. Make sure the decimal part is correct.
  6. When this is working show your lab instructor.
  7. Comment out the code for this part and proceed to the next section. One easy way to comment out several consecutive lines of code is to highlight the code in the definitions pane and then choose Edit -> Comment Lines from the menu.

Part III - For Loops

  1. Add a comment // Part III
  2. What happens when you add the following for-loop to your main method?
        for (int i = 0; i < 10; ++i) {
          System.out.print(i + " ");        
        }
    			
  3. Modify the for-loop line to print the numbers 5 through 20, including 20.
  4. Modify the loop to add up the numbers from 5 through 20. Hint: declare an integer variable sum immediately before the for-loop. Make sure to initialize sum to 0. Now you need to keep adding i to sum inside the for loop (the stuff between the for-loop braces). Do this using the following line inside the for-loop.
    			   sum = sum + i;
    			

    The way to think about the statement sum = sum + i; is Take the old value of sum, add i to it, then stick the result back in the variable sum.

  5. Print the value in sum to the console. Print a nice message such as
    The answer is ___________
  6. Comment out the code for this part of the lab.

Part IV - Classes/Objects - Turtles

  1. Add a comment // Part IV
  2. Without looking in the book or previous labs declare a World object and put a Turtle on it. If you get stuck and can't remember go ahead and look, but at this point in the semester you should be able to recite from memory how to do this.
  3. In your main method use a for-loop to make your turtle go in a circle. Copy the for-loop from part III above and change the loop body to use a forward and turn method call.
  4. Comment out the code from part IV except for the declarations of the World and Turtle objects. Proceed to the next section.

Part V - Turtle Methods

  1. In the Turtle.java file add a method named circle that makes a turtle go in a circle.
  2. Make the turtle object you declared in part IV go in a circle by calling the circle method on a turtle object.
  3. Create a second turtle object at coordinates 70,70 on the world object and have it go in a circle by calling the circle method.
  4. What is the benefit of adding the circle method to the Turtle class as opposed to putting the for-loop directly in the main method? Write your answer below.
  5. Comment out all of the code for Part V and proceed to the next section. Your program should no longer show a world or turtle object.

Part VI - Classes/Objects - Pictures

  1. Add code to your main method that allows the user to choose a picture to display using a dialog box and then displays the selected picture. You'll need to declare a String object (to hold the file name) and a Picture object and then call the ___________ method to show it.
  2. Rather than let the user choose a file hard code the file name to
          "T:/Harcourt/Spring2008/CS140/gallery/yummy.jpg"
    			
  3. Create a new picture object that displays the picture above as black-and-white. Hint, call your grayScale method you built in the previous lab. It should still be in your Picture class.
  4. Comment out the picture objects you have so far so we don't keep on displaying them in the remainder of the lab.

Part VI - Picture methods

In previous labs you have added methods to your Picture class that modify images; methods such as grayScale and extractRedChannel. Review the grayScale method below.
   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));

    }

  }
  1. Write a complete sentence next to every line of code of the grayScale method above that briefly explains what that line does. Don't forget the first line that starts with the Java keyword public and the very last line that contains just the }
  2. Add a comment // Part V to your main method in Lab7.
  3. Create a picture object and display the picture at
          "T:/Harcourt/Spring2008/CS140/gallery/sunset.jpg"
    			
  4. This picture is very red. Add a method named decreaseRed to your Picture class that decreases the amount of red in a picture by 20%.
  5. Call the decreaseRed method on the sunset picture and display the new picture.

Extra Credit

Modify the method decreaseRed so that it takes a parameter which represents the scaling factor, the amount we need to decrease the red by. Use this parameter value instead of hard coding the value to decrease it by 20%. Modify the main program to pass in a scaling value when you call the decreaseRed method.