Instructor: Dan Shiovitz
Name: _____________________________________________________________
Mean was 25.52, min 17, max 30.
s_(SEMANTICS)_ dictate(s) their meaning.
A number of answers here, of course. The best two:
Given the variables below, evaluate the following expressions.
Distinguish floating-point numbers from integers.
int hour = 3, minute = 35;
double time = 11.0;
Implement a code fragment (not a whole program) that prints whether or not a potato is suitable for being made into potato chips. Assume you are given four variables, weight, length, width, and depth, for the potato, and that they have been assigned positive floating-point values. Print "yes" if the potato with those values meets the standards given below, and "no" if it doesn't. Assume a rectangular potato.
if (length >= 10.0 && weight > 66.0 && (width * depth / weight) >= 0.35) System.out.println("Yes."); else System.out.println("No.");
Show the output for the code fragment below in the box provded. Show a trace of your execution for partial credit (the more information given the more likely I will be able to follow your logic).
int a = 18, b = 23, c = 0; while (a < b) { if (a % 2 == 1) System.out.print(c); else { System.out.println("Hop"); c = c + 1; } a = a + 1; } System.out.println("Skip Jump" + b);
Ok, the first thing to notice is that this loop loops as long as a is less than b. b never changes. a gets incremented by one each time the loop body occurs. Therefore the loop will happen once when a is 18, once when it's 19, 20, 21, 22, and then a will be 23 and the test will fail and the loop will stop. Each time the loop body happens, also, before a is incremented, we test if it's odd or even (that's what the % 2 does, remember?). If it's odd, we print (with no newline!) the value of c (which is a variable, not a letter, so it won't print "c", it'll print whatever c stores). If it's even, we print "Hop", followed by a newline, and then increase c. When the loop completes, we print "Skip Jump" followed by the value of b, which doesn't change for the whole program. Therefore, the output is:
Hop 1Hop 2Hop Skip Jump23