dunlea unit 3
1. Determine the exact output for the following code segment: for (int i=1; i<=10; i++) System.out.println(i*i);
1 4 9 16 25 36 49 64 81 100
What is Output: double num = 24.0; while (num <= 32) { System.out.print(num + " "); num += 1.25; }
24.0 25.25 26.5 27.75 29.0
What is Output: int m = 30, n = 80; while ((m < 200) && (n > 20)) { System.out.println(m + " and " + n); m += 15; n -= 10; }
45 and 70, 60 and 60, 75 and 50, 90 and 40, 105 and 30
To check if two reference variables both refer to the same object, use the operator
==
A state variable must:
A state variable must be initialized. The state will be changed as appropriate. The state must be maintained as appropriate.
Which of the following will print the even numbers beginning with 2 and ending with 20 - there can be more than one solution: a. for (int a=2; a<20; a+=2) System.out.println(a); b. for (int b=0; b<20; b+=2) System.out.println(b+2); c. for (int c=2; c<=20; c++) System.out.println(c); d. for (int d=1; d<=20; d++) if (d%2 == 0) System.out.println(d);
B, C, D
The loop boundary is the
Boolean expression that evaluates as true or false
int b = 24, c = 12; while (b != c) { System.out.println("b is " + b + " and c is " + c); b++; c += 6; } System.out.println("b is " + b + " and c is " + c);
Infinite Loop
int w = 15, x = 85, z = 25; while ((w < 19) && (z > 2)) { z = x/w; x--; w++; System.out.print("Let's run " + w + " miles, "); System.out.print("bike " + x + " miles, "); System.out.println("and skip " + z + " miles."); }
Let's run 16 miles, bike 84 miles, and skip 5 miles. Let's run 17 miles, bike 83 miles, and skip 5 miles. Let's run 18 miles, bike 82 miles, and skip 4 miles. Let's run 19 miles, bike 81 miles, and skip 4 miles.
what is output? int month = 5; switch(month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); case 6: System.out.println("June"); default: System.out.println("Summer or Fall"); break; }
May June Summer or Fall
What is the Output? char rating = 'N'; while (rating <= 'Z') { switch(rating) { case 'N': System.out.println("N rated tires allow for speeds up to 87 mph"); break; case 'P': System.out.println("P rated tires allow for speeds up to 93 mph"); break; case 'Q': System.out.println("Q rated tires allow for speeds up to 99 mph"); break; case 'R': System.out.println("R rated tires allow for speeds up to 106 mph"); break; case 'S': System.out.println("S rated tires allow for speeds up to 112 mph"); break; case 'T': System.out.println("T rated tires allow for speeds up to 118 mph"); break; case 'U': System.out.println("U rated tires allow for speeds up to 124 mph"); break; case 'H': System.out.println("H rated tires allow for speeds up to 130 mph"); break; case 'V': System.out.println("V rated tires allow for speeds up to 149 mph"); break; case 'W': System.out.println("W rated tires allow for speeds up to 168 mph"); break; case 'Y': System.out.println("Y rated tires allow for speeds up to 186 mph"); break; case 'Z': System.out.println("Z rated tires allow for speeds up to 149 mph"); break; default: System.out.println(rating + " is not a tire rating!"); break; } rating +=3;
N rated tires allow for speeds up to 87 mph Q rated tires allow for speeds up to 99 mph T rated tires allow for speeds up to 118 mph W rated tires allow for speeds up to 168 mph Z rated tires allow for speeds up to 149 mph
int bank = 3000; int car = 4000; int hours = 0; while (bank < car) { hours++; bank += 12; } System.out.println("You worked " + hours + " hours to buy your car!");
You worked 84 hours to buy your car!
If several reference variables refer to the same object, each variable is said to be an __________of the object.
aliases
causes program control to exit out of a while loop
break
Java provides a _____________ that forces an immediate end to a control structure (while, for, do, and switch).
break command
this is an example of what loop? int number, total = 0; do { System.out.print ("Enter an integer (-1 to quit) --> "); number = console.readInt(); if (number >= 0) total += number; } while (number >= 0);
do-while loop
what is this an example loop of: char letter; for (letter = 'A'; letter <= 'Z'; letter++) System.out.print( letter);
for loop
An object that has no references to it is called
garbage
find output int m=1; do { System.out.println(m + " " + (m-1)); m+=2; } while (m != 10);
infinite loop
Pseudo code example
initialize total and count to 0 initialize smallest to Integer.MAX_VALUE (the largest value of type int) get first score while score is not a negative value increment total increment count change smallest if necessary get next score subtract smallest from total calculate average
The key difference between a while and do-while is the
location of the boundary condition.
what is this an example of: void picture (int n) { int line, spaces, stars, loop; for (line = 1; line <= n; line++) { spaces = line - 1; for (loop = 1; loop <= spaces; loop++) System.out.print (" "); // print a blank space stars = n - line + 1; for (loop = 1; loop <= stars; loop++) System.out.println ("*"); System.out.println ( ); } }
nested loop
A for loop is appropriate when
the initialization value and number of iterations is known in advance.
In a while loop, the boundary condition is located at the
top of the loop.
What type of loop is this? int number = 1; // initialize while (number <= 10) // loop boundary condition { System.out.println(number); number++; // increment }
while loop