Goal: Learn how while loops differ from do-while loops.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: do-while Swimming
/// File Name: DoWhileSwimming.java
/// Date Finished: Jan 13, 2016
import java.util.Scanner;
public class DoWhileSwimming
{
public static void main( String[] args ) throws Exception
{
Scanner keyboard = new Scanner(System.in);
String swimmer1 = "GALLANT";
String swimmer2 = "GOOFUS ";
double minimumTemperature = 79.0; // degrees Fahrenheit
double currentTemperature;
double savedTemperature;
int swimTime;
System.out.print("What is the current water temperature? ");
currentTemperature = keyboard.nextDouble();
savedTemperature = currentTemperature; // saves a copy of this value so we can get it back later.
System.out.println( "\nOkay, so the current water temperature is " + currentTemperature + "F." );
System.out.println( swimmer1 + " approaches the lake...." );
swimTime = 0;
while ( currentTemperature >= minimumTemperature )
{
System.out.print( "\t" + swimmer1 + " swims for a bit." );
swimTime++;
System.out.println( " Swim time: " + swimTime + " min." );
Thread.sleep(600); // pauses for 600 milliseconds
currentTemperature -= 0.5; // subtracts 1/2 a degree from the water temperature
System.out.println( "\tThe current water temperature is now " + currentTemperature + "F." );
}
System.out.println( swimmer1 + " stops swimming. Total swim time: " + swimTime + " min." );
currentTemperature = savedTemperature; // restores original water temperature
System.out.println( "\nOkay, so the current water temperature is " + currentTemperature + "F." );
System.out.println( swimmer2 + " approaches the lake...." );
swimTime = 0;
do
{
System.out.print( "\t" + swimmer2 + " swims for a bit." );
swimTime++;
System.out.println( " Swim time: " + swimTime + " min." );
Thread.sleep(600);
currentTemperature -= 0.5;
System.out.println( "\tThe current water temperature is now " + currentTemperature + "F." );
} while ( currentTemperature >= minimumTemperature );
System.out.println( swimmer2 + " stops swimming. Total swim time: " + swimTime + " min." );
}
// When the input was "80.5", each swimmer swam for 4 minutes.
// When the input was "78", Gallant swam for 0 minutes, and Goofus swam for 1 minute.
// Gallant checks the water first before diving in.
// Goofus, however, dives right in, doesn't check first.
// A while loop will check whatever's in the conditional first before running the block.
// A do-while loop, however, will run the block once, and then check the conditional.
// The while loop is the "pre-test" loop, and the do-while loop is the "post-test" loop.
}
Goal: Modify a program, replacing the while loop with a do-while loop.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Flip Again?
/// File Name: FlipAgain.java
/// Date Finished: Jan 13, 2016
import java.util.Random;
import java.util.Scanner;
public class FlipAgain
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
String again;
do {
int flip = rng.nextInt(2);
String coin;
if ( flip == 1 )
coin = "HEADS";
else
coin = "TAILS";
System.out.println( "You flip a coin and it is... " + coin );
System.out.print( "Would you like to flip again (y/n)? " );
again = keyboard.next();
} while (again.equals("y"));
}
}
Goal: Modify the Roll Doubles program to include a do-while loop.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Dice Doubles
/// File Name: DiceDoubles.java
/// Date Finished: Jan 13, 2016
import java.util.Random;
public class DiceDoubles {
public static void main(String[] args) {
Random rand = new Random();
int r1, r2;
System.out.println("HERE COMES THE DICE!");
do {
r1 = 1 + rand.nextInt(6);
r2 = 1 + rand.nextInt(6);
System.out.println();
System.out.println("Roll #1: " + r1);
System.out.println("Roll #2: " + r2);
System.out.println("The total is " + (r1 + r2) + "!");
} while (r1 != r2);
}
}
Goal: Modify the Number Guessing with a Counter program to include a do-while loop.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Again with Number Guessing
/// File Name: NumberGuessingAgain.java
/// Date Finished: Jan 13, 2016
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingAgain {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int correct = 1 + rand.nextInt(10), count = 1, guess;
System.out.print("I have chosen a random number between 1-10. Guess the number.\n> ");
do {
guess = input.nextInt();
if (guess != correct) {
System.out.print("Incorrect try again.\n> ");
count++;
}
} while (guess != correct);
System.out.println("Correct, and it took you " + count + " tries!");
}
}
Goal: Create a simple calculator with a do-while loop.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Baby Calculator
/// File Name: BabyCalculator.java
/// Date Finished: Jan 13, 2016
import java.util.Scanner;
public class BabyCalculator {
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
double a, b, c;
String op;
do
{
System.out.print("> ");
a = keyboard.nextDouble();
op = keyboard.next();
b = keyboard.nextDouble();
if (op.equals("+")) {
c = a + b;
} else if (op.equals("-")) {
c = a - b;
} else if (op.equals("*")) {
c = a * b;
} else if (op.equals("/")) {
c = a / b;
} else if (op.equals("%")) {
c = a % b;
} else if (op.equals("^")) {
c = Math.pow(a, b);
} else {
System.out.println("Undefined operator: '" + op + "'.");
c = 0;
}
System.out.println(c);
} while (a != 0);
}
}