Instructor: Dan Shiovitz
Name: _____________________________________________________________
str1 == str2
compares if the contents of Strings str1
and
str2
are the same.
There are different names for these, but basically:
An airplane is about to touch down on a landing pad. Each second
it is still in the air, it slows down by 10 mph (assume it never
gets into negative speed). Implement a code fragment using a
while loop to count down to zero seconds (when the plane
touches down), printing the number of seconds each time and then
print "Final speed at touchdown:
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:
int secondsToTouchdown
and int
currentSpeed
have each been declared and initialized to
some value.. An example output might be, for some value of
secondsToTouchdown and currentSpeed:
5..4..3..2..1..Final speed at touchdown: 20 mph.
while (secondsToTouchdown > 0)
{
System.out.print(secondsToTouchdown + "..");
currentSpeed = currentSpeed - 10;
}
System.out.println("Final speed at touchdown: " + currentSpeed + " mph.");
char charAt(int pos)
int length()
String str = new String("Panda");
int len = str.length();
int x, y, z = 1;
while (z <= len) {
x = len - z;
while (x < len) {
System.out.print(str.charAt(x));
x = x + 1;
}
y = len;
while (y > z) {
System.out.print(y);
y = y - 1;
}
System.out.println();
z = z + 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 (z <= len). z 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 "Panda" 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 (x < len). len is always
5, and x increases by one each time, so the question is what does x
start at? x starts at something that depends on the value of len and
z: c = (len - z); len is always 5, again, and z ranges from 1-5, so
x'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 x. x, 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 "a" (because charAt indexes
strings from 0). The second time it runs, it will run twice and
print "d" and then "a". The last time it runs it will run five times
and print out "P", then "a", then "n", then "d", then "a".
The second loop is almost identical to this one, except it counts
down rather than up. Also, it's printing out the value of y every
time (not the character y -- that'd be 'y' (with the single
quotes)). So the final expected output is:
a 5 4 3 2
d a 5 4 3
n d a 5 4
a n d a 5
P a n d a