CSE 8A Lecture 10
• Reading for next class: 6.3
• PSA4 Interview: due tomorrow, don’t forget
• PSA5: Posted (get started)
• Today (random useful stuff):
– Variable scope
– Returning a value
– Static methods
– Comments
– If-statements
1. Two colors are considered "distant" if:
READING QUIZ – NO TALKING – CORRECTNESS MATTERS
A. If all three color components (red, green, blue)
are different
B. If two of the color components are different
C. If they are distant in a 3-dimensional RGB space
D. If any one component is distant
2. If I wanted the following lines to print “if statement was true” what could go in the blank to do this? int x = 30;
if(_______)
System.out.println(“if statement was true);
A. x>30 B. x<30 C. x<0 D. x>0
READING QUIZ – NO TALKING – CORRECTNESS MATTERS
3. The following method loops through each pixel in an image checking if the distance between the color in the current picture is less that 50 away from the color red (defined as red = 100, green = 0, blue = 0). If the distance between the current color and the defined red is less than 50, the red and blue color values of that pixel are set to 0. What needs to go in the blank to complete this method. public void turnRedIntoGreen()
{
Color red = new Color(100,0,0);
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
//loop through the pixels
for(int index = 0; index<pixelArray.length; index++)
{
pixel = pixelArray[index]
//if dist between current color and red is less than 50
if (____________________________)
pixel.setColor(new Color(0, pixel.getGreen(), 0));
}
}
A. pixel.colorDistance(red) < 50.0
B. pixel.getRed() < 50.0
C. pixel.getColor() < red READING QUIZ – NO TALKING – CORRECTNESS MATTERS
4. What gets printed in the following: int x = 12, r = 10;
if(x < 12)
{
x = 100;
}
else
{
r = r + 20;
}
System.out.prinln(x+r);
A. 34
B. 110
C. 42
D. 80
READING QUIZ – NO TALKING – CORRECTNESS MATTERS
Thank you for your feedback!
• Just a couple more questions…
How do you feel about the pace of the class?
A. Way too slow
B. A little too slow
C. Just about right
D. A little too fast
E. Way too fast
Thank you for your feedback!
• Just a couple more questions…
How do you feel about the difficulty of the PSAs?
A. Way too short/easy
B. A little too short/easy
C. Just about right
D. A little too long/hard
E. Way too long/hard
Thank you for your feedback!
• Just a couple more questions…
How many hours to do you spend outside of class per week?
A. 0-1:59:59
B. 2-3:59:59
C. 4-5:59:59
D. 6-8:59:59
E. 9 or more
Feedback on Feedback
• What you want to keep:
– CLICKER QUESTIONS/Group discsusion/Peer
instruction!
– Energy/enthusiasm
– Clearly explaining code (Box-and-arrow diagrams!)
– Posting slides online
Feedback on Feedback
• What you want to keep:
– CLICKER QUESTIONS/Group discussion/Peer
instruction!
– Energy/enthusiasm
– Clearly explaining code (Box-and-arrow diagrams!)
– Posting slides online
“The only thing that would make this class better is
if free donuts were handed out while people massaged our feet”
Feedback on Feedback
• What you want to quit (most feedback under
“start”):
– Pair programming
– Exams that lag behind current material
– Too much time for group discussion (but many say not
enough time…)
Feedback on Feedback
• What you want to start:
– Review session/practice problems
• We will try to provide these, but remember, you have “practice problems” already (and as you
advance in college you will be expected to generate your own)
– More office hours
• I’m always available by appointment
– Work with same partner more
• Change: You can work with the same partner up to 3 times
– Real-world applications /stuff not in book
• Sound good!
– Posting slides clean and with comments
• Will do!
– Running code/writing code in class
• Yup! (But writing without running is still an important skill)
– More challenges on PSAs
• Coming up!
– Make the classroom warmer (Sorry, bring a sweater)
Interm Exam 2
• WOW! Nice job!
– Median: 9.5
– High score: 10 (208 of these!)
• Exams provide a minimum bar sanity check
• If you scored lower than a 7, it’s time to REALLY
take action
– Come to the lab/office hours and get help
– Re-do all of the lab quizzes, clicker questions, reading
questions, etc.
Exam Problem 1 // for loop version
public void maxBlue()
{
Pixel p;
Pixel[] pixArray = this.getPixels();
for (int index = pixArray.length/2; index<pixArray.length; index++)
{
p = pixArray[index];
p.setBlue(255);
}
}
// while loop version
public void maxBlue()
{
Pixel p;
Pixel[] pixArray = this.getPixels();
int index = pixArray.length/2;
while (index < pixArrray.length )
{
p = pixArray[index];
p.setBlue(255);
index++;
}
}
Exam Problem 2
Pixel[] pixArr = this.getPixels();
int index = 0;
while (index < pixArr.length)
{
pixArr[index].setBlue(0);
___________________________________________;
index++;
}
Exam Problem 3 public void swapRedGreen()
{
Pixel[] pixArray = this.getPixels();
for (int index = 0; index<pixArray.length; index++)
{
Pixel p = pixArray[index];
p.setRed( p.getGreen() );
p.setGreen( p.getRed() );
}
}
WHOOPS!
red
green
p
Parameters and scope In Picture.java…
public void copyRegionTo (Picture target, int xSource,
int ySource, int xTarget, int yTarget )
{
// Body omitted to save space
}
In main…
Picture fish = new Picture( "fish.jpg" );
Picture blank = new Picture();
fish.copyRegionTo(blank, 10, 50, 30, 30);
Variables only exist in the region they are defined.
I.e. variables in main cannot be accessed in copyRegion and vice versa
The region where a variable exists is called its scope
Parameters and scope In Picture.java…
public void copyRegionTo (Picture target, int xSource,
int ySource, int xTarget, int yTarget )
{
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = target.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
}
In main… Picture fish = new Picture( "fish.jpg" );
Picture blank = new Picture();
fish.copyRegionTo(blank, 10, 50, 30, 30);
Main’s variables
fish
blank
public void copyRegionTo (Picture target, int xSource,
int ySource, int xTarget, int yTarget )
{
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = target.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
}
Parameters and scope In Picture.java…
In main… Picture fish = new Picture( "fish.jpg" );
Picture blank = new Picture();
fish.copyRegionTo(blank, 10, 50, 30, 30);
Main’s variables
fish
blank
copyRegionTo’s variables
target
this
xSource
ySource
xTarget
yTarget
Also sX, tX, sY, sY,
sPixel, tPixel
fish
blank
Parameters and scope In Picture.java…
In main… Picture fish = new Picture( "fish.jpg" );
Picture blank = new Picture();
fish.copyRegionTo(blank, 10, 50, 30, 30);
Main’s variables
fish
blank
public void copyRegionTo (Picture target, int xSource,
int ySource, int xTarget, int yTarget )
{
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = target.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
}
Parameters and return values In Picture.java… public Picture copyRegionToNew(int xSource,
int ySource, int xTarget, int yTarget )
{
Picture newCanvas = new Picture();
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = newCanvas.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
}
In main… Picture fish = new Picture( "fish.jpg" );
Picture newCanvas = fish.copyRegionToNew(10, 30, 50, 50);
newCanvas.show();
What error will the following code produce?
A. This code will not compile
B. The line “Picture newCanvas = fish.copyRegionToNew…” in main will cause an error
C. The line newCanvas.show() will cause an error
Parameters and return values In Picture.java… public Picture copyRegionToNew(int xSource,
int ySource, int xTarget, int yTarget )
{
Picture newCanvas = new Picture();
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = newCanvas.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
}
In main… Picture fish = new Picture( "fish.jpg" );
Picture newCanvas = fish.copyRegionToNew(10, 30, 50, 50);
newCanvas.show();
fish
newCanvas
Some of copyRegionTo’s
variables main’s variables
newCanvas
this
Some comments on comments /* A method to copy a 100x100 region of the calling object’s
* image to a blank canvas.
* xSource, ySource: the upper left corner of the
* region to be copied.
* xTarget, yTarget: the upper left corner where the region
* will appear in the new canvas.
* returns a new canvas with the region copied into it. */
public Picture copyRegionToNew(int xSource,
int ySource, int xTarget, int yTarget )
{
Picture newCanvas = new Picture();
Pixel sPixel, tPixel = null;
for (int sX = xSource, tX = xTarget; sX < 100+xSource; sX++, tX++)
{
for (int sY = ySource, tY = yTarget; sY < 100+ySource; sY++, tY++)
{
sPixel = this.getPixel(sX,sY);
tPixel = newCanvas.getPixel(tX,tY);
tPixel.setColor(sPixel.getColor());
}
}
return newCanvas;
}
Header (method) comments required.
Inline comments if necessary
Unraveling the magic of main
public static void main( String[] args )
Still a bit mysterious, for now
Doesn’t return anything
Parameters (how are these
passed in?)
Method name
Who “owns” the method…
Classes vs. Objects In the code below, how many classes are there? How many objects?
(And what does the memory model look like?)
Picture p = new Picture( “fish.jpg” );
Picture p2 = new Picture( p );
Picture p3 = p2;
A. 1 class, 2 objects
B. 1 class, 3 objects
C.2 classes, 2 objects
D.2 classes, 3 objects
E. 3 classes, 2 objects
Classes vs. Objects In the code below, how many classes are there? How many objects?
(And what does the memory model look like?)
Picture p = new Picture( “fish.jpg” );
Picture p2 = new Picture( p );
Picture p3 = p2;
The Picture class
Picture objects
new new
p
p3
p2 variables
variables
Static vs. nonstatic methods
Picture result =
Picture.collage(p1, p2, p3);
(nonstatic)
public class Picture{ ... public static Picture collage(Picture p1, Picture p2, Picture p3) public void filter1()
Picture p =
new picture( “fish.jpg”);
p.filter1();
Class - owned
Object - owned
static
the Picture p
collage
filter1
the Picture class
the Picture p1
the Picture p2
the returned
Picture
the Picture p3
Static vs. nonstatic methods
Picture result =
Picture.collage(p1, p2, p3);
(nonstatic)
public class Picture{ ... public static Picture collage(Picture p1, Picture p2, Picture p3) public void filter1()
Picture p =
new picture( “fish.jpg”);
p.filter1();
Class - owned
Object - owned
static
the Picture p
collage
filter1
the Picture class
the Picture p1
the Picture p2
the returned
Picture
the Picture p3
Static methods can be called directly on the class (can also be called on an object)
Non-static methods must be called on an object
Unraveling the magic of main
public static void main( String[] args )
Still a bit mysterious, for now
Doesn’t return anything
Parameters (how are these
passed in?)
Method name
Who “owns” the method…
Why does main have to be static? (Discuss with group)
Chapter 6: Conditionally modifying pixels
All pixels change if COLOR
meets criteria
All pixels change if meet both a
COLOR and COORDINATE criteria
All pixels change if COORDINATES
meet criteria
Select the if statement to make bottom half of picture some color
1) Solo: (30 sec)
2) Discuss: (1 min)
3) Group: (30 sec)
public void fillBottom(Color newColor)
{
Pixel pix;
for (int y = 0; y < this.getHeight(); y++)
{
for (int x = 0; x < this.getWidth(); x++)
{
<<<SELECT LINE OF CODE>>>>
{
pix = this.getPixel(x,y);
pix.setColor(newColor);
}
}
}
A) if(y<this.getHeight()/2)
C) if (this.getPixel(x,y) < this.getHeight()/2)
B) if(y>this.getHeight()/2)
D) if (this.getPixel(x,y) > this.getHeight()/2)
Does the order of the for loops matter?
1) Solo: (30 sec)
2) Discuss: (1 min)
3) Group: (30 sec)
public void fillBottom(Color newColor) {
Pixel pix;
for (int y = 0; y < this.getHeight(); y++)
{
for (int x = 0; x < this.getWidth(); x++)
{
<<<SELECT LINE OF CODE>>>>
{
pix = this.getPixel(x,y);
pix.setColor(newColor);
}
}
} A. Yes, since we are changing the bottom half, we have to “fill in” across the rows in the inner loop
B. Yes, because we need to make sure the if statement is checking y not x
C. No, the if statement controls the assignment
Real-world example of if-else rules (among other things)
Eamonn Keogh, UCR
ALARM!
Temp too high!
if (temp > 104.0)
Real-world example of if-else rules (among other things)
Eamonn Keogh, UCR
TRILLIONS of data points
(or more!)
How long would it take to loop through this data?
How could you find patterns?
Real-world example of if-else rules (among other things)
Eamonn Keogh, UCR
Usually, the sensors only display the last
few minutes of data and figures such as
the minimum and maximum temperature
for that day. In most cases, the rest of the
data is discarded.
This is in part due to legal and privacy
issues, which the researchers believe can
be solved. It’s also because computer
scientists didn’t have the tools to mine the
vast amounts of data produced in pediatric
intensive care units.
That changed after Keogh and a group of
researchers recently developed a new
technique, which allows for searching of
datasets with more than one trillion objects.
That’s a larger set than the combined size
of all datasets in all data mining papers
ever published.
http://ucrtoday.ucr.edu/9667
Real-world example of if-else rules (among other things)
Eamonn Keogh, UCR
Keogh plans to use the archived data to
develop algorithms that incorporate what
he calls “if then rules” that can assist
doctors. For example, if a heart beat looks
like this, then a child may have difficulty
breathing in five seconds.
http://ucrtoday.ucr.edu/9667
Summary of Concepts
• Variable scope
• Good commenting style
• If statements
• Efficiency
TODO
• Reading for next class: 6.3
• Start PSA5