 |
|

|
 |

09/24-29/09 - Conditional Loops
|
 |
 |
 |
  | Conditional loops don't repeat a specific number of times. They repeat until a particular condition (test) becomes false.
|
 |
 |
 |
 |
  | /** * Demo of while loops * * @author Chuck Iverson * @version 9/24/09 */
import java.util.Scanner;
public class WhileLoops { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); int sum = 0; int value; char response; boolean done = false; int count = 0; while (!done) { ++count; System.out.print("Enter an integer: "); value = keyboard.nextInt(); keyboard.nextLine(); // remove leftover newline character sum += value; System.out.print("Add another integer? (Y/N) "); response = keyboard.nextLine().toUpperCase().charAt(0); done = (response != 'Y'); } System.out.println("The sum of " + count + " integers is " + sum); } }
|
 |
 |
 |
 |
  | Demo of do-while loop and input validation
|
 |
 |
 |
 |
  | /** * Demo of do-while input-validation loop * * @author Chuck Iverson * @version 9/24/09 */
import java.util.Scanner;
public class DoWhileLoop { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); int value; boolean valid; do { System.out.print("Enter a positive integer: "); value = keyboard.nextInt(); valid = (value > 0); if (!valid) System.out.println(value + " is not a positive integer. Try again."); } while (!valid); System.out.println("You entered " + value); } }
|
 |
 |
 |
 |
  | Another example of a do-while loop and input validation
|
 |
 |
 |
 |
  | /** * Demo of do-while input-validation loop * using chars * * @author Chuck Iverson * @version 9/24/09 */
import java.util.Scanner;
public class Vowels { public static void main(String [] args) { Scanner keyboard = new Scanner(System.in); char ch; boolean valid; do { System.out.print("Enter a vowel: "); ch = keyboard.nextLine().charAt(0); valid = isVowel(ch); if (!valid) System.out.println(ch + " is not a vowel. Try again."); } while (!valid); System.out.println("You entered " + ch); } public static boolean isVowel(char ch) { switch (Character.toUpperCase(ch)) { case 'A': case 'E': case 'I': case 'O': case 'U': return true; default: return false; } } }
|
 |
 |
 |
 |
  | Write a program that plays a guessing game with the user. The program should generate a random integer between 1 and some maximum (such as 100), then prompt the user repeatedly to guess the integer. When the user guesses incorrectly, the game should give the user a hint about whether the correct answer is higher or lower than the guess. Once the user guesses correctly, the program should print a message showing the number of guesses made.
Consider extending this program by making it play multiple games until the user chooses to stop and then print the statistics about the player's total and average number of guesses.
|
 |
 |
 |
 |
  | Write a program that turns the guessing game around. Let the user pick an integer, tell the computer the lower bound (such as 1) and upper bound (such as 100), and have the computer use a bisection algorithm (that is, the computer should always pick the middle integer of the interval from lowest to highest). After each guess the computer makes, the user should inform the computer by entering H, L or C whether the computer's guess was too high, too low or correct.
|
 |
 |
 |
 |
  | There are four blood types: A, B, O and AB. The table below shows the rules for “emergency transfusions.”
 Thus you can transfuse anyone with the same blood group as the donor. Additionally you can give type O blood to any recipient (A, B or AB). People with type O blood are called "universal" donors. The table shows that people with type AB blood can receive any type of blood. People with type AB blood are called "universal" recipients.
Create a BloodTransfusion class which contains a static method
public boolean static compatibleTransfusion(String donor, String recipient)
which takes the blood type of the donor and the recipient as string variables and returns true if a compatible transfusion is possible and false, otherwise. Test all possible combinations shown in the table to make sure your program is correct. Then create a main which calls your compatibleTranfusion class in a loop until the user is finished.
|
 |
 |
 |
 |
  | The famous British mathematician G.H. Hardy relates an anecdote that has become famous in mathematical circles. He once mentioned to the brilliant young Indian mathematician Ramanujan that he had ridden in a taxi whose number was ???, which he considered to be a very dull number. Ramanujan promptly replied that, on the contrary, the number was very interesting, since it was the smallest (positive) integer that could be written as the sum of two cubes in two different ways! Write a program (using four nested for loops) to find the number of Hardy’s taxi and the next smallest integer that is the sum of two cubes in two different ways. (Note: to illustrate the idea using squares rather than cubes: 50 is the smallest integer that is the sum of two squares in two different ways (12 + 72 or 52 + 52; 65 is the next smallest integer that is the sum of two squares in two different ways (12 + 82 or 42 + 72). For those seeking a challenge, an interesting extension of this program is to find the ten smallest positive integers that are the sum of two cubes in two different ways. How can you be sure that you have found the smallest values? Does your program necessarily generate them in order? If you can, write a program to guarantee that the values are generated in order from smallest to largest.
|
 |
 |
|


 |
 |
 |