Download presentation
Presentation is loading. Please wait.
1
Section 15.1 Introduction
2
Array Example [16] Ingrid [17] Darlene [18] Gene [19] Sean [20]
Stephanie [11] Holly [12] Blake [13] Michelle [14] Remy [15] Haley [06] Diana [07] Jessica [08] David [09] Anthony [10] Alec [01] Isolde [02] John [03] Greg [04] Maria [05] Heidi
3
2-D Array Example [04][01] Ingrid [04][02] Darlene [04][03] Gene
[04][04] Sean [04][05] Stephanie [03][01] Holly [03][02] Blake [03][03] Michelle [03][04] Remy [03][05] Haley [02][01] Diana [02][02] Jessica [02][03] David [02][04] Anthony [02][05] Alec [01][01] Isolde [01][02] John [01][03] Greg [01][04] Maria [01][05] Heidi
4
Section 15.2 Declaring a Static 2D Array
5
// Java1501. java // This program introduces 2D Java static arrays
// Java1501.java // This program introduces 2D Java static arrays. // For 2D arrays two sets of index operators are needed. // The first set of index brackets stores the rows value. // The second set of index operators stores the cols value. public class Java1501 { public static void main(String[ ] args) { int twoD[ ][ ]; // declaration of two-dimensional integer array twoD = new int[2][3]; // new 2D array is constructed with 2 rows and 3 cols twoD[0][0] = 1; twoD[0][1] = 2; twoD[0][2] = 3; twoD[1][0] = 4; twoD[1][1] = 5; twoD[1][2] = 6; System.out.print(twoD[0][0] + " "); System.out.print(twoD[0][1] + " "); System.out.print(twoD[0][2] + " "); System.out.println(); System.out.print(twoD[1][0] + " "); System.out.print(twoD[1][1] + " "); System.out.print(twoD[1][2] + " "); } }
6
// Java1502.java // A set of nested loops is used with 2D arrays to assign and display individual values. // Additionally, the declaration of the 2D array is done in one statement. public class Java1502 { public static void main(String[ ] args) { int twoD[ ][ ] = new int[2][3]; // 2D array declaration in one statement int count = 1; for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { twoD[row][col] = count; count++; } } for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { System.out.print(twoD[row][col] + " "); } System.out.println(); } } }
7
This also shows that a 2D array
// Java1503.java // This program demonstrates how to use an initializer list. // Commented lines 15 and 16 show a matrix style. public class Java1503 { public static void main(String[ ] args) { int twoD[ ][ ] = { {1,2,3}, {4,5,6} }; // int twoD[ ][ ] = { {1,2,3}, // {4,5,6} }; for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { System.out.print(twoD[row][col] + " "); } System.out.println(); } } } To the computer, these are both identical. This also shows that a 2D array is an “array of arrays”.
8
int matrix[ ][ ] = new int[7][5]; // 7 rows and 5 columns
// Java1504.java // This program demonstrates what happens when rows and columns are confused. // A matrix of 7 rows and 5 columns is created. // The program attempts to display 5 rows and 7 columns. public class Java1504 { public static void main(String[ ] args) { int k = 1; int matrix[ ][ ] = new int[7][5]; // 7 rows and 5 columns for (int r = 0; r < 7; r++) for (int c = 0; c < 5; c++) { matrix[r][c] = k; k++; } System.out.println(); for (int r = 0; r < 5; r++) // should be 7 for (int c = 0; c < 7; c++) // should be 5 System.out.print(matrix[r][c] + " ");
9
// Java1505.java // This program allows the user to specify the number of rows and columns. // Note that the output will not line up nicely as it combines single, double and triple digit numbers. import java.util.Scanner; // necessary to use the <Scanner> class public class Java1505 { public static void main(String args[]) Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows > "); int numRows = input.nextInt(); System.out.print("Enter the number of columns --> "); int numCols = input.nextInt(); System.out.println("\n"); int k = 1; int matrix[][] = new int[numRows][numCols]; for (int r = 0; r < numRows; r++) for (int c = 0; c < numCols; c++) matrix[r][c] = k; k++; } System.out.println(); System.out.print(matrix[r][c] + " ");
11
Section 15.3 Two-Dimensional Array Output
12
// Java1506.java // This program demonstrates the <DecimalFormat> class. // By using this we can make output line up properly. import java.text.DecimalFormat; // necessary to use the <DecimalFormat> class import java.util.Scanner; // necessary to use the <Scanner> class public class Java1506 { public static void main(String args[]) DecimalFormat threeDigits = new DecimalFormat("000"); Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows > "); int numRows = input.nextInt(); System.out.print("Enter the number of columns --> "); int numCols = input.nextInt(); System.out.println("\n"); int k = 1; int matrix[][] = new int[numRows][numCols]; // 7 rows and 5 columns for (int r = 0; r < numRows; r++) for (int c = 0; c < numCols; c++) matrix[r][c] = k; k++; } System.out.println(); System.out.print(threeDigits.format(matrix[r][c]) + " ");
14
// Java1507. java // This program uses the <for
// Java1507.java // This program uses the <for..each> loop structure to display the matrix. // In this case it is not necessary to use the height and width of the array. public class Java1507 { public static void main(String[ ] args) { int[ ][ ] mat = { {1,2,3,4}, {5,6,7,8} }; displayMatrix(mat); } public static void displayMatrix(int[ ][ ] matrix) { for (int[ ] row : matrix) { for (int number : row) System.out.print(number + " "); System.out.println(); } } }
15
Close-up View of using for...each in the Outer Loop
For each array element row in matrix, which is an int array, do the following ... for (int[ ] row: matrix) { for (int number: row) System.out.print(threeDigits.format(number) + " "); System.out.println(); }
16
Close-up View of using for...each in the Inner Loop
For each array element number in row, which is an int, display the number. for (int[ ] row: matrix) { for (int number: row) System.out.print(threeDigits.format(number) + " "); System.out.println(); }
17
for...each Loop Structure Limitations
The for..each loop structure is read only for any type of data structure. This is true for the one-dimensional array as well as the two-dimensional array. Furthermore, the for..each loop will access every element of the array and cannot be limited to smaller section.
18
Section 15.4 2D Arrays and the length Field
19
// The <length> field is used for both row and column length.
// Java1508.java // This program creates a 3 X 3 2D array and uses a method to display the array elements. // The <length> field is used for both row and column length. public class Java1508 { public static void main(String args[]) int[][] mat = {{1,2,3}, {4,5,6}, {7,8,9}}; displayMatrix(mat); } public static void displayMatrix(int[][] m) for (int r = 0; r < m.length; r++) for (int c = 0; c < m.length; c++) System.out.print(m[r][c] + " "); System.out.println();
20
// Java1509.java // The same <displayMatrix> method is used to display a // 2 X 4 2D array. This time the method does not display correctly. public class Java1509 { public static void main(String args[]) int[][] mat = { {1,2,3,4}, {5,6,7,8} }; displayMatrix(mat); } public static void displayMatrix(int[][] m) for (int r = 0; r < m.length; r++) for (int c = 0; c < m.length; c++) System.out.print(m[r][c] + " "); System.out.println();
21
2D Array Reality A two-dimensional array is actually a one-dimensional array of one-dimensional array elements.
22
// Java1510.java // A very slight change with the column length results in the correct array display. public class Java1510 { public static void main(String args[]) int[][] mat = { {1,2,3,4}, {5,6,7,8} }; displayMatrix(mat); } public static void displayMatrix(int[][] m) for (int r = 0; r < m.length; r++) for (int c = 0; c < m[0].length; c++) System.out.print(m[r][c] + " "); System.out.println();
23
The length Field for 2D Non-Ragged Arrays
Consider the following statement: int matrix[][] = new int[5][4]; The value of matrix.length is 5. This is the number of rows. The value of matrix[0].length is 4. This is the number of columns. The values of matrix[1].length, matrix[2].length, matrix[3].length and matrix[4].length are also 4.
24
Ragged Array Example [06][01] Ingrid [06][02] Darlene [06][03] Gene
[06][04] Sean [06][05] Stephanie [05][01] Anthony [05][02] Alec [05][03] Haley [04][01] Holly [04][02] Blake [04][03] Michelle [04][04] Remy [03][01] John [03][02] Greg [03][03] Heidi [03][04] Maria [03][05] David [02][01] Diana [02][02] Jessica [01][01] Isolde NOTE: This is possible because a 2D array is essentially a 1D array of 1D arrays, and each 1D array can be a different size.
25
// Java1511.java // This program demonstrates how to construct an irregular two-dimensional array // in the shape of a triangle, using a "ragged" array. // It also shows how to use length for different column sizes. public class Java1511 { public static void main(String args[]) int[][] mat = { {1}, {1,2}, {1,2,3}, {1,2,3,4}, {1,2,3,4,5} }; displayMatrix(mat); } public static void displayMatrix(int[][] m) for (int r = 0; r < m.length; r++) for (int c = 0; c < m[r].length; c++) System.out.print(m[r][c] + " "); System.out.println();
26
AP Exam Alert Two-dimensional ragged arrays, as the shown in the example below are not tested on the APCS Examination. int[][] mat = {{1}, {1,2}, {1,2,3}, {1,2,3,4}, {1,2,3,4,5}};
27
// Java1512.java // In this program we explorer the "array of array" concept further. // Notice how the "2D Array" uses a single set of [ ] index operators. public class Java1512 { public static void main(String[ ] args) { int[ ][ ] mat = { {1,2,3}, {4,5,6}, {7,8,9} }; System.out.println("mat: " + mat); System.out.println("mat[0]: " + mat[0]); System.out.println("mat[1]: " + mat[1]); System.out.println("mat[2]: " + mat[2]); } }
28
// Java1513.java // In this program 1D arrays are created first and then used to create a 2D array. public class Java1513 { public static void main(String[ ] args) { int[ ] list1 = {1,2,3}; int[ ] list2 = {4,5,6}; int[ ] list3 = {7,8,9}; int[ ][ ] mat = new int[3][3]; mat[0] = list1; mat[1] = list2; mat[2] = list3; displayMatrix(mat); } public static void displayMatrix(int[ ][ ] m) { for (int r = 0; r < m.length; r++) { for (int c = 0; c < m.length; c++) System.out.print(m[r][c] + " "); System.out.println(); } } }
29
Section 15.5 Two-Dimensional Dynamic Arrays
30
// Java1514.java // This program demonstrates how to declare a two-dimensional dynamic array. // It also demonstrates two different output approaches. import java.util.ArrayList; public class Java1514 { public static void main (String args[ ]) { ArrayList<String> cats = new ArrayList<String>(); cats.add("Lions"); cats.add("Tigers"); ArrayList<String> swimmers = new ArrayList<String>(); swimmers.add("Whales"); swimmers.add("Dolphins"); ArrayList<String> primates = new ArrayList<String>(); primates.add("Gorillas"); primates.add("Chimpanzees"); ArrayList<ArrayList<String>> mammals = new ArrayList<ArrayList<String>>(); mammals.add(cats); mammals.add(swimmers); mammals.add(primates); System.out.println(mammals); System.out.println(); for (ArrayList<String> mammal: mammals) { for (String animal: mammal) System.out.println(animal); System.out.println(); } } }
32
// Java1515.java // This program example demonstrates how to use the original <for> // loop structure to display dynamic two-dimensional arrays. import java.util.ArrayList; public class Java1515 { public static void main (String args[ ]) { ArrayList<String> cats = new ArrayList<String>(); cats.add("Lions"); cats.add("Tigers"); ArrayList<String> swimmers = new ArrayList<String>(); swimmers.add("Whales"); swimmers.add("Dolphins"); ArrayList<String> primates = new ArrayList<String>(); primates.add("Gorillas"); primates.add("Chimpanzees"); ArrayList<ArrayList<String>> mammals = new ArrayList<ArrayList<String>>(); mammals.add(cats); mammals.add(swimmers); mammals.add(primates); for (int row = 0; row < mammals.size(); row++) { for (int col = 0; col < mammals.get(row).size(); col++) System.out.println(mammals.get(row).get(col)); System.out.println(); } } }
33
2D Arrays: Dynamic vs Static
for (int row = 0; row < mammals.size(); row++) { for (int col = 0; col < mammals.get(row).size(); col++) System.out.println(mammals.get(row).get(col)); System.out.println(); } Dynamic for (int r = 0; r < m.length; r++) { for (int c = 0; c < m[0].length; c++) System.out.print(m[r] [c] + " "); System.out.println(); } Static
34
Section 15.6 Introduction to Number Systems
35
Calculator Warning The AP® Computer Science Examination does not allow the use of calculators. It is vitally important that you learn to do all computations on paper.
36
Section Counting in Other Number Systems
37
Counting In Other Number Systems
Counting is something that you will likely take for granted, at least counting in base-10. In base-10 there are ten different single digits from 0 to 9. Counting in base-10 requires cycling through these ten digits. Every time 9 is reached the counting starts over at 0, and at the same time the digit in the next column is incremented by one. However, consider the fact that there are 60 seconds in a minute, 60 minutes in an hour. How about 12 inches to a foot, 3 feet to a yard, and 1,760 yards to a mile. Not everything is done in base-10.
38
The Odometer Analogy The odometer is the part of the car that tells you how many miles you have driven. Example: Since each place holder can hold 10 possible digits (0-9), we can say that this odometer is counting in base-10. If we continued to let it count, we would see the following: 3 1 4 2 6 3 1 4 2 6 7 3 1 4 2 7 3 1 4 2 6 8 3 1 4 2 7 3 1 4 2 6 9 3 1 4 2 7 3 1 4 2 7 3 1 4 2 7 5 3 1 4 2 7 3 1 4 2 7 6
39
The Broken Odometer Now suppose somebody decides to remove all of the 8s and 9s from your car’s odometer. Since each place holder can hold 8 possible digits (0-7), we can say that this odometer is counting in base-8. If we continued to let it count, we would see the following: 3 1 4 2 6 3 1 4 2 6 7 3 1 4 2 7 3 1 4 2 7 3 1 4 2 7 5 3 1 4 2 7 3 1 4 2 7 6 3 1 4 2 7 3 1 4 2 7 3 1 4 2 7 3 1 4
40
Counting Rules for Numbers of All Bases
There are as many single digits as the base value. The largest single digit is 1 less than the base value. You could say that in base N the range of single digits is from 0 to N-1. Base 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base , 1, 2, 3, 4, 5, 6, 7, 8 Base , 1, 2, 3, 4, 5, 6, 7 Base , 1, 2, 3, 4, 5, 6 Base , 1, 2, 3, 4, 5 Base , 1, 2, 3, 4 Base , 1, 2, 3 Base , 1, 2 Base , 1
43
List the next 10 base-8 numbers after 321
List the next 10 base-8 numbers after 406 List the next 10 base-4 numbers after 121 List the next 10 base-3 numbers after 11 List the next 10 base-2 numbers after 101
44
Section Counting in Base-16
45
Counting In Base-16 Counting in base-16 is significant in computer science. You will see that computer memory addresses are displayed in base-16 or hexadecimal. A computer’s NIC (Network Interface Card) has a special address which is a base-16 number. Future IP addresses will use base-16 as well. There is a unique relationship between base-2 and base-16 that we will look at later. This relationship is the reason base-16 is used in Assembly Language. Right now you need to learn how to count in base-16.
46
The Really Messed Up Odometer
Now suppose somebody decides to add 6 digits (A-F) to your car’s odometer. Since each place holder can hold 16 possible digits (0-F), we can say that this odometer is counting in base-16. If we continued to let it count, we would see the following: 3 A C 2 6 3 A C 2 6 7 3 A C 2 6 3 A C 2 6 8 3 A C 2 6 D 3 A C 2 6 9 3 A C 2 6 E 3 A C 2 6 3 A C 2 6 F 3 A C 2 6 B 3 A C 2 7
47
Counting In Base-16 (continued)
48
List the next 10 base-16 numbers after 100
a 6b 6c 6d 6e List the next 10 base-16 numbers after 1001 a 100b List the next 10 base-16 numbers after abc abd abe abf ac0 ac1 ac2 ac3 ac4 ac5 ac6 List the next 10 base-16 numbers after 999 99a 99b 99c 99d 99e 99f 9a0 9a1 9a2 9a3
49
Section Converting to Base-10
50
Flashback to Elementary School
Do you remember learning about the One’s Column, Ten’s Column, Hundred’s Column’s, Thousand’s column, etc. back in elementary school. Understanding how Base-10 works is essential to understanding how other bases work. Consider the representation of the number 1,234,567 below: 1,000,000 100,000 10,000 1,000 100 10 1 2 3 4 5 6 7 200,000 30,000 4,000 500 60 1,000, , , , = 1,234,567
51
Scientific Notation Flashback
Do you remember learning about powers of 10, exponents, and Scientific Notation. This is also essential. Consider the representation of the number 1,234,567 below: 1,000,000 100,000 10,000 1,000 100 10 1 106 105 104 103 102 101 2 3 4 5 6 7 1 * 106 2 * 105 3 * 104 4 * 103 5 * 102 6 * 101 7 * 100 200,000 30,000 4,000 500 60 1* * * * * * *100 = 1,234,567 In base-10, every column is a power of 10.
52
Applying the Rule to Other Bases
Let us see what happens with a base-5 number. Consider the representation of the number below: 625 125 25 5 1 54 53 52 51 50 3 2 4 1 * 54 3 * 53 2 * 52 0 * 51 4 * 50 375 13204 base-5 = 1*54 + 3*53 + 2*52 + 0*51 + 4*50 = = 1,054 base-10 In base-5, every column is a power of 5.
53
Other Examples 3214 base-5 = 3 x 53 + 2 x 52 + 1 x 51 + 4 x 50
70416 base-8 = 7 x x x x x 80 5A9D base-16 = 5 x A x x D x 160 3204 base-4 = Incorrect base-4 number You should notice that once the first step is done – the expansion step – all that remains is simple arithmetic. Base-16 is a special case and requires an extra step, which will be shown on the next slide. 3204 base-4 is intentionally not expanded because it is not a valid number. Digit 4 is not possible in base-4. The only 4 digits allowed in base-4 are 0, 1, 2 & 3.
54
Converting Base-16 to Base-10
1FA base-16 = ??? base-10 1 * F * A * 160 = 1 * * * 160 = 1 * * * 1 = = 506 base-10 The Extra Step After the initial expansion, use this chart and convert all Base-16 letters to their Base-10 values. Hex Dec A 10 B 11 C 12 D 13 E 14 F 15
55
54321 base-Q = 5 x Q4 + 4 x Q3 + 3 x Q2 + 2 x Q1 + 1 x Q0
If number expansion makes sense to you, you can then take any number in any base and determine place holder values. You can also create a general method for expansion for a number with base-Q. 54321 base-Q = 5 x Q x Q x Q x Q x Q0
56
Section Converting from Base-2 to Base-10
57
Converting Base-2 to Base-10
Base 2 is another special case that has a simpler solution or numbers between 0 and 255 – which are the numbers currently used in IP Addresses. Binary (base-2) numbers are frequently written as a group of 8 bits. The 1st 8 powers of 2 are shown below: To convert the binary number to decimal look for the 1s. Add there place values and ignore the 0s. base-2 = = 85 base-10 128 64 32 16 8 4 2 1
58
Another way to look at it
Suppose you have the binary number One way to do this is by adding place values: base-2 = = 254 base-10 If you realize that binary = decimal 255 you might find a shorter way to do this by subtracting place values of the 0s from 255. base-2 = = 254 base-10 128 64 32 16 8 4 2 1
59
Section Converting from Base-10 to Any Base
60
The Division Method There are several different ways to convert a base-10 number to another base. One way involves dividing the base-10 number by the desired base value again and again until you get a quotient of 0. If you then take all of the remainders in reverse order, you will have your answer. This method works for any base. The example on the next slide demonstrates converting to base-2 for simplicity. Other examples will follow.
61
Converting Decimal to Binary
Convert to binary: 201 / = remainder 1 100 / = remainder 0 50 / = remainder 0 25 / = remainder 1 12 / = remainder 0 6 / = remainder 0 3 / = remainder 1 1 / = remainder 1 When the quotient is 0, take all the remainders in reverse order for your answer: =
62
Converting Decimal to Base-5
Convert to Base 5: / = remainder 1 200 / = remainder 0 40 / = remainder 0 8 / = remainder 3 1 / = remainder 1 When the quotient is 0, take all the remainders in reverse order for your answer: =
63
Converting Decimal to Octal
Convert to Base 8: / = remainder 6 293 / = remainder 5 36 / = remainder 4 4 / = remainder 4 When the quotient is 0, take all the remainders in reverse order for your answer: = 44568
64
Converting Decimal to Hexadecimal
Convert to hexadecimal: 31 r 10 16 )506 48 26 16 10 1 r 15 16 )31 15 0 r 1 16 )1 1 Using the division method, when we take the remainders in reverse order we get 1(15)(10). We need to do one extra step to make this right.
65
Converting Decimal to Hexadecimal Extra Step
Convert to hexadecimal: 31 r 10 = A 16 )506 48 26 16 10 1 r 15 = F 16 )31 15 0 r 1 16 )1 1 Hex Dec A 10 B 11 C 12 D 13 E 14 F 15 When the 10 and 15 are converted to A and F, we can now properly find the answer of 1FA.
66
Section 15.7 The AP® Picture Labs
67
The AP® Picture Lab The Picture AP® Labs are different from the other 2 AP® Labs in a variety of ways. Unlike the Magpie Chatbot Lab or the Elevens Lab, the Picture AP® Labs are more than one lab, are completely contained within this chapter, are presented in complete form from the start , (There are no simple/initial versions.) will NOT have its code explained in great detail. There is a great deal of information hiding used in the Picture Labs. You will use the provided methods even if you do not understand their implementation. The reason these labs are in the 2D Array chapter is that a picture is a 2D array of colored pixels.
68
Exposure Java and the AP® Labs
The AP® Computer Science Test Development Committee has coordinated the creation of these new AP® Labs for the College Board®. The third AP® Lab, called the AP Picture Lab along with its documentation, is developed by Barbara Ericson of the Georgia Institute of Technology. Exposure Java and its Authors Leon Schram (father) and John Schram (son) include the three AP® labs in this textbook, but had no part in its development. What has been done in Exposure Java is to present the AP® Labs throughout the curriculum where appropriate. The labs are not always shown completely, but rather a smaller sequence of incomplete versions of these labs are introduced to help in understanding the computer science concepts and Java code presented by each lab. Even though, the initial lab introductions, and various stages along the way are different from the AP® labs presented by the College Board®, the final stages shown in Exposure Java are exactly, like the College Board® versions.
69
Section The AP® Picture Lab Experiment 01 Picking a Color
70
// ColorDemo01.java // This demo shows the Red, Green and Blue color with their highest (255) intensities. import java.awt.*; import java.applet.*; public class ColorDemo01 extends Applet { public void paint(Graphics g) { Color brightRed = new Color(255,0,0); Color brightGreen = new Color(0,255,0); Color brightBlue = new Color(0,0,255); System.out.println("Red: " + brightRed); System.out.println("Green: " + brightGreen); System.out.println("Blue: " + brightBlue); g.setColor(brightRed); g.fillRect(50,100,200,200); g.setColor(brightGreen); g.fillRect(300,100,200,200); g.setColor(brightBlue); g.fillRect(550,100,200,200); } }
71
// ColorDemo02.java // This color demo shows a new random color and its // RGB values each time it is executed. import java.awt.*; import java.applet.*; public class ColorDemo02 extends Applet { public void paint(Graphics g) { int rndRed = (int) (Math.random() * 255); int rndGreen = (int) (Math.random() * 255); int rndBlue = (int) (Math.random() * 255); Color randomColor = new Color(rndRed,rndGreen,rndBlue); g.setColor(randomColor); g.fillRect(50,50,400,400); g.setColor(Color.black); g.setFont(new Font("Arial",Font.BOLD,48)); String colorString = "[" + rndRed + "," + rndGreen + "," + rndBlue + "]"; g.drawString(colorString,100,225); } }
72
import javax. swing. JColorChooser; import javax. swing
import javax.swing.JColorChooser; import javax.swing.JFrame; import java.awt.Color; /** * A class to make working with a color chooser easier for students. * It uses a JColorChooser to let the user pick a color and returns the chosen color object. * Barb Ericson */ public class ColorChooser { /** * Method to let the user pick a color and return the color object. the picked color or red if no color was picked */ public static Color pickAColor() { Color color = Color.white; // create a JFrame to be the parent of the color chooser open dialog if you don't do this then you may not see the dialog. JFrame frame = new JFrame(); frame.setAlwaysOnTop(true); // use the color chooser to pick the color color = JColorChooser.showDialog(frame,"Pick a color",color); return color; } /** Main method for testing the ColorChooser */ public static void main(String[ ] args) { Color pickedColor = ColorChooser.pickAColor(); System.out.println(pickedColor); } }
75
Section The AP® Picture Lab Experiment 02 Exploring a Picture
76
/. Test Main. It will explore the beach. /
/** * Test Main. It will explore the beach */ public static void main( String args[]) { Picture pix = new Picture("beach.jpg"); pix.explore(); }
79
Using the Javadoc Comments
Section The AP® Picture Lab Experiment 03 Using the Javadoc Comments
80
2 (out of 3) Different Types of Comments
// This is a single-line comment. System.out.println("Hello!"); // So is this. /* This is a multi-line comment. */ /***************** * So is this. * *****************/
81
Javadoc Comments
82
Using a method requires knowledge of the…
Class identifier Method identifier Method description (what does it do) Method parameter requirements
83
Javadoc Files
84
ColorChooser Javadocs
85
Picture Javadocs
86
Summary of AP® Picture Lab Classes
98
Group Exercises for 3 or 4 People
With your groups analyze the 11 classes and interface. Create a hierarchy diagram. This diagram shows superclasses, subclasses and implementing classes. There are also independent classes which have no interaction with any of the other classes. Identify them. It is not necessary to remember all the many methods, nor is it necessary to even remember all the classes. What is necessary is that you can quickly search through the provided JavaDoc webpages of the AP® Picture labs and find the class information and the method information that you require.
99
Removing All Blue from a Colored Picture
Section The AP® Picture Lab Experiment 04 Removing All Blue from a Colored Picture
100
Step 1 Load the PictureTester.java file.
Scroll to the bottom of the file and look for the main method. You will see many method calls that are all commented out. Only testZeroBlue is called.
101
Step 2 Find method testZeroBlue, which creates a Picture object with "beach.jpg". Method explore is called twice before and after blue removal. The zeroBlue method is called to remove all blue color from the image. public static void testZeroBlue() { Picture beach = new Picture("beach.jpg"); beach.explore(); beach.zeroBlue(); beach.explore(); } Compile and execute the program. You should see the two pictures.
102
Original Beach Picture
103
With Blue Removed
104
Step 3 Examine the zeroBlue method.
For this experiment the PictureTester class and the Picture class have been altered. Many methods, not necessary for this experiment were removed, for simplicity. Load the Picture.java file and find the zeroBlue method.
105
Step 4 Method zeroBlue first creates a 2D pixels array with method getPixels2D. You will not find getPixels2D in the Picture class. Look in the superclass of Picture, SimplePicture; you will find it there. Many other methods are found in SimplePicture class as well. The for..each loop is used to access each individual Pixel object of the array. You will also need to examine the Pixel class, which will be used frequently. The setBlue method of the Pixel class is used to set the blue to 0 for every pixel.
106
Step 4 Continued /** Method to set the blue to 0 */ public void zeroBlue() { Pixel[][] pixels = this.getPixels2D(); for (Pixel[] rowArray : pixels) { for (Pixel pixelObj : rowArray) { pixelObj.setBlue(0); } } }
107
Change a Colored Picture to Black & White
Section The AP® Picture Lab Experiment 05 Change a Colored Picture to Black & White
108
Step 1 Load the PictureTester.java file.
Scroll to the bottom of the file and look for the main method. You will see many method calls that are all commented out. Uncomment the testGrayScale method by removing the two comment slashes.
109
Step 2 public static void testGrayScale() {
Add method testGrayScale to the PictureTester class at the bottom above main. public static void testGrayScale() { Picture pix = new Picture("beach.jpg"); pix.explore(); pix.grayScale(); }
110
Step 3 The PictureTester class is only concerned with creating Picture objects and testing them with some new method. All that has been done so far is call method testGrayScale. In that method a new Picture object is constructed. The object pix then calls method explore, which displays the image. The explore method already exists in the PictureExplorer class. Much of what you will do involves knowing what exists - and can be used - and knowing what does not exist and must be created. The grayScale method does not exist and needs to be created. Why must it be created in the Picture class and not the PictureTester class?
111
Step 4 Black & White pictures are not pictures with groups of pixels that are black or white. These pictures display pixels with shades of gray. There are 256 shades of gray. Each gray shade is created with the same values for red, green and blue. (0,0,0) is totally black. (100,100,100) is a medium shade of gray. Here are steps necessary to turn a colored picture into a black & white picture: Create a two-dimensional array pixels object Pixel objects store color values. Access each pixel in the array and get the three color values. Add up the three color values of the pixel. Divide this color sum by three to get the gray value. Assign the equivalent gray color to the pixel.
112
Step 5 In your groups create the grayScale method. Do not be afraid to use trial & error. It is not necessary to have the complete and correct solution in your head before you start. We learn a lot from incorrect program code. The first picture below is the beach.jpg with the original color picture. The second picture shows the converted black and white version of beach.jpg. When you execute the program, the pictures will be on top of each other. You can drag them besides each other to make comparisons.
113
Step 5 Output 1
114
Step 5 Output 2
115
Step 5 Output 3 The black and white picture is actually on top of the color picture. Use your mouse to move it to the side.
116
Section The AP® Picture Lab Experiment 06 Mirror Images Mirror Images
117
Step 1 Observe the pictures on the next several slides which are taken from your future lab assignment. In this experiment you will write various methods that manipulate two-dimensional, number arrays. You will find that the algorithms you create to alter the arrays will follow the same logic when you start to alter the pictures.
122
Step 2 Open the Experiment06 folder and load the Mirror01st.java. The file is shown below and it is the first of five array-altering experiments. Each one of the five programs will create a two-dimensional, static int array with an initializer list. Method displayMatrix is already finished. You and your partner need to write the mirror method.
125
Step 3 Your objective is to complete the program by writing method mirror. The end result is a display of two matrixes. The top matrix is the original array created with the initializer list. The second matrix is the appearance after executing your mirror method.
126
Step 4 Your objective is to complete the program by writing method mirror. The end result is a display of two matrixes. The top matrix is the original array created with the initializer list. The second matrix is the appearance after executing your mirror method.
127
Step 5 Complete method mirrorHorizontal, which alters the two-dimensional integer matrix from the original display, shown at the top, to the new storage, shown at the bottom.
128
Step 6 Complete method mirrorVertical, which alters the two-dimensional integer matrix from the original display, shown at the top, to the new storage, shown at the bottom.
129
Step 7 Complete method mirrorDiagonal, which alters the two-dimensional integer matrix from the original display, shown at the top, to the new storage, shown at the bottom.
130
Step 8 Little was stated about array size in the previous five experiements. All the matrixes were square-sized. That is required for the mirrorDiagonal method, but the others only require a rectangular matrix. Consider odd-sized and even sized matrixes. If your solutions work well for the provided matrix size, will it work also when you change the size? Perhaps your teacher already had you change sizes with each experiment, but if you completed each experiment successfully, with the provided matrix, try it again. Change any even-sized to odd-sized and change the odd-sized to even-sized and see if you methods still perform correctly.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.