Instructor: Dan Shiovitz
Name: _____________________________________________________________
Mean was 25.20, min 20, max 30.
s_(SYNTAX)_ dictate(s) the form by which they are used.
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 = 13, minute = 29;
double time = 10.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 <= 8.0 && weight >= 50.0 && (width * depth / weight) > 0.30) 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 = 0, b = 6, c = 0; while (a < b) { if (a % 2 == 0) { System.out.print(b * c); c = c + 1; } else System.out.println("Tweedle"); a = a + 1; } System.out.println("Twaddle" + a);
Ok, the first thing to notice is the 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 0, once when it's 1, 2, 3, 4, 5, and then a will be 6 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 "Tweedle" followed by a newline. If it's even, we print the product of b and c, with no newline, and then we increment c by one. When the loop completes, we print "Twaddle" followed by the value of a (which, you remember, was 6 when the loop stopped). Therefore, the output is:
0Tweedle 6Tweedle 12Tweedle Twaddle6