Code
/// Name: Afaf Nabeeha
/// Period: 7
/// Program Name: do-While Swimming
/// File Name: Swimming.java
/// Date Finished: 12/17/2015 public class Swimming
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 ) //pre test loop
{
System.out.print( "\t" + swimmer1 + " swim 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." );
}
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 //post test loop
{
System.out.println( "\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." );
// 1. Yes, both Goofus and Gallant swim for the same amount of time.
// 3. Gallant checks temperature first.
// 4. Goofus goes right in and then stops.
// 5. A while loop checks the condition first before running the program. A do-while loop does not check condition until after the program is finished running.
// 6. The pre-test loop is the while loop and the post-test loop is the do-while loop.
}
}
Picture of the output