Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.
lab document created by Whitaker Brand and Marty Stepp
Goals for today:
for
loops for repeating
lines of code
Write the results of each of the following expressions.
12 / 5 + 8 / 4
|
4 |
|
3 * 4 + 15 / 2
|
19 |
|
-(1 + 2 * 3 + (1 + 2) * 3)
|
-16 |
|
42 % 5 + 16 % 3
|
3 |
a
,
b
, and c
What are the values of a
, b
, and c
after the following statements? Write your answers in the boxes on the
right.
int a = 5; int b = 10; int c = b; a++; // a? 6 b--; // b? 9 c = c + a; // c? 16
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Oops { public static void main(String[] args) { int x; System.out.println("x is" x); int x = 15.2; // set x to 15.2 System.out.println("x is now + x"); int y; // set y to 1 more than x y = int x++; System.out.println("x and y are " + x + and + y); } } |
answer on next slide...
+
between "x is"
and x
x
before assigning it a
value
15.2
into a variable of
type int
"
mark should be between now
and +
int
here
y
should be same type as x
y
to be 1 more
than x
and
should be in quotes with surrounding spaces
public class Oops { public static void main(String[] args) { double x = 0.0; System.out.println("x is" + x); x = 15.2; // set x to 15.2 System.out.println("x is now " + x); double y; // set y to 1 more than x y = x + 1; System.out.println("x and y are " + x + " and " + y); } }
for
looppublic class Count2 { public static void main(String[] args) { for ( fill me in! ) { System.out.println( fill me in! ); } } }
2 times 1 = 2 2 times 2 = 4 2 times 3 = 6 2 times 4 = 8
for
loop
practicepublic class Triangle { public static void main(String[] args) { for (int line = 1; line <= 4; line++) { for (int stars = 1; stars <= expression ; stars++) { System.out.print("*"); } System.out.println(); } } }
continued on the next slide...
We want to produce the following output:
******* ***** *** *
Fill in the table below indicating how many stars appear on each line of output.
Line | Stars |
---|---|
1
|
7 |
2
|
5 |
3
|
3 |
4
|
1 |
continued on the next slide...
We need an expression for the number of stars on each line of this form:
multiplier * line + constant
for (int stars = 1; stars <= expression ; stars++) {Your program should now produce the correct output. You can verify with the Output Comparison Tool.
for
loop:
8 11 14 17 20 23
for
loop
practicepublic class Beer { public static void main(String[] args) { for ( fill me in! ) { System.out.println( fill me in! ); } } }
continued on the next slide...
99 bottles of beer on the wall, 99 bottles of beer Take one down, pass it around, 98 bottles of beer on the wall 98 bottles of beer on the wall, 98 bottles of beer Take one down, pass it around, 97 bottles of beer on the wall ... (output continues in the same pattern) ... 1 bottles of beer on the wall, 1 bottles of beer Take one down, pass it around, 0 bottles of beer on the wall
Write the results of each of the following expressions.
5 * 6 / 4 % 3 - 23 / (14 % 6)
|
-10 |
|
30 % 9 + 5 % 8 - 11 % 4 % 2
|
7 |
|
1 + 9 / 2 * 2.0
|
9.0 |
|
2.5 * 2 + 17 / 4
|
9.0 |
|
4.5 / 3 / 2 + 1
|
1.75 |
|
46 / 3 / 2.0 / 3 * 4/5
|
2.0 |
|
50 / 9 / 2.0 + 200 / 10 / (5.0 / 2)
|
10.5 |
i
,
j
, and k
What are the values of i
, j
, and k
after the following statements?
Write your answers on the right. (What is the code really doing?)
int i = 2; int j = 3; int k = 4; int x = i + j + k; i = x - i - j; // i? 4 j = x - j - k; // j? 2 k = x - i - k; // k? 1
(answer on next slide)
i
: 4
j
: 2
k
: 3
Bday
that
declares four variables and assigns appropriate values to them.
My birthday is 9/19, and Suzy's is 6/14.
Suppose you have a real number variable x
. Write a Java
expression that computes a variable named y
storing the
following value:
y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2
(We haven't learned a way to do exponents yet, but you can simulate them using several multiplications.)
Use the example program on the next slide to test your code.
Copy/paste this program into jGRASP to test your solution.
// expected output: // y is 7043.7 public class EquationY { public static void main(String[] args) { double x = 5; double y = put your expression for y here ; System.out.println("y is " + y); } }
(answer on next slide)
double y = 12.3*x*x*x*x - 9.1*x*x*x + 19.3*x*x - 4.6*x + 34.2;
If you want an added challenge, try to come up with a way to compute the
above value while using the *
operator no more than 4 times.
(click Next → for answer)
double y = (((12.3 * x - 9.1) * x + 19.3) * x - 4.6) * x + 34.2;
public class OddStuff { public static void main(String[] args) { int number = 32; for (int count = 1; count <= number; count++) { System.out.println(number); number = number / 2; } } }
32 16 8 4
******** *********** ************** ***************** ******************** ***********************
public static void stars() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j <= 20 - 2 * i; j++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } }
* * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ******** ******** ********* ********* ********************
for
loops to produce the
following output:
000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999
99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000
999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221
-----1----- ----333---- ---55555--- --7777777-- -999999999-
for
loops:
!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!////// \\\\\\\\!!!!!!//////// \\\\\\\\\\!!//////////
size 4 | size 7 |
---|---|
!!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// |
!!!!!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!!!!!////// \\\\\\\\!!!!!!!!!!//////// \\\\\\\\\\!!!!!!////////// \\\\\\\\\\\\!!//////////// |
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from Chapter 2 and try to solve them! We suggest doing some Exercises related to loops.