Documentation Index
Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
Use this file to discover all available pages before exploring further.
56. Do-While Swimming - Link
So far you have only worked with one type of loop: the while loop. But there is another type: the “do-while” loop.The do-while loop works almost exactly like a while loop. In fact, most of the time they are equivalent. Examine the program below to see if you can figure out the tiny difference.Files NeededSample OutputGoofus and Gallant are both going swimming. They hate to swim in cold water; once the water temperature drops below 79°F, they stop.Run the program, and type in 80.5 for the water temperature.What is the current water temperature? 80.5
Okay, so the current water temperature is 80.5F.
GALLANT approaches the lake....
GALLANT swims for a bit. Swim time: 1 min.
The current water temperature is now 80.0F.
GALLANT swims for a bit. Swim time: 2 min.
The current water temperature is now 79.5F.
GALLANT swims for a bit. Swim time: 3 min.
The current water temperature is now 79.0F.
GALLANT swims for a bit. Swim time: 4 min.
The current water temperature is now 78.5F.
GALLANT stops swimming. Total swim time: 4 min.
Okay, so the current water temperature is 80.5F.
GOOFUS approaches the lake....
GOOFUS swims for a bit. Swim time: 1 min.
The current water temperature is now 80.0F.
GOOFUS swims for a bit. Swim time: 2 min.
The current water temperature is now 79.5F.
GOOFUS swims for a bit. Swim time: 3 min.
The current water temperature is now 79.0F.
GOOFUS swims for a bit. Swim time: 4 min.
The current water temperature is now 78.5F.
GOOFUS stops swimming. Total swim time: 4 min.
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." );
}
}
Assignments turned in without these things will not receive any points.
- Run the program, and type in 80.5 for the current water temperature. Do Goofus and Gallant swim for the same amount of time? Put your answer in a comment.
Answer: Yes, Goofus and Gallant swim for the same amount of time.
- Run the program again, but this time enter 78 for the starting temperature. What changes?
Answer: The program will not run because the current water temperature is already below the minimum temperature. Except GOOFUS, the program will run because the do-while loop will run at least once.
- Does Gallant check the water temperature first, or does he just dive right in?
Answer: Gallant checks the water temperature first.
- What about Goofus? Does he check the water temperature first or just dive in?
Answer: Goofus just dives right in. He checks the water temperature after swimming.
- What is the difference between a
while loop and a “do-while” loop?
Answer: The difference between a while loop and a “do-while” loop is that the while loop checks the condition first before executing the code block, while the “do-while” loop executes the code block first before checking the condition.
- One of these loops is sometimes called a “pre-test loop”, and the other is called a “post-test loop”. Which one is which?
Answer: The while loop is sometimes called a “pre-test loop”, and the “do-while” loop is called a “post-test loop”.
57. Flip Again? - Link
In this program, you’ll see how using a do-while loop might be better than a while loop.Files NeededSample OutputThe code I have provided does not compile. Once you fix it, it will look roughly like this.You flip a coin and it is... TAILS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? n
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 = "y";
while ( again.equals("y") )
{
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();
}
}
}
Assignments turned in without these things will not receive any points.
- The code as given does not compile. Notice that the
while loop tests if again.equals("y"), but the variable again doesn’t have a value at first. Give it a value so that the code will compile and the loop will run at least once.
Answer: String again = "y"; should be added before the while loop. The code will compile and the loop will run at least once.
- Now that program is working, change the loop from a
while loop to a do-while loop. Make sure it still works.
Answer: The while loop should be changed to a do-while loop. The program will still work.
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") );
- What happens if you delete what you added in step 1? Change the line back to just
String again; Does the program still work? Why or why not? (Answer in a comment.)
Answer: The program will not work because the variable again does not have a value at first. The program will throw an error because the variable again is not initialized.
58. Shorter Double Dice - Link
Redo the Dice Doubles assignment (the dice program with a loop) so that it uses a do-while loop instead of a while loop. Otherwise it should behave exactly the same.If you do this correctly, there should be less code in this version.Sample OutputHERE COME THE DICE!
Roll #1: 3
Roll #2: 5
The total is 8!
Roll #1: 6
Roll #2: 1
The total is 7!
Roll #1: 2
Roll #2: 5
The total is 7!
Roll #1: 1
Roll #2: 1
The total is 2!
import java.util.Random;
public class ShorterDoubleDice
{
public static void main( String[] args )
{
Random r = new Random();
int roll1, roll2;
System.out.println( "HERE COMES THE DICE!" );
do
{
roll1 = 1 + r.nextInt(6);
roll2 = 1 + r.nextInt(6);
System.out.printf( "\nRoll #1: %s" , roll1 );
System.out.printf( "\nRoll #2: %s" , roll2 );
System.out.printf( "\nThe total is %s!\n" , roll1 + roll2 );
} while ( roll1 != roll2 );
}
}
Assignments turned in without these things will not receive any points.
- What is a do-while loop?
Answer: A do-while loop is a loop that executes a block of code at least once before checking the condition.
59. Again with the Number-Guessing - Link
Redo the Number-Guessing with a Counter assignment using a do-while loop instead of a while loop. Otherwise it should do exactly the same things (including the counter).Make sure that it doesn’t mess up if you guess it on the first try.Sample OutputI have chosen a number between 1 and 10. Try to guess it.
Your guess: 5
That is incorrect. Guess again.
Your guess: 4
That is incorrect. Guess again.
Your guess: 8
That is incorrect. Guess again.
Your guess: 6
That's right! You're a good guesser.
It only took you 4 tries.
AgainWithTheNumberGuessing.java
import java.util.Random;
import java.util.Scanner;
public class AgainWithTheNumberGuessing
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int number = 1 + r.nextInt(10);
int guess;
int tries = 0;
System.out.println( "I have chosen a number between 1 and 10. Try to guess it." );
do
{
System.out.print( "Your guess: " );
guess = keyboard.nextInt();
if ( guess != number )
System.out.println( "That is incorrect. Guess again." );
tries++;
} while ( guess != number );
System.out.println( "That's right! You're a good guesser." );
System.out.printf( "It only took you %s tries.\n" , tries );
}
}
114. Baby Calculator - Link
Write a calculator program that allows the user to add, subtract, multiply or divide two numbers. (This is sometimes called a “four-function” calculator.)The program must loop until they enter a zero as the first number.Files NeededSample Output>2 + 3
5
>4 * 9
36
>0 + 2
Bye, now.
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();
if ( a == 0 )
{
System.out.println("Bye, now.");
break;
}
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
{
System.out.println("Undefined operator: " + op);
continue;
}
System.out.println(c);
} while ( true );
}
}