Assignemnt #69: Using Do While Loops with a Model
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: DoWhileSwimming
/// File Name: DoWhileSwimming.java
/// Date Finished: 1/4/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.println( "What is the current water temperature? " );
currentTemperature = keyboard.nextDouble();
savedTemperature = currentTemperature;
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);
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;
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. " );
}
}
/// both Goofus and Gallant swim for a total of 4 minutes when the temp is 80.
/// if the temp is only 78 they can only swim for one minute.
/// both determine the water temp before diving into the lake.
/// a while loop tests the conidtion before execution until a condition is met, however the do while loop executes the code before checking to see if the condition is met.
/// since the While loop tests the code before execution, it can be referred to as a pre-test loop and a do while loop can also be called a post-test loop by the same logic.
Picture of the output