Assignment #69 and do-while Swimming
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: do-while Swimming
/// File Name: DoWhileSwimming.java
/// Date Finished: 10/15/2015
/// 1. At 80.5 degrees, Goofus and Gallant swim for the same amount of time.
/// 2. When 78 is the starting temperature, Gallant stops swimming immediately, but Goofus swims for a minute and then stops.
/// 3. Gallant checks the water temperature before diving in.
/// 4. Goofus does not check the water temperature before diving in.
/// 5. A while loop only runs a portion of code if certain conditions are met, whereas a do-while loop always runs the "do" portion of the code, then checks whether or not the "while" conditions are met and determines whether to run the ensuing portion of code.
/// 6. A while loop is a "pre-test loop," whereas a "do-while" loop is a "post-test loop."
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." );
}
}
Picture of the output