February 16 - 20, 2009

advertisement
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: Exam 2
 3/4: Go over Exam 2
 3/6: Exam 2 Makeup
 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
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.

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);
}
 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(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.
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
Boolean Expression
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.
true
Boolean Expression
loop body
Increment
Go back to the
boolean
expression to see
if we need to
execute the loop
again
false
This is the part of the code that is
executed over and over again.
Increment our
loop counter
(usually by 1) to
tell us that we
have executed
the loop one
more time.
Execute the rest of
the code in program
This is the code in
the program after
the loop is finished.
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
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.
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.
public void printHelloPictureWidthTimes() {
for(int count=1; count<=_pic.getWidth();
count++){
System.out.println("Hello");
}
}
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).
 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.
•
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++){
}
}
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.");
}
 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.
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.
}
}
}
Download