What do these do?
y *= 2;
x += 1;
x++;
++x;
Write a for loop that generates the following output
(many middle terms are intentionally left out):
-1000
-996
-992
-988
//... output deleted
988
992
996
1000
Answer
an if question:
For what exact range of values of the variable x does
the code below print the letter C? these
if (x <= 200 )
if (x < 100)
if (x <= 0)
System.out.println("A");
else
System.out.println("B");
else
System.out.println("C");
else
System.out.println("D");
another loop (with a switch) to trace
int count;
for (count = 1; count <= 12; count++) {
switch (count % 3) { // mod 3
case 0:
g.setColor(Color.red);
break;
case 1:
g.setColor(Color.blue);
break;
case 2:
g.setColor(Color.green);
break;
}
g.drawLine(10, count * 10 , 80, count * 10);
}
code and output
Specific to lab 7:
loops and sequencing (the order of sequencing) are very very
very very very very very important to programming
Using the code I provided with editing so will compile:
public void setNewGame(){
int chosenColors[] = {0,0,0,0,0,0,0,0};
// explain why have it ... and why here
// Box boxes[][] = new Box[4][4];
// what is problem with declaring this here?
for (int i = 0; i <= 3; i++) {
// do calculations to set/change x variables
System.out.println("my col value is: " + i);
for (int j = 0; j <= 3; j++){
System.out.println("my row value is: " + j);
// do calculations to set/change y variables
// generate a random color (actually a random
// int) which then accesses a color
// in your array colorArray[] (see below) AND
int rint = (int)(Math.random() * 8);
// this **could** generate more than 2 of the
// same color - so a check is needed ... somewhere
// you have to keep track of what colors have already
// been chosen and not allow more than twice
// another array of ints is a good idea
boxes[i][j] = new Box(x, y, height, width, colorArray[rint]);
// do you have what you think you have?
System.out.println("coordinates: row " + j + " column " + i);
}
}
repaint();
}
looks like. Why?
Set x and y coordinate in outer loop:
for(col = 0; col <=3; col++){
localX = initialBoxX + col*width;
// what and why
System.out.println("my col value is: " + col);
localY = initialBoxY + row*height;
// what and why
for(row = 0; row<4; row++){
System.out.println("my row value is: " + row);
// missing color stuff
boxes[row][col] =
new Box(localX, localY, height, width, colorChoices[rint]);
System.out.println("coordinates: row " + row + " column " + col);
}
}
how this would increment and its applet
and
initialize chosenColor array
for(col = 0; col <=3; col++){
int chosenColors[] = {0,0,0,0,0,0,0,0};
// what and why
localX = initialBoxX + col*width;
System.out.println("my col value is: " + col);
for(row = 0; row<4; row++){
System.out.println("my row value is: " + row);
localY = initialBoxY + row*height;
rint = (int)(Math.random() * 8);
chosenColors[rint] = chosenColors[rint] +1;
// some color stuff missing about choosing two colors
// even if put here would result in the applet below
boxes[row][col] =
new Box(localX, localY, height, width, colorChoices[rint]);
System.out.println("coordinates: row " + row + " column " + col);
}
}
Discussion on how this would look and its applet
and
where to change color
int chosenColors[] = {0,0,0,0,0,0,0,0};
for(col = 0; col <=3; col++){
localX = initialBoxX + col*width;
rint = (int)(Math.random() * 8);
// and the missing color lines again...
System.out.println("my col value is: " + col);
for(row = 0; row<4; row++){
System.out.println("my row value is: " + row);
localY = initialBoxY + row*height;
boxes[row][col] =
new Box(localX, localY, height, width, colorChoices[rint]);
System.out.println("coordinates: row " + row + " column " + col);
}
}
Discussion on how this would look and its applet
and
Simple placement of a line, in general, makes a difference
int chosenColors[] = {0,0,0,0,0,0,0,0};
for(col = 0; col <=3; col++){
localX = initialBoxX + col*width;
System.out.println("my col value is: " + col);
for(row = 0; row<4; row++){
System.out.println("my row value is: " + row);
localY = initialBoxY + row*height;
rint = (int)(Math.random() * 8);
while (chosenColors[rint] >= 2)
{rint = (int)(Math.random() * 8);}
chosenColors[rint] = chosenColors[rint] +1;
boxes[row][col] =
new Box(localX, localY, height, width, colorChoices[rint]);
System.out.println("coordinates: row " + row + " column " + col);
}
}
Think about how this above code differs from the following:
int chosenColors[] = {0,0,0,0,0,0,0,0};
for(col = 0; col <=3; col++){
System.out.println("my col value is: " + col);
for(row = 0; row<4; row++){
System.out.println("my row value is: " + row);
localY = initialBoxY + row*height;
rint = (int)(Math.random() * 8);
while (chosenColors[rint] >= 2)
{rint = (int)(Math.random() * 8);}
chosenColors[rint] = chosenColors[rint] +1;
boxes[row][col] =
new Box(localX, localY, height, width, colorChoices[rint]);
System.out.println("coordinates: row " + row + " column " + col);
}
localX = initialBoxX + col*width;
}
and its applet
It is a very subtle difference in the placement of the line
localX = initialBoxX + col*width;
Consider what it does to the output...