Instructor: Dan Shiovitz
Name: _____________________________________________________________
Mean was similar to last time, around 25, though with a greater variation.
str1 == str2
compares if the contents of Strings str1
and
str2
are the same.
There are different names for these, but basically:
A rocket ship is sitting on a launch pad, waiting to blast
off. Each second it sits on the launch pad, it uses up 5% of its
remaining fuel. When its second count reaches zero, it blasts off.
Assume the variables int secondsToGo
and
double fuelRemaining
have each been declared and
initialized to some value. Implement a code fragment using a
while loop to count down to zero seconds, printing out the
number of seconds each time, and then print "blast off!" and the
remaining fuel amount when the counter gets to zero. An example
output might be, for some value of secondsToGo and
fuelRemaining:
3..2..1..BLAST OFF! (85.0 units of fuel remaining)
Here's a simple solution:
while (secondsToGo > 0) { System.out.print(secondsToGo + ".."); fuelRemaining = fuelRemaining * 0.95; } System.out.println("BLAST OFF! (" + fuelRemaining + " units of fuel remaining)");
Show the output for the code fragment below in the box provided. Show a trace of your execution for partial credit (the more information given the more likely I will be able to follow your logic).
For your reference, here are two methods from the String class:
char charAt(int pos)
int length()
String str = new String("Score"); int len = str.length(); int a = 1, b, c; while (a <= len) { c = len - a; while (c < len) { System.out.print(str.charAt(c)); c = c + 1; } b = len; while (b > a) { System.out.print('-'); b = b - 1; } System.out.println(); a = a + 1; }Ok, here we go. These are all while loops, so the first thing to do is to determine how many times they all go. They're all counting loops, which have three parts. What are they? The initialization, the test, and the each time. For the outer loop, you can see the test is (a <= len). a increases by one at the bottom of the loop each time, and it starts at 1, so the outer loop will execute len times. The length of "Score" is five. Then there's two inner loops. So the big picture is going to be some number of times (five), do something, and then do something else. Each of those inner somethings is a loop too. A quick glance shows they're both printing something, so we expect to see five lines, with each line made up of two parts. The first inner loop goes while (c < len). len is always 5, and c increases by one each time, so the question is what does c start at? c starts at something that depends on the value of len and a: c = (len - a); len is always 5, again, and a ranges from 1-5, so c's initial value will range from 4 to 0. That, in turn, means the loop will first execute (5 - 4) times, then (5 - 3) times, ..., down to (5 - 0) times. Or, in other words, the five times it executes, it will run first once, then twice, then three times, then four, then five. Each time it runs, the body prints out the character at position c. c, you remember, ranges from its initial value, (somewhere between 4 and 0), and one less than len. Putting those two facts together, we see that the first time the loop runs, it will print str.charAt(4), which is "e" (because charAt indexes strings from 0). The second time it runs, it will run twice and print "r" and then "e". The last time it runs it will run five times and print out "S", then "c", then "o", then "r", then "e". The second loop is almost identical to this one, except it counts down rather than up. Also, it's printing out -s every time, not particular characters. So the final expected output is:
e - - - -
r e - - -
o r e - -
c o r e -
S c o r e