- Write a Java program that asks the user for a positive integer
n and computes and prints out the sum of the squares of all
numbers smaller than or equal to n, that is, 1 +
22 + 32 + 42 +...+n2
For example,
Please enter a positive number: 4
The sum of squares of all number up to 4 is 30.
Goodbye.
- Write an algorithm that repeatedly asks the user for a positive
number n and prints the sum 1 + 2 + 3 + ... + n.
When the user enters 0 or a negative number, it
terminates. For example:
Enter a number: 5
The sum is 15.
Enter a number: 10
The sum is 55.
Enter a number: 2
The sum is 3.
Enter a number: -1
Fine, i quit.
- Write a Java program that uses a loop to read in 5 pairs of
scores, where each pair represents the score of a football game
between Bowdoin College and some other team. Determine, for each pair
of numbers, whether Bowdoin won or lost, and after reading the 5
pairs of values, print out the won/lost/tie record of Bowdoin. In
addition, if the record is a perfect 5-0, print out the message
"Congratulations on your undefeated season!!!".
For example:
Enter Bowdoin score: 5
Enter oponent score: 2
[... repeat this 4 more times..]
Bowdoin game statistics:
wins: 3
loses: 1
ties: 1
Feel free to use your imagination to make it more "user friendly". For
example, you can display the game number every time you ask the user
for a score:
game 1:
Enter Bowdoin score: 5
Enter oponent score: 3
game 2:
Enter Bowdoin score: 2
Enter oponent score: 3
[... repeat this 3 more times..]
Thank you.
Bowdoin game statistics:
wins: 3
loses: 1
ties: 1
- Suppose you are assigned to write an algorithm to bore the
user. I'm sure there are better ways to do it, but here is one way to
do it: your algorithm repeatedly asks if the user is bored. If yes, it
keeps repeating, the same question, over and over. Until the user is
finally bored, at which point it terminates. For example:
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 1
Finally!
Can you come up with a more ingenious way to be boring
(while using a loop)? Feel free to share!
- Modify your previous algorithm so that it keep strack of how
many times it asks the question. If the user is not bored after 5
attempts, it gives up (terminates).
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
[....2 more times ]
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
I give up.