February 16 - 20, 2009 CSE 113 B
Announcements 2/16: Exam 1 Makeup Papers Available 2/20: Exam 2 Review Sheet Available in Lecture 2/27: Lab 2 due by 11:59:59pm 3/2: Exam 2 3/4: Go over Exam 2 3/6: Exam 2 Makeup
Methods for CSE113Picture class Method that takes in a pixel location and returns the color of that pixel. public java.awt.Color getColorAtPixel(int x, int y) { //Get the pixel at the location (x,y) Pixel pixel = _pic.getPixel(x,y); //Get the color from that pixel java.awt.Color color = pixel.getColor(); //return the color to the caller of the method return color; }
Comments Comments are text inserted into the program for humans to read. Comments are ignored by the compiler. Comments can be of two types: // starts a to-the-end-of-line comment /* is the start of a multi-line comment Which ends with */ DrJava colors comments green to differentiate them from the rest of the code.
Method for CSE113Picture class Write a method that takes in a pixel location and a color and sets the color of that pixel to the color passed in as the parameter. public void setColorAtPixel(int x, int y, java.awt.Color color) { //Get the pixel at the location (x,y) Pixel pixel = _pic.getPixel(x,y); //Set the color of that pixel pixel.setColor(color); }
Repetition If we want the computer to do something over and over for us, we need to use repetition. Different programming languages implement repetition in different ways. Most languages have loops as a way to implement repetition. Java has several different types of loops, one of them is the for-loop.
For-loop Syntax for(initialization; boolean expression; increment) { //loop body } The parts in black above are necessary syntax. The other parts are the names of the sections of the loop syntax. Let’s take a closer look at how a loop is executed in the machine and the different sections of the loop.
Code executed before the for keyword Loop Execution This the code that has been executed before we want to loop Code executed before the for keyword Then we initialize the loop counter – create a variable to help us keep track of how many times we’ve looped Initialization true false A boolean expression is one that evaluates to true or false. Depending on the evaluation, we will do two different things. This expression controls whether we keep looping or stop looping. Boolean Expression
Loop Execution - continued true false Boolean Expression This is the part of the code that is executed over and over again. loop body Increment our loop counter (usually by 1) to tell us that we have executed the loop one more time. Increment This is the code in the program after the loop is finished. Go back to the boolean expression to see if we need to execute the loop again Execute the rest of the code in program
Loop that prints out “Hello” five times for (int count = 1; count <= 5; count++) { System.out.println(“Hello”); } We typed this in at the interactions pane and saw the five “Hello”s on the screen. Remember that count++ is a shortcut way of writing count = count + 1
We can make a method to execute that loop public void printHello5Times() { for(int count = 1; count <= 5; count++) { System.out.println("Hello"); } Now we can call this method and see the Hellos on the screen.
How about letting the caller specify how many times to print hello? public void printHelloNTimes(int numberOfTimes) { for(int count = 1; count <= numberOfTimes; count++){ System.out.println("Hello"); } Notice the parameter that will tell us how many times Hello should be printed. When we call this method, we need to pass in the actual number of times to print Hello.
Print out hello the same number of times as the number of pixels in a picture’s width public void printHelloPictureWidthTimes() { for(int count=1; count<=_pic.getWidth(); count++){ System.out.println("Hello"); }
Print out numbers to count the width of the picture public void printPictureWidth() { for(int count=1; count<=_pic.getWidth(); count++){ System.out.println(count); } System.out.println(“The width of the picture is ” + _pic.getWidth()); The + in the last print statement is the string concatenation operator. It allows us to combine strings together (or in this case, a string and a number).
Looping through all pixels in a picture We can write similar methods for looping through the height of a picture. If we want to loop through all the pixels, then we need to combine the looping of the width and the height of a picture. Putting two loops together is called nesting of loops.
Nested loop that gets to every pixel in the picture Note that this is slightly different than what was talked about in lecture on Friday for(int column=0; column<_pic.getWidth(); column++){ for(int row=0; row<_pic.getHeight(); row++){ }
Method that prints out the color of every pixel in a picture public void printOutColorsOfPixels() { for(int column=0;column<_pic.getWidth();column++){ for(int row=0; row<_pic.getHeight(); row++) { Pixel pixel = _pic.getPixel(column, row); System.out.println(pixel.getColor()); } System.out.println("This picture is: " + _pic.getWidth()+" pixels wide "+" and " + _pic.getHeight()+" pixels tall.");
Working with Pictures We can use the basic format of the previous method any time we need to do something with the pixels of a picture. The only line that would need to change is the line after we get the pixel object. We use that line to do whatever we want to change the pixel.
Alternate loop for pictures public void sampleLoopForPictures() { for(int x = 0; x < _pic.getWidth(); x++){ for(int y = 0; y < _pic.getHeight(); y++) { Pixel pixel = _pic.getPixel(x, y); // Insert code for what you want to do // with pixels here. }