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.

Using Counters

// 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.

Using Accumulators

// 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;
sumOfValues += newValue;

 

Sample Program – AverageValue

This shows the contents of the Average Value program.

 

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 next number in the sequence is always obtained by adding the 2 numbers together and dropping the "10's" digit ex - 8 + 9 = 17, drop the "1" to get "7".
This can be done by moding the answer by 10 or by subtracting 10 if the number is greater than 10.

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:
1, 8, 9, 7, 6, 3, 9, 2, 1, 3, 4, 7, 1, 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
If you get a 2, 3 or 12 on the first roll you loose
If you get any other number, the game is not over and you have to keep rolling

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) you win
If you get a 7, you loose
If you get any other number, you keep rolling until you either get a 7 or your match from the first roll

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.

Add a Play Again option!