Download Discussion S14-5– while loops, methods Exam scores expected by

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Collatz conjecture wikipedia , lookup

Transcript
Discussion S14-5– while loops, methods
Exam scores expected by midnight tonight
Program 4 due Tuesday: be sure to include comments!
A while loop with a special goal:
int num = 300000;
int splitCount = 0;
while (num > 1){
num = num/2;
splitCount++;
}
System.out.println("split count:" + splitCount);
}
1.Write a while-loop that prints the characters in (any) string s in a column in reverse order.
2. Complete the while loop below, which should print a column of numbers from 1000 down to
100 in steps of 2 (1000 -998-996 – etc); and then revise - from 1500 to 300 in steps of 3.
int n =
;
while (
){
System.out.println (
n=
;
}
);
3. Write a while loop that prints
blah (1)
blahblah (2)
blahblahblahblah (4)
blahblahblahblahblahblahblahblah (8)
etc.,
doubling in length on each line, until a line longer than 150 total characters has been printed.
4. How many times does this loop print “hello”:
int j = 0; int k = 15;
while (j < k){j = j + 2; k = k – 3; System.out.println(“hello”);}
5. Write a complete program that does the following. It reads in an int value from the keyboard –
a starting amount of money, in cents. That’s what you get on day one. Then, on every subsequent
day your money doubles – so on day 2 you have twice the start amount, day 3 four times the start
amount, day 4 you have eight times the start amount, and so forth. On what day do you exceed
one million dollars (one hundred million cents) for the first time? Print that day value to the
console as the final program action.
6. Write a one class program that generates a sequence of the so-called hailstone numbers, and
report certain statistics on the sequence your program generates. The hailstone numbers can be
described succinctly as follows: Start with a positive integer; if that number is even, divide it in
half, and continue; if the number is odd, triple it and add 1, and continue. Proceed in this way
until you arrive at the value 1.
For example, if you start with 20, this is the sequence you get: 20,10,5,16,8,4,2,1. If you start
with 21, you get this sequence: 21, 64, 32,16, 8, 4, 2, 1.
Here is how your program should work for the seed value 21:
Welcome to DrJava.
> java Hail
Enter a positive hailstone starting value
21
21
64
32
16
8
4
2
1
start: 21
term count: 8
biggest: 64
7. Diffy Game:
The Diffy Game is a one-player game that manipulates sequences of four whole numbers, called
Diffys. The game starts with an initial Diffy supplied by the user. Each move of the game replaces
the current Diffy with its successor - another Diffy, whose four numbers are the pairwise
successive differences of the original Diffy. So: the successor of (2, 3, 5, 1) is (1, 2, 4, 1) because
1 is the difference of 2 and 3, 2 is the difference of 3 and 5, 4 is the difference of 5 and 1, and 1 is
the difference of 1 and 2.
We always compute the difference by subtracting the smaller number from the larger number.
Notice that the last term in the new Diffy sequence is formed from the last element and the first
element of the old sequence. A typical sequence:
(6 10 5 1)->(4 5 4 5)->(1 1 1 1)->(0 0 0 0)
The Diffy game ends when the numbers in the Diffy sequence are all 0's: (0,0,0,0).
Write a two-class Diffy game player. In the driver read in 4 starting (non-negative) int values,
and then play the game. Be sure to include a nextDiffy method (that returns the next Diffy object
in the sequence); a printDiffy method that reports on the current Diffy; and an isGameOver()
method that determines when the game ends. Your program should print the Diffy sequence
elements in a column as the game proceeds. Note that the framework for the game – a while
loop that terminates when the game is over – is the same as the framework for the Dog
Track assignment.