Top Banner
February 16 - 20, 2009
19

February 16 - 20, 2009. 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:

Dec 13, 2015

Download

Documents

Alberta Barker
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: February 16 - 20, 2009.  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:

February 16 - 20, 2009

Page 2: February 16 - 20, 2009.  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:

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

Page 3: February 16 - 20, 2009.  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:

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;}

Page 4: February 16 - 20, 2009.  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:

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.

Page 5: February 16 - 20, 2009.  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:

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 getColorAtPixel(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);}

Page 6: February 16 - 20, 2009.  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:

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.

Page 7: February 16 - 20, 2009.  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:

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.

Page 8: February 16 - 20, 2009.  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:

Code executed before the for

keyword

Initialization

Boolean Expression

true false

This the code that has been executed before we want to loop

Then we initialize the loop counter – create a variable to help us keep track of how many times we’ve looped

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.

Page 9: February 16 - 20, 2009.  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:

loop body

Increment

Boolean Expression

true false

This is the code in the program after

the loop is finished.

Increment our loop counter (usually by 1) to tell us that we have executed the loop one more time.

This is the part of the code that is executed over and over again.

Execute the rest of the code in

program

Go back to the boolean

expression to see if we need to execute the

loop again

Page 10: February 16 - 20, 2009.  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:

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

Page 11: February 16 - 20, 2009.  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:

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.

Page 12: February 16 - 20, 2009.  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:

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.

Page 13: February 16 - 20, 2009.  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:

public void printHelloPictureWidthTimes() {

for(int count=1; count<=_pic.getWidth();

count++){

System.out.println("Hello");

}

}

Page 14: February 16 - 20, 2009.  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:

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).

Page 15: February 16 - 20, 2009.  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:

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.

Page 16: February 16 - 20, 2009.  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:

• 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++){

}

}

Page 17: February 16 - 20, 2009.  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:

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.");

}

Page 18: February 16 - 20, 2009.  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:

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.

Page 19: February 16 - 20, 2009.  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:

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.

}

}

}