Instructions: Follow the steps given in each section below. For each question, click the radio button next to the most correct answer. When you are finished, type your name and your partner at the end of the document, write down your answers for your records, then press the Submit button. You may submit just one lab response. If you submit more than one, only the first one counts for a grade.
This lab is designed to review loop control structures in Java. By the end of this lab, you should have a good grasp of the difference between counting loops and event-controlled loops. You should also be able to tell when to use each of the loop control structures in Java: the for statement, the while statement, and the do-while statement. You should also know how to avoid writing an infinite loop in a program, and how to stop an infinite loop from the keyboard.
This lab also covers material on numerical accuracy and introduces you to representation of integers and floating-point numbers in Java. By the end of this lab, you should be able to explain how to avoid round-off errors in programming, and how to avoid overflow conditions when using integers.
// Demonstrates a counting loop public class SimpleCountingLoop { public static void main(String[] args) throws java.io.IOException { for (int count = 1; count <= 5; count++) { System.out.print(count); } System.out.println("!!"); } }
// Demonstrates a counting loop in which the user controls // the number of iterations import Keyboard; public class UserControlledCountingLoop { public static void main(String[] args) throws java.io.IOException { int repeatTimes = Keyboard.readInt("Count to: "); for (int count = 1; count <= repeatTimes; count++) { System.out.print(count); } System.out.println("!!"); } }
// Demonstrates a loop that's not a counting loop import Keyboard; public class NotCountingLoop { public static void main(String[] args) throws java.io.IOException { char countAgain = Keyboard.readChar("Start? (y/n): ", "yn"); while (countAgain == 'y' || countAgain == 'Y') { System.out.println("Looping ... "); countAgain = Keyboard.readChar("Again? (y/n): ", "yn"); } System.out.println("Done"); } }
// Shows that the starting value of a for statement can // be a variable. import Keyboard; public class CountFromValue { public static void main(String[] args) throws java.io.IOException { int startAt = Keyboard.readInt("Start counting at: "); for (int count = startAt; count <= 5; count++) { System.out.print(count); } System.out.println("!!"); } }
// Shows how to count down rather than up. // Replace ??? with the correct expression. public class CountDown { public static void main(String[] args) throws java.io.IOException { for (int count = 5; count >= 1; ???) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
// Shows how to make a counting loop count by a value // besides 1 (or -1) public class CountByThree { public static void main(String[] args) throws java.io.IOException { for (int count = 0; count <= 12; count = count + 3) { System.out.print(count); } System.out.println("!!"); } }
// Demonstrates use of a for statement that uses floating-point // values. // Replace each occurrence of ??? with a floating-point value, so // the program counts from 1.0 to 2.0 by 0.1 public class FloatingPointCount { public static void main(String[] args) throws java.io.IOException { for (double count = ???; count < ???; count = count + ???) { System.out.println("Counting ... " + count); } System.out.println("Done"); } }
Counting ... 1.0 Counting ... 1.1 Counting ... 1.2 Counting ... 1.3 Counting ... 1.4 Counting ... 1.5 Counting ... 1.6 Counting ... 1.7 Counting ... 1.8 Counting ... 1.9 Counting ... 2.0 Done
for (double count = ???; count <= ???; count = count + ???)
That is, with <= instead of <. This may look more correct, but it doesn't work very well.
while (condition) { statements; }
// Demonstrates use of the while statement public class DemonstrateWhile { public static void main(String[] args) throws java.io.IOException { char sayAgain = Keyboard.readChar("Say hello? (y/n): ", "yn"); while (sayAgain == 'y' || sayAgain == 'Y') { System.out.println("H E L L O !!"); sayAgain = Keyboard.readChar("Say hello again? (y/n): ", "yn"); } } }
// Shows how to use a sentinel value to mark the end of data public class DemonstrateSentinel { public static void main(String[] args) throws java.io.IOException { int product = 1, number; number = Keyboard.readInt("Enter first number (0 to stop): "); while (number != 0) { product = product * number; number = Keyboard.readInt("Enter next number (0 to stop): "); } System.out.println("The product is " + product); } }
// Shows how to use a sentinel value to mark the end of data // WRONG!! public class DemonstrateSentinel2 { public static void main(String[] args) throws java.io.IOException { int product = 1, number; while (number != 0) { number = Keyboard.readInt("Enter next number (0 to stop): "); product = product * number; } System.out.println("The product is " + product); } }
int product = 1, number = 0;
initialize the variable used in the condition while (the variable is not equal to sentinel value) { do whatever needs to be done inside the loop get the next value of the variable used in the condition }
// Demonstration of an infinite loop public class InfiniteLoop { public static void main(String[] args) throws java.io.IOException { int x = 0; while (x < 10) { System.out.print(x); } System.out.println("Done!"); } }
// Demonstration of a do-while loop public class DemonstrateDoWhile { public static void main(String[] args) throws java.io.IOException { int response; do { response = Keyboard.readInt("What's the sum of 3 and 4? "); } while (response != 7); System.out.println("That's right!"); } }
// This program compares a while statement with a do-while // statement public class CompareWhileAndDoWhile { public static void main(String[] args) throws java.io.IOException { int number; System.out.println("Begin while statement ---"); number = 100; while (number < 10) { System.out.println(number); number++; } System.out.println("--- End while statement"); System.out.println("Begin do-while statement ---"); number = 100; do { System.out.println(number); number++; } while (number < 10); System.out.println("--- End do-while statement"); } }
Begin while statement --- ?????? --- End while statement Begin do-while statement --- ?????? --- End do-while statement
// Add up 0.10 until the sum is 1.0 public class AddTo1 { public static void main(String[] args) { double total = 0.0; while (total != 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done"); } }
double total = 0; while (total != 1) { total = total + 1/10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total != 1) { total = total + 1/10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (total != 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total != 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
int total = 0; while (total != 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
int total = 0.0; while (total != 10) { total = total + 1; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total != 10) { total = total + 1; System.out.println("Total so far: " + total); } System.out.println("Done");
int total = 0; while (total != 10) { total = total + 1; System.out.println("Total so far: " + total / 10.0); } System.out.println("Done");
while (total != 10) { total = total + 1; System.out.println("Total so far: " + total / 10.0); } System.out.println("Done");
double total = 0.0; while (total != 1.1) { while (total <= 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } } System.out.println("Done");
while (total != 1.1) { while (total <= 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } } System.out.println("Done");
double total = 0.0; while (total != 1.1) { total <= total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total != 1.1) { total <= total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (total != 0.9) { if (total <= 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } } System.out.println("Done");
while (total != 0.9) { if (total <= 1.0) { total = total + 0.10; System.out.println("Total so far: " + total); } } System.out.println("Done");
double total = 0.0; while (total <= 0.9) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total <= 0.9) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (total - 1.0 > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (total - 1.0 > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (1.0 - total > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (1.0 - total > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (Math.abs(total - 1.0) < 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (Math.abs(total - 1.0) < 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
double total = 0.0; while (Math.abs(total - 1.0) > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
while (Math.abs(total - 1.0) > 0.001) { total = total + 0.10; System.out.println("Total so far: " + total); } System.out.println("Done");
// This program takes the square root of a number, squares that // number, and compares the result squared with the original import Keyboard; public class Squares { public static void main(String[] args) throws java.io.IOException { System.out.println("This program takes the square root of a "); System.out.println("number, squares that number, and compares"); System.out.println("the result squared with the original."); // Get the number double value = Keyboard.readDouble("Enter a number: "); // Take the square root double squareRootOfValue = Math.sqrt(value); System.out.println("The square root of " + value + " is " + squareRootOfValue); // Find the square of the square root double squareOfSquareRootOfValue = squareRootOfValue * squareRootOfValue; System.out.println("The square of the square root of " + value + " is " + squareOfSquareRootOfValue); // Compare the square of the square root with the original if (squareOfSquareRootOfValue == value) { System.out.println("The square of the square root of " + value + " is equal to " + value); } else { System.out.println("The square of the square root of " + value + " is NOT equal to " + value); System.out.println("The difference is " + (value - squareOfSquareRootOfValue)); } } }
// Subtracts one from 4.067815793829482158E18 and shows the result import Keyboard; public class InaccurateArithmetic { public static void main(String[] args) throws java.io.IOException { double centimetersToProximaCentauri = 4.067815793829482158E18; double centimetersToProximaCentauriMinus1 = centimetersToProximaCentauri - 1; System.out.println("centimetersToProximaCentauri is supposed to be --> " + "4.067815793829482158E18"); System.out.println("centimetersToProximaCentauri is stored as -------> " + centimetersToProximaCentauri); System.out.println("centimetersToProximaCentauri - 1 is stored as ---> " + centimetersToProximaCentauriMinus1); if (centimetersToProximaCentauri == centimetersToProximaCentauriMinus1) { System.out.println("The last two numbers are the same"); } else { System.out.println("The last two numbers are different"); } } }
// Illustrates integer overflow import Keyboard; public class Overflow { public static void main(String[] args) throws java.io.IOException { System.out.println(2000 * 1000 * 1000 * 4 / 4); } }
Write down your answers for your records, then press the Submit button below to turn in the lab assignment.