while (condition) statement
Example 1: Output the numbers between 1 and 10 using a while loop. How many times will this loop execute? How many times will the condition be evaluated?
int num = 1;
while ( num <= 10 ) {
System.out.print(num + " ");
num++;
}
System.out.println();
Example 2: Reading an initial value so that the loop test can be evaluated the first time is called priming the loop. This loop stops when number is equal to 0. Do you know exactly how many times this loop will execute?
int sum = 0;
Scanner in = new Scanner(System.in));
System.out.print("Enter a number to be added: ");
int number = in.nextInt();
while ( number != 0 ) {
sum += number;
System.out.print("Enter a number to be added: ");
number = in.nextInt();
}
System.out.println("total = " + sum);
Example 3: If the user enters an invalid value, a loop can be used to print an error message and to continue reading values until a valid one is entered. This loop stops when month is between 1 and 12, inclusive. Do you know exactly how many times this loop will execute?
Scanner in = new Scanner(System.in));
System.out.print("Enter the month (1-12): ");
int month = in.nextInt();
while ( month < 1 || month > 12 ) {
System.out.println("That is an invalid value.");
System.out.print("Enter the month (1-12): ");
month = in.nextInt();
}
System.out.println("month = " + month);
A bank account with an initial balance of $10000 earns 5% interest. The interest is computed at the end of every year on the current balance and then deposited into the bank account. How many years does it take for the balance to reach 20,000? See the table below for an example.
Year Balance 0 10,000 1 10,500 2 11,025 3 11,576.25 4 12,155.06 5 12,762.82
An accessor method is a method that does not change the state of an object. That is, it does not change the values stored in the instance field.
A mutator method is a method that changes the state of an object. That is, it changes one or more of the values stored in the instance field.
Write a program that includes a class Investment. The constructor takes an initial balance and a rate of interest. It will contain the typical accessor methods getBalance and getInterest and a method yearsForBalance that will return the number of years it takes to reach a target balance. Your class should also contain a method reset that takes new values for the initial balance and rate of interest as parameters and stores these values in the instance field - This method should be called from your constructor.
Your test class should:
Write a program that finds the greatest common divisor, or gcd, of two numbers. The gcd of two numbers x and y is the largest number that evenly divides both x and y. For example, the gcd of 12 and 42 is 6, and the gcd of 14 and 74 is 2. An example of Euclids algorithm is given below.
Example 1:
Step 1: Divide one number by the other. The order does not make a difference.12/42 = 0 remainder 12Step 2: Divide the divisor by the remainder from step 1.42/12 = 3 remainder 6Step 2 should be repeated until the remainder = 0. The last divisor is the answer.12/6 = 2 remainder 0
Stop - the answer is 6
Example 2:
Step 1: Divide one number by the other. The order does not make a difference.74/14 = 5 remainder 4Step 2: Divide the divisor by the remainder from step 1.14/4 = 3 remainder 2Step 2 should be repeated until the remainder = 0. The current divisor is the answer.4/2 = 2 remainder 0
Stop - the answer is 2
Write a class Gcd that has a constructor that takes two integers. You should have the usual accessor method(s). Gcd should also have a method calculateGcd that returns the gcd of the numbers. Furnish a method reset that takes new values for the numbers and stores them in the instance fields so that calculateGcd can be calledagain. Call reset from the constructor to initialize your instance field.
Your test class should do the following:
| In a simulation, you repeatedly generate random numbers and use them to simulate an activity. |
Setup:
Public interface for the Dice class:
// Constructor public Dice (int sides) // accessors // Returns the number of sides on the die public int getSides() // Returns the value of the last roll public int getLastRoll() // Outputs the state of the die public String toString() // modifier // Finds and returns the value of the last roll public int roll()
Write a test program that simulates throwing 2 Dice. You are to determine the number of throws it takes to reach a target sum. In the test class: