Introduction to Java using NetBeans
Section 6 : Loops
Counters, Accumulators, and Sentinels
Many algorithms require counting and summing of values.
Counters are useful for keeping track of the number of times a user enters a value, makes a guess, or types a password.
Accumulators are used for a wide range of reasons such as finding the total of a list of numbers and calculating your score in your favourite game.
A sentinel value is a special value that acts as a flag to signify that a condition has been met. Sentinel values are often stored in constants. Sentinels are used in several program structures including if and loops structures.
// When declaring a counter, you need to give it a starting value; this is called initializing the counter.
int numValues = 0;
// Counters are incremented by a constant value. The following two program statements are equivalent; you can useany of these statement to add one to the counter.
You can add any number to the counter to make it count up or down by any amount.
// When declaring an accumulator, you need to give it a starting value; this is called initializing the accumulator.
int sumOfValues = 0;
// This variable is called an accumulator because its value “accumulates.” The following two program statements are equivalent; you can use either statement to sum values as they are entered by the user.
sumOfValues = sumOfValues + newValue;
Sample Program – AverageValue
Note: To print multiple lines
in a text field use append instead of setText
txtOutputBox.append(whatever + "/n");
Assignment 6.4 - Investment (Console or GUI)
Many people plan for retirement by investing the same amount each year during their working years, hoping to reach $1 million dollars by the time they retire. Write a program that asks a person how much they want to invest each year (minimum $2000) and how much they expect to earn each year in interest (allow from 5% to 15%). The program should calculate how many years it will take to reach $1 million.
Use loops to "trap" the user when they don't enter a valid amount - i.e. force the user to keeep entering the investment and / or rate until they enter something within the acceptable range.
Test values :
Assignment 6.5 - Necklace ( Console or GUI)
An interesting problem in number theory is sometimes called the "necklace problem".
This problem begins with 2 single-digit numbers.
The process continues until the necklace closes - that is, it returns to the original 2 numbers.
Here is an example that starts with the numbers 1 and 8:
Write a program that prompts the user for 2 single-digit positive numbers, and then displays the necklace of numbers generated as a result. Use while loops to force the user to enter the numbers correctly.
Assignment 6.6 - Craps (Console or GUI)
Craps is a dice game played at many casinos around the world. In the game of craps you roll 2 die and get a total.
If you get a 7 or 11 on the first roll you win
When the game goes beyond the first roll, your first roll becomes the "point" that you have to match again. So on all rolls after the first roll:
If you match what you got on the first roll (if you match your point)
Your program should keep playing until the game is either won or lost.
Display each of the rolls.
You may use a roll again button or roll automatically.