### CS51 - Spring 2010 - Lecture 19

• Book problem 15.4.1
- university enrolls int NUM_STUDENTS students
- students can take one or more courses
- Grades for each course are between 0.0 and 4.0
- Calculate the average for each student:

- Note: it's convenient to think about 2D arrays as matrices, but they're not always matrices. Sometimes, you will have rows without the same number of columns. A 2D array is an array of arrays!

• nibbler lab
- due Tuesday night (24 hour extension)

• How is an image represented/stored in a computer?
- a common approach is a matrix of Colors (often represented by RGB values)
- How would we do this?
- Color[][] image

• show ImageEdit demo
- we have two window areas
- the one on the left is our image
- the one on the right is our modified image
- two variables
- Color[][] source
- Color[][] target
- other private variables
- private int height; // number of rows
- private int width; // number of cols
- grayscale
- modifies one pixel at a time
- What makes a color gray?
- gray colors have identical RGB values
- a measure of brightness
- horizontal reversal
- to do this we'll have two for loops and fill in "target"
for( int row = 0; row < height; row++ ){
for( int col = 0; col < width; col++ ){
- let's look at a diagram - where does pixel row, col come from?
- row stays the same
- col is the distance from the col to the end of the image
- target[row][col] = source[row][width-col-1]
- target[row][col] = source[row][source[col].length-col-1]
- vertical inversion
- again, where does pixel row, col come from?
- column stays the same
- row is the distance from the row to the end of the image
- target[row][col] = source[height - row - 1][col]
- Is there a more efficient way of doing this?
for( int row = 0; row < height; row++ ){
target[row] = source[height-row-1]
- Is there any downside/difference to this second approach?
- rectangular averaging
- target is the average of the pixels in the source in a rectangular range
- private int range; // defines how far from the pixel we look in any direction
- let's start by not worrying about the boundary cases
for( int row = 0; row < height; row++ ){
for( int col = 0; col < width; col++ ){
int redSum = 0;
int greenSum = 0;
int blueSum = 0;
int count = 0;

for( int i = row-range; i < row+range+1; i++ ){
for( int j = col-range; j < col+range+1; j++ ){
redSum += source[i][j].getRed();
greenSum += source[i][j].getGreen();
blueSum += source[i][j].getBlue();
count++;
}
}

// average the values
target[row][col] = new Color(redSum/count, greenSum/count, blueSum/count);
}
}
- What do we need to add to account for the boundaries?
- Some useful functions
- Math.min
- Math.max
for( int i = Math.max(O,row-range); i < Math.min(width, row+range+1); i++ ){
for( int j = Math.max(0,col-range); j < Math.min(hieght, col+range+1); j++ ){

• larger dimensional arrays
- int[][][]
- the java language does not restrict the number of dimensions
- most implementations, max at 255

• extensible arrays
- how would you do it?
- java.util.ArrayList
- get(index) // returns value at index
- set(index, value) // sets value at index to be value

• Strings
- Where have we seen them so far?
- "This is a string"
- ""+10 // gives us the String version of 10
- JLabel label;
- label.getText();
- String[] words;
- System.out.println(String);
- What methods might we want?
- concatenation: +
- substring
- indexOf
- startsWith
- endsWith
- trim
- split
- equals
- compareTo
- length
- replace
- lowercase
- uppercase
- Strings are a class and are therefore objects
- They're also a built in type
- Strings are immutable!
- you cannot change a String
- all the String methods return a new String
- How do you think they're implemented?
- Array of characters... more on characters later

• Some examples
- String test = " A string";
- test.substring(0, 2);
" A"
- test.substring(2, 7);
" a stri"
- test.substring(7);
"ng"
- test.startsWith(" A ");
true
- test.startsWith(" a");
false
- test.endsWith("n");
false
- test.endsWith("string");
true
- test.toLowercase();
" a string";
- test.toUppercase();
" A STRING";
- test.trim();
"A string"
- test.indexOf("i");
6
- test.indexOf("ring");
5
- test.indexOf("A string");
1
- test.indexOf("banana");
-1
- " This is a string".indexOf("i", 4);
6
- test.replace("s", "S");
" A String"
- test.replace("stri", "ris");
" A rising"
- "This is a string".replace(i, "");
"Ths s a strng";
- test.split(" ");
["", "A", "string"];
- test.trim().toUppercase().substring(3);
"TRING";
- test.toLowercase().trim().substring(5, 7).startsWith("i");
true
- test.subString(0,3) + "longer " + test.subString(3);
" A longer string"
- test.length();
9
- test.toCharArray()
[' ', 'A', ' ', 's', 't', 'r', 'i', 'n', 'g']
- test == " A string";
false
- test.equals(" A string");
true
- test.equals(" a string");
false
- test.equalsIgnoreCase(" a string");
true
- " A string" == " A string"
true
(Java uses a pool of Strings for string literals, so if a String literal occurs multiple times, it actually refers to the same String. Bottom line, just use .equals!)

• show StringDemos demo

• http://www.sr.se/P1/src/sing/#