Lecture

advertisement
Creative Commons Attribution
Non-Commercial Share Alike License
• http://creativecommons.org/licenses/by-ncsa/3.0/
• Original Developer: Beth Simon, 2009
bsimon@cs.ucsd.edu
• TODO:
CSE8A Lecture 6
– Get an interview today or tomorrow DON’T WAIT UNTIL LAST
MINUTE
• Tutor hours are listed on the web, sign up on board
• Log in and have your code open in Dr. Java
• Read next class: REVIEW up to 127, take the online reading quiz
(there’s NEW questions).
• NEW RESOURCE: http://wiki.ucsd.edu
– You need to post your PSA2 on it for a point (either A or B, but B is cooler)
– You can “grab” your picture to upload with screen capture. Name the file with
you and your partners last names
• Files with the same name “replace” each other.
• Follow your grades on moodle. It’s important to keep track of how
you are doing…
– Labs – Score 9 or 10
– Quizzes 7-10
– PSAs 18-20
– Midterms and Final average around 73-77%
CSES Meeting TONIGHT 6:30
CSE B225 (basement, other side of hall)
• Free pizza!
• Go, get involved!
– Bonfires, CSE Day, Technical Interviewing
Advice, etc.!
• What do YOU want to do in CSE?
The Jacobs School of Engineering at the University of California, San Diego
cordially invites you to attend the annual
Mark and Janet (‘82) Handzel
Transfer Student Reception
October 15th | 5:00 – 6:30PM
Powell-Focht Bioengineering Hall (PFBH)
Fung Auditorium, First Floor
Meet fellow engineering transfer students and learn how
to get involved in research, internship, and leadership opportunities.
Hors d’oeuvres served.
Please RSVP with full name and PID to ess@soe.ucsd.edu
Inquires: 858-534-6105
This event is sponsored through a generous gift from former transfer student and alumna Janet Handzel and
husband Mark.
By the end of today’s class you should
be able to…
• LG11: Use a for each loop in Java to loop over all pixels in a picture
and perform a transformation on those pixels (e.g. decrease their
red component).
• LG12: Apply Java syntax to cast a variable to another value (and
explain how it’s useful in setting picture colors).
• LG13: Read and understand a while loop for modifying a Picture,
including being able to draw a memory model of this code
• LG14: Describe the single array representation of pixels in
Picture.java
• LG16: Find bugs in code including: a common “empty loop” bug,
using the “how many times does this statement get executed”
analysis technique.
• LG17: Argue about the best location of a print statement to debug
a problem with a loop.
Clicker Quickie:
• In the decreaseRed method in the book
we see Pixel[] pixelArray = this.getPixels();
int value = 0;
for (Pixel pO: pixelArray)
{
value = pO.getRed();
value = (int) (value * .5);
pO.setRed(value);
}
What’s the (int)for?
1. Making sure value is set
to a whole (int) number
2. Doing a cast
3. Making sure value is set
to ½ of original red
4. Rounding the calculation
appropriately
Vote
Which are
right?
A
1,2,3 and 4
B
1 2 and 4
C
1 and 4
D
2 and 4
1) Solo: (60 sec)
2) Discuss: (2min)
3) Group: (30 sec)
How many times is
each set of code executed?
Pixel[] pixelArray = this.getPixels();
int value = 0;
Pixel p = null;
int index = 0;
while (index < pixelArray.length)
{
value = pixelArray[index].getRed();
value = (int) (value * 0.5);
pixelArray[index].setRed(value);
index = index + 1;
}
Pixel[] pixelArray = this.getPixels();
int value = 0;
Pixel p = null;
int index = 0;
while (index < pixelArray.length)
{
value = pixelArray[index].getRed();
value = (int) (value * 0.5);
pixelArray[index].setRed(value);
}
What is this
code doing?:
Two ways to
look at it.
(be able to do both)
1. Trace execution of the code by drawing memory to
represent what is happening to variables (and how
their values change)
2. Summarize, in a sentence that your grandmother
would understand the purpose of the code
1.
Asked as “what does this code do”?
First, we need to have an idea of what our variables “look like” in memory.
This involves understanding important things about the types in the above code
What is the type of the indicated
variable
A 1 Pixel
2 Pixel[]
B 1 Pixel[]
2 Pixel
C 1 Pixel[]
2 Color
D 1 Pixel
2 Color
E None of the
above
So what are the types on variables in a
_____________
A 1 Color
2 int
3 Color
B 1 Color
2 int
3 int
C 1 int
2 Color
3 int
D 1 int
2 Color
3 Color
E 1 Pixel
2 Color
3 int
Download