New Slide Presentation: 2d arrays
1
Grades are due Thursday 2/24
To be counted for this 6 week marking period I need the following assignments by Wednesday 2/23:
2
2 dimensional arrays
3
2 dimensional arrays
4
2 dimensional arrays
int [][] naNums= {{-3, -1, 5},
{7, 12, 13} };
/*Java automatically sizes the array to 2 rows and 3 columns
Note: rows first, then columns!*/
System.out.println(naNums[1][2]);
//displays 13
System.out.println(naNums[2][1]);
//Out of bounds exception!
5
2 dimensional arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
6
2 dimensional arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
7
2 dimensional arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
System.out.println(alphabet[0][1]);
System.out.println(alphabet[1][0]);
System.out.println(alphabet[3][4]);
System.out.println(alphabet[4][3]);
8
2 dimensional arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
System.out.println(alphabet[0][1]);
System.out.println(alphabet[1][0]);
System.out.println(alphabet[3][4]);
System.out.println(alphabet[4][3]);
9
Find the output
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
System.out.print(alphabet[3][3]);
System.out.print(alphabet[0][0]);
System.out.print(alphabet[1][3]);
System.out.println(alphabet[2][4]);
System.out.println(alphabet.length);//white
System.out.println(alphabet[0].length);//yellow
System.out.println(alphabet[3].length);//red
10
11
Review: Loops
X X X X X X
12
Review: Loops
for(int i = ?; i <= ?; i?)
{
System.out.print("X ");
}
X X X X X X
13
Review: Loops
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
X X X X X X
14
Review: Nested Loops
X X X X X X
X X X X X X
X X X X X X
15
Review: Nested Loops
for(??)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
??
}
X X X X X X
X X X X X X
X X X X X X
16
Review: Nested Loops
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
X X X X X X
X X X X X X
X X X X X X
17
Review: Nested Loops
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
18
Review: Nested Loops
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
X X X X X X
X X X X X X
X X X X X X
19
Review: Nested Loops
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
X X X X X X
X X X X X X
X X X X X X
20
Review: Nested Loops
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
X X X X X X
X X X X X X
X X X X X X
21
A good thing to remember
for(int j = 1; j <= 3; j++)
{
for(int i = 1; i <= 6; i++)
{
System.out.print("X ");
}
System.out.println(); //ends the line
}
Codingbat 2d array problems
22
Codingbat: countFives
23
Codingbat: countFives
24
Nested loops
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
25
Nested loops
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
26
Nested loops
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20 x2
3 6 9 12 15 18 21 24 27 30 x3
4 8 12 16 20 24 28 32 36 40 x4
5 10 15 20 25 30 35 40 45 50 x5
6 12 18 24 30 36 42 48 54 60 x6
7 14 21 28 35 42 49 56 63 70 x7
8 16 24 32 40 48 56 64 72 80 x8
9 18 27 36 45 54 63 72 81 90 x9
1020 30 40 50 60 70 80 90 100 x10
27
Nested loops
for(int nJ = 1; nJ <= 10; nJ++)
System.out.print(nJ + "\t");
//inside loop counts columns 1 to 10
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20 x2
3 6 9 12 15 18 21 24 27 30 x3
4 8 12 16 20 24 28 32 36 40 x4
5 10 15 20 25 30 35 40 45 50 x5
6 12 18 24 30 36 42 48 54 60 x6
7 14 21 28 35 42 49 56 63 70 x7
8 16 24 32 40 48 56 64 72 80 x8
9 18 27 36 45 54 63 72 81 90 x9
1020 30 40 50 60 70 80 90 100 x10
28
Nested loops
for(int nI = 1; nI <= 10; nI++)
{
for(int nJ = 1; nJ <= 10; nJ++)
System.out.print( ?? + "\t");
??
}//outside loop counts rows 1 to 10
//(inside loop column)*outside row
//ends the line
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20 x2
3 6 9 12 15 18 21 24 27 30 x3
4 8 12 16 20 24 28 32 36 40 x4
5 10 15 20 25 30 35 40 45 50 x5
6 12 18 24 30 36 42 48 54 60 x6
7 14 21 28 35 42 49 56 63 70 x7
8 16 24 32 40 48 56 64 72 80 x8
9 18 27 36 45 54 63 72 81 90 x9
10 20 30 40 50 60 70 80 90 100 x10
29
Nested loops
30
Nested loops and 2D arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
31
Nested loops and 2D arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
for(int r = 0; r < alphabet.length; r++)
32
Nested loops and 2D arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
for(int r = 0; r < alphabet.length; r++)
for(int c = 0; c < alphabet[r].length; c++)
System.out.print(alphabet[r][c]);
33
This is necessary if the rows don’t all have the same number of elements
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
for(int r = 0; r < alphabet.length; r++)
for(int c = 0; c < alphabet[r].length; c++)
System.out.print(alphabet[r][c]);
34
You may be familiar with this 2d array where the rows don’t all have the same number of elements
35
Here’s another non-rectangular 2d array where different rows have a different number of columns
36
Here’s another non-rectangular 2d array where different rows have a different number of columns
37
Practice Quiz Question:�Complete the 2 nested loops
String [][] words ={
{"the", "quick", "brown", "fox"},
{"jumps", "over"},
{"the", "lazy", "dog"}
};
for(int r = ??; ??; ??)
for(int c = ??; ??; ??)
System.out.print(words[r][c] + " ");
Output
38
Buttons and GUIs
39
Adding Guido
40
Adding Guido
41
Adding Guido
42
Example code
43
Example code
44
Adding Guido Manually
45
Defining a MyButton class and using it
import de.bezier.guido.*;
private MyButton button1;
public void setup (){
size(400, 400);
textAlign(CENTER);
textSize(24);
// make the manager
Interactive.make( this );
// create a button
button1 = new MyButton( 190, 190, 20, 20 );
}
public void draw (){
background( 0 );
if(button1.isOn())
text("Button is Activated", 200, 240);
}
public class MyButton{
private float x, y, width, height;
private boolean on;
public MyButton ( float xx, float yy, float w, float h ){
x = xx; y = yy; width = w; height = h;
Interactive.add( this ); // register it with the manager
}
public void mousePressed () { on = !on;}
public void draw () {
if ( on ) fill( 200 );
else fill( 100 );
rect(x, y, width, height);
}
public boolean isOn(){return on;}
}
46
Defining a MyButton class
public class MyButton
{
private float x, y, width, height;
private boolean on;
public MyButton ( float xx, float yy,
float w, float h )
{
x = xx; y = yy; width = w; height = h;
Interactive.add( this );
// register it with the manager
}
public void mousePressed () { on = !on;}
public void draw ()
{
if ( on ) fill( 200 );
else fill( 100 );
rect(x, y, width, height);
}
public boolean isOn(){return on;}
}
47
Using MyButton
import de.bezier.guido.*;
private MyButton button1;
public void setup ()
{
size(400, 400);
textAlign(CENTER);
textSize(24);
// make the manager
Interactive.make( this );
// create a button
button1 =
new MyButton( 190, 190, 20, 20 );
}
public void draw ()
{
background( 0 );
if(button1.isOn())
text("Button is Activated",200, 240);
}
48
In class: Copy and paste the code below and then modify it to have two larger buttons. Display the text only if both are on.
import de.bezier.guido.*;
private MyButton button1;
public void setup (){
size(400, 400);
textAlign(CENTER);
textSize(24);
// make the manager
Interactive.make( this );
// create a button
button1 = new MyButton( 190, 190, 20, 20 );
}
public void draw (){
background( 0 );
if(button1.isOn())
text("Button is Activated", 200, 240);
}
public class MyButton
{
private float x, y, width, height;
private boolean on;
public MyButton ( float xx, float yy, float w, float h )
{
x = xx; y = yy; width = w; height = h;
Interactive.add( this ); // register it with the manager
}
public void mousePressed () { on = !on;}
public void draw ()
{
if ( on ) fill( 200 );
else fill( 100 );
rect(x, y, width, height);
}
public boolean isOn(){return on;}
}
49
Or fork this repl.it https://repl.it/@MrSimonLowell/ButtonPractice1
50
The Game of Life
The Game of Life probably isn't what you think, it's a program that simulates how cells might grow or die based on a small set of rules
51
Video: The Game of Life
52
The Game of Life
53
Stable and Repeating Patterns
54
Getting Started with Game of Life
55
create two integer constants NUM_ROWS and NUM_COLS
public final static int SOME_CONSTANT = 5;
public void setup() {
//constants are NOT local to a function
}
create two integer constants NUM_ROWS and NUM_COLS
Declaring and Initializing 2d arrays: How many rows and columns?
int [][] naNums;
naNums = new int[2][3];
56
Declaring and Initializing 2d arrays: 2 rows and 3 columns
int [][] naNums;
naNums = new int[2][3];
57
Declaring and Initializing 2d arrays: What would the output be?
int [][] naNums;
naNums = new int[2][3];
System.out.println(naNums[1][1]);
58
Zero!�In Java, uninitialized values are 0 or equivalent
int [][] naNums;
naNums = new int[2][3];
System.out.println(naNums[1][1]);
59
Now, what would the output be?
boolean [][] naNums;
naNums = new boolean[2][3];
System.out.println(naNums[1][1]);
60
false�(the boolean equivalent of zero)
int [][] naNums;
naNums = new int[2][3];
System.out.println(naNums[1][1]);
61
What would the output be with a String?
String [][] naNums;
naNums = new String[2][3];
System.out.println(naNums[1][1]);
62
null�(the Object or pointer equivalent of zero)
String [][] naNums;
naNums = new String[2][3];
System.out.println(naNums[1][1]);
63
Initializing 2 dimensional arrays
int [][] naNums;
naNums = new int[2][3];
for(int row = 0; row < 2; row++)
for(int col = 0; col < 3; col++)
naNums[row][col] = 3;
64
Initializing a 2d array of Life
private Life[][] buttons;
public void setup ()
{
//other java not shown
buttons = ??
65
Initializing a 2d array of Life
private Life[][] buttons;
public void setup ()
{
//other java not shown
buttons = new Life ??;
66
Initializing a 2d array of Life
private Life[][] buttons;
public void setup ()
{
//other java not shown
buttons = new Life[20][20];
67
Even better: use the constants!
private Life[][] buttons;
public final static int NUM_ROWS = 20;
public final static int NUM_COLS = 20;
public void setup ()
{
//other java not shown
buttons = new Life[NUM_ROWS][NUM_COLS];
68
What’s the mistake?
private Life[][] buttons;
public void setup ()
{
//other java not shown
Life[][] buttons = new Life[20][20];
69
buttons is declared twice
private Life[][] buttons;
public void setup ()
{
//other java not shown
Life[][] buttons = new Life[20][20];
70
How many new Lifes have been created? (Hint: it's a trick question)
private Life[][] buttons;
public void setup ()
{
//other java not shown
buttons = new Life[20][20];
71
Another way to ask the question would be: “What value is in each cell of buttons after this code executes?”
Hint: look back at slides 61 & 62
None yet! Imagine an array with 20 rows and columns each of which is empty
private Life[][] buttons;
public void setup ()
{
//other java not shown
buttons = new Life[20][20];
72
Use nested loops in row column order to initialize buttons
private Life[][] buttons;
public void setup ()
{
//other Java code not shown
for(int r = ??; r < ??; r++)
for(int c = ??; c < ??; c++)
buttons[r][c] = new Life(??,??);
//other Java code not shown
73
Notice the Life constructor takes 2 arguments, the row and the column
Getting the grid to appear
74
Getting the grid to appear
75
76
Video: Conway’s Game of Life Part 2
77
Counting Neighbors
create two integer constants NUM_ROWS and NUM_COLS
78
Counting Neighbors
create two integer constants NUM_ROWS and NUM_COLS
79
Counting Neighbors
create two integer constants NUM_ROWS and NUM_COLS
80
The 8 possible neighbors
create two integer constants NUM_ROWS and NUM_COLS
(row, col)
(row, col-1)
(row, col+1)
(row+1, col)
(row+1, col-1)
(row+1, col+1)
(row-1, col)
(row-1, col-1)
(row-1, col+1)
81
Counting Neighbors
create two integer constants NUM_ROWS and NUM_COLS
What are valid cells?
create two integer constants NUM_ROWS and NUM_COLS
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
valid
not valid
not valid
not valid
not valid
not valid
not valid
not valid
not valid
not valid
83
Codingbat
create two integer constants NUM_ROWS and NUM_COLS
84
Game of Life due date 3/12
create two integer constants NUM_ROWS and NUM_COLS
85
Spraypaint Game of Life
create two integer constants NUM_ROWS and NUM_COLS
86
Why do we need the “buffer”?
create two integer constants NUM_ROWS and NUM_COLS
87
Why do we need the “buffer”?
create two integer constants NUM_ROWS and NUM_COLS
88
Why do we need the “buffer”?
create two integer constants NUM_ROWS and NUM_COLS
0
0
1
2
2
0
1
2
3
2
0
1
2
1
1
89
Why do we need the “buffer”?
create two integer constants NUM_ROWS and NUM_COLS
0
1
2
2
1
0
1
1
1
1
0
1
2
2
1
90
Why do we need the “buffer”?
create two integer constants NUM_ROWS and NUM_COLS
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Quizziz
create two integer constants NUM_ROWS and NUM_COLS
92
Getting Started with Minesweeper
93
create two integer constants NUM_ROWS and NUM_COLS
create two integer constants NUM_ROWS and NUM_COLS
94
initialize the 2d array buttons
create two integer constants NUM_ROWS and NUM_COLS
95
Use nested loops to create a new MSButton for each row column pair
create two integer constants NUM_ROWS and NUM_COLS
96
Uncomment the first two lines of the MSButton constructor
create two integer constants NUM_ROWS and NUM_COLS
97
go to line 7, and initialize bombs to be a new empty ArrayList of type MSButton
create two integer constants NUM_ROWS and NUM_COLS
98
Add code to the setBombs() function
create two integer constants NUM_ROWS and NUM_COLS
99
Uncomment the 3rd and 4th lines of MSButton draw()
create two integer constants NUM_ROWS and NUM_COLS
100
Add a while loop in setBombs()
create two integer constants NUM_ROWS and NUM_COLS
Recursion in Minesweeper
101
Remove marked blobs to the left
102
Remove marked blobs to the left
103
Remove marked blobs to the left
104
Remove marked blobs to the left
105
Remove marked blobs to the left
106
Remove marked blobs to the left
107
Remove marked blobs to the left
108
Remove marked blobs to the left
109
Practice Quiz Question
import de.bezier.guido.*;
private Blob[][] blobs;
void setup ()
{
size(400, 400);
// make the manager
Interactive.make( this );
blobs = new Blob[10][10];
for (int r = 0; r < 10; r++)
for (int c = 0; c < 10; c++)
blobs[r][c] = new Blob(r, c);
}
void draw() {
}//empty
public class Blob
{
private int r, c;
private float x, y, width, height;
private boolean marked;
public Blob ( int rr, int cc )
{
width = 40;
height = 40;
r = rr;
c = cc;
x = c*width;
y = r*height;
marked = Math.random() < .5;
Interactive.add( this ); // register it with the manager
}
public boolean isMarked()
{
return marked;
}
public boolean isValid(int row, int col)
{
if (row>=0 && row<10 && col>=0 && col<10)
return true;
else
return false;
}
public void mousePressed ()
{
marked = false;
//your code here
//if position to left is valid and marked
//call mousePressed for the blob on left
}
public void draw ()
{
if (marked)
fill(50);
else
fill( 255 );
rect(x, y, width, height);
fill(0);
}
}
110
marked vs clicked
111
contains
ArrayList <Integer> arlist =
new ArrayList <Integer>();
arlist.add(3);
System.out.println(arlist.contains(2));
arlist.add(2);
System.out.println(arlist.contains(2));
System.out.println(arlist);
112
contains
ArrayList <Integer> arlist =
new ArrayList <Integer> ();
arlist.add(3);
System.out.println(arlist.contains(2));
arlist.add(2);
System.out.println(arlist.contains(2));
System.out.println(arlist);
113
contains
String str = new String("The best thing about a boolean is even if you are wrong, you are only off by one.");
System.out.println(str.contains("worst"));
System.out.println(str.contains("best"));
System.out.println(str.contains("z"));
114
contains
String str = new String("The best thing about a boolean is even if you are wrong, you are only off by one.");
System.out.println(str.contains("worst"));
System.out.println(str.contains("best"));
System.out.println(str.contains("z"));
115
Checking if it doesn’t contain something
ArrayList <Integer> arlist =
new ArrayList <Integer> ();
arlist.add(1);
arlist.add(2);
if(!arlist.contains(3)) //doesn’t contain 3
arlist.add(3);
System.out.println(arlist);
116
Checking if it doesn’t contain something
ArrayList <Integer> arlist =
new ArrayList <Integer> ();
arlist.add(1);
arlist.add(2);
if(!arlist.contains(3)) //doesn’t contain 3
arlist.add(3);
System.out.println(arlist);
117
Practice Quiz Question:�Find the output
int [] array = {4,-2,4,3,7,-2,4,6};
ArrayList <Integer> arlist =
new ArrayList <Integer> ();
for(int i = 0; i < array.length; i++)
if(!arlist.contains(array[i]))
arlist.add(array[i]);
System.out.println(arlist);
118
119
120
What is the output?
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(myNum);
}
}
121
this
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(this.myNum);
}
}
122
this
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();//displays 5
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(this.myNum);
}
}
123
this
public void mystery() {
System.out.println(this.myNum);
}
public void mystery() {
System.out.println(myNum);
}
124
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(this);
}
public String toString(){
return "My number is " + myNum;
}
}
125
System.out.println(this);
System.out.println(this.toString());
126
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(this);
}
public String toString(){
return "My number is " + myNum;
}
}
127
public void setup() {
Thingy bob = new Thingy(5);
Thingy notBob = new Thingy(7);
bob.mystery();
}
class Thingy {
private int myNum;
public Thingy(int n) {
myNum = n;
}
public void mystery() {
System.out.println(bob); //ERROR!
}
public String toString(){
return "My number is " + myNum;
}
}
128
this
System.out.println(this);
129
2 Part Practice Quiz Question:�A. Find the output�B. Identify which two uses of this are unnecessary
130
public void setup() {
WhatsIt bob = new WhatsIt('a', 1);
WhatsIt notBob = new WhatsIt('b', 2);
notBob.mystery();
}
class WhatsIt {
private char myChar;
private int myNum;
public WhatsIt(char c, int n) {
myChar = c;
myNum = n;
}
public void mystery() {
System.out.println("char is " + this.myChar);
System.out.println(this);
}
public String toString(){
return "number is " + this.myNum;
}
}
In Class:
�
Use appropriate variable names in your loops like row and col
131
"\t" is tab
(5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7)
You may find this helpful:
System.out.print("("+row +","+col+")"+"\t");
132
133
134
Nested loops and graphics
135
One loop will make the x coordinates and the other the y
for( ?? ) //8 rows
{
for( ?? ) //6 columns
{
ellipse(x,y,5,5);
}
}
136
If I move down one row, what's changing, x or y?
for( ?? ) //8 rows
{
for( ?? ) //6 columns
{
ellipse(x,y,5,5);
}
}
137
The outer loop changes y and the inner loop changes x
for(int y = ?? ; y <= ?? ; y ?? ) //8 rows
{
for(int x = ?? ; x <= ?? ; x ?? //6 columns
{
ellipse(x,y,5,5);
}
}
138
The outer loop changes y and the inner loop changes x
for(int y = 10 ; y <= 80 ; y +=10 ) //8 rows
{
for(int x = 20 ; x <= 70 ; x +=10 //6 columns
{
ellipse(x,y,5,5);
}
}
139
The outer loop changes y and the inner loop changes x
for(int y = 10 ; y <= 80 ; y +=10 )
//8 rows at 10 20 30 40 50 60 70 80
{
for(int x = 20 ; x <= 70 ; x +=10
//6 columns at 20 30 40 50 60 70
{
ellipse(x,y,5,5);
}
}
140
Practice Quiz Question
Practice Quiz Question
import de.bezier.guido.*;
private Blob[][] blobs;
public final static int NUM_ROWS = 15;
public final static int NUM_COLS = 12;
void setup ()
{
size(400, 400);
// make the manager
Interactive.make( this );
blobs = new Blob[NUM_ROWS][NUM_COLS];
for(int r = 0; r < NUM_ROWS; r++)
for(int c = 0; c < NUM_COLS; c++)
blobs[r][c] = new Blob(r,c);
}
void draw(){}//empty
public class Blob
{
private int myRow, c;
private float x,y, width, height;
private boolean marked;
public Blob ( int rr, int cc )
{
width = 400/NUM_COLS;
height = 400/NUM_ROWS;
myRow = rr;
c = cc;
x = c*width;
y = myRow*height;
marked = Math.random() < .5;
Interactive.add( this ); // register it with the manager
}
public boolean isMarked()
{
return marked;
}
public boolean isValid(int row, int col)
//post condition: returns true if both row and col are valid, false otherwise
{
//your code here
return false;
}
public void mousePressed ()
{
if(marked == true)
{
marked = false;
if(isValid(myRow,c-1) && blobs[myRow][c-1].isMarked())
blobs[myRow][c-1].mousePressed();
//3 more recursive calls
}
}
public void draw ()
{
if (marked)
fill(50);
else
fill( 255 );
rect(x, y, width, height);
fill(0);
}
}
141
The call to isValid is first
142
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
143
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
144
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
145
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
146
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
147
Constants are public, final and static
public final static int SOME_CONSTANT = 5;
public void setup() {
//java code not shown
}
148
Constants are public, final and static
149
Constants are public, final and static
150
Constants are public, final and static
151
Constants are public, final and static
152
Constants are public, final and static
153
Constants are public, final and static
154
bob
notBob
2
Constants are public, final and static
155
bob
notBob
3
Constants are public, final and static
156
A common beginner mistake
157
A common beginner mistake
158
static variables and functions
159
static variables and functions
160
Practice Quiz Question:�Which line will cause an error?
Thingy bob = new Thingy();
bob.num = 6;
Thingy sam = new Thingy();
sam.num = 7;
161
for each loops & 2D arrays
char [][] alphabet ={
{'a','b','c','d','e','f','g'},
{'h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u'},
{'v','w','x','y','z'}
};
for(char[] row: alphabet)
for(char letter: row)
System.out.print(letter);
162
In class: Modify your previous program to display the x coordinate of the last button that was clicked
163