Pictures are an excellent example of the abstraction process that is critical to computer science: a complex object is broken down into tiny pieces (Pixels) that are easy to work with.
In this reading we’ll look at how pictures are encoded, learn about the Pixel class, and learn how to manipulate thousands or millions of pixels with only a few lines of code. To accomplish these tasks we’ll delve into two major topics: arrays and loops. The former allows us to group together items of the same type using a single name, while the latter will allow us to repeat the same action over and over.
We’ve learned a fair amount of Java’s syntax and we’ll be learning even more for working with arrays and loops. This syntax will seem unnatural, which can be frustrating. However, as you gain more experience working with Java, the syntax will become second nature. One simple way to speed up the process of gaining experience is to have DrJava open and follow along with the examples in the book.
Sections 4.1-4.3.1 (pp. 75-93) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
double
).The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
Textbook problem: 4.1 (excluding “Flowchart”, “Infinite Loop,” and “Variable Scope”).
Write a Java statement that creates a variable named myQuizGrades
, which refers to a new array of int
values that represents 3 quiz grades. You can use whatever values you want for these quiz grades.
Assume that you have a Picture variable named mySorority
. Write a Java expression that calculates the total number of pixels in the picture.
Write a Java statement that creates a Pixel variable name upperRightPixel
and makes it refer to the upper right Pixel in your mySorority
picture.
Write a Java statement that sets the color of your upper right pixel to a new Color object that is crimson. The (R, G, B) values for crimson are (220, 20, 60).
Repeat the last exercise but use the setRed
, setGreen
, and setBlue
methods to change the Pixel’s color instead of creating a Color object.
The following Java method is missing a line of code to make it loop through all the pixels in the array, printing their current blue value. What is this missing line?
public void printBlueVals()
{
Pixel[] thePixels = this.getPixels();
int val = 0;
// MISSING LINE OF CODE HERE
{
val = pixOb.getBlue();
System.out.println(val);
}
}