# Lab 4 Solution

Lab4.

Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab4.java.

Hints

Example of using a loop to read in sentinel values can be found in Section 4.5 of the book as well as

the example videos for the week. The instructions below follow the example in the sample video.

Examples of loops for various tasks can be found in Section 4.7 of the book as well as the videos for the week.

Part 0: Task Overview

The overall goal of this lab is to continually read in integers, one at a time, from the user until a 0 is entered.

0 should not be counted as an input, but rather is a sentinel used to stop the loop. At this point, if the user

entered any values (besides 0) the following output should be displayed:

A line stating the smallest integer the user entered

A line stating the largest integer the user entered

A line stating the number of integers entered

A line stating the number of even integers entered

A line stating the number of odd integers entered

A line stating the average value of all of the integers that the users entered

Otherwise a warning message should be displayed.

IMPORTANT: Please do not include the 0 in these calculations!

The basic outline for the code logic is given below:

Part 1: Before the while

These steps should be carried out in your main method prior to starting the while loop.

1. Set up the Scanner.

2. Declare and initialize variables needed for the calculations in the loop.

Note: It is important to declare these before the loop since they need to be printed out after the

loop. Any variable declared in the loop is only usable inside of the loop.

The following variables will be needed (You can see the coding sample video with sentinel values

and averages for an example of initializing a couple variables before a loop. Here we just initialize

more):

largest number (see below about initializing these)

smallest number (see below about initializing these)

a counter for how many integers have been entered (this would be initialized to 0, since before

the loop no integers have been entered yet)

number of even integers (how many have been entered before the loop is started?)

number of odd integers (how many have been entered before the loop is started?)

sum of all the values entered needed for the average (what is the sum before any value is

entered?)

a variable to hold the input. Prompt the user to

"Enter a series of values (0 to quit):"

4.Read in the rst value with the Scanner and store this in the variable used to hold the input.

5. Initialize the variable that holds the largest and smallest values to the rst input just read in.

Note: These must be initialized to the rst value entered to work properly. If they were just

initialized to 0 then if the user never entered any negative numbers, the minimum would remain

0. Similarly, if the user never entered any positive numbers, the maximum would remain 0.

Part 2: Setting up the while

Now we are ready for the loop.

Set up a while loop to continue while the input is not equal to 0, the sentinel value.

{

For this sentinel problem we will use a while loop (though other loops could be used, particularly

the do while with an extra if).

{

We will have multiple statements inside the while loop. These must go in braces. For example:

while (input not equal to 0) {

// Put the instructions below in here

}

Part 3: Inside the while loop

This is where we will calculate all the needed values. The order these are done inside the loop is not

important. You should add these one at a time and check to make sure the result is working correctly before

adding more.

1. First nd the maximum and minimum values.

The idea is given in the if statements in the while loops in Section 4.7.5. For the maximum (the

minimum can be found with a small change):

if (input greater than maximum)

maximum = input // update max

2. Find the number of even integers entered.

To nd if the current input value is even or odd you can use the remainder operator with 2

(input % 2)

. If the result is 0, the number is even. Otherwise it is odd. The logic for this part

then is:

if (input is even) // that is input % 2 is 0

increase number of evens by 1

otherwise

increase number of odds by 1

3. Calculate the updated sum.

Add the current input to the sum and save the result back in the sum. You can get an idea from

section 4.7.1 or the sentinel average program example.

4. Increase the counter by one since one more input has been seen.

This is as done in the Average example in section 4.7.1 or the sample video with the counter line.

5. Finally, read in the next input value.

This updates the loop control variable to be ready to process the next input.

Part 4: After the Loop

This section goes after the close brace

'}'

of the while loop. Here we will print the results if any data

has been entered or print a message that

"No data was entered"

. The Average example in 4.7.1 and the

Sentinel Average video sample show checking to make sure input was entered.

1. If any data has been entered (What would the count be if only 0 was entered?)

Print out the results stating the max, min, number of integers entered, number even and odd.

(See sample output below)

Calculate the average, by dividing the sum by the count and display the results. Be careful of

integer division if both the sum and count are integer variables.

2.Otherwise, only the sentinel was entered, so output a warning that

"No data was entered"

.

Sample Output

Below is an example of what your output should roughly look like when this lab is completed. All text in

bold represents user input.

Sample Run 1:

Enter a series of integers (zero to quit):

3

-4

5

12

-7

0

The smallest integer is -7

The largest integer is 12

Total number of integers entered is 5

Total even numbers entered is 2

Total odd numbers entered is 3

The average value is 1.8

Sample Run 2:

Enter a series of integers (zero to quit):

-10

-2

-6

0

The smallest integer is -10

The largest integer is -2

Total number of integers entered is 3

Total even numbers entered is 3

Total odd numbers entered is 0

The average value is -6.0

Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab4.java.

Hints

Example of using a loop to read in sentinel values can be found in Section 4.5 of the book as well as

the example videos for the week. The instructions below follow the example in the sample video.

Examples of loops for various tasks can be found in Section 4.7 of the book as well as the videos for the week.

Part 0: Task Overview

The overall goal of this lab is to continually read in integers, one at a time, from the user until a 0 is entered.

0 should not be counted as an input, but rather is a sentinel used to stop the loop. At this point, if the user

entered any values (besides 0) the following output should be displayed:

A line stating the smallest integer the user entered

A line stating the largest integer the user entered

A line stating the number of integers entered

A line stating the number of even integers entered

A line stating the number of odd integers entered

A line stating the average value of all of the integers that the users entered

Otherwise a warning message should be displayed.

IMPORTANT: Please do not include the 0 in these calculations!

The basic outline for the code logic is given below:

Part 1: Before the while

These steps should be carried out in your main method prior to starting the while loop.

1. Set up the Scanner.

2. Declare and initialize variables needed for the calculations in the loop.

Note: It is important to declare these before the loop since they need to be printed out after the

loop. Any variable declared in the loop is only usable inside of the loop.

The following variables will be needed (You can see the coding sample video with sentinel values

and averages for an example of initializing a couple variables before a loop. Here we just initialize

more):

largest number (see below about initializing these)

smallest number (see below about initializing these)

a counter for how many integers have been entered (this would be initialized to 0, since before

the loop no integers have been entered yet)

number of even integers (how many have been entered before the loop is started?)

number of odd integers (how many have been entered before the loop is started?)

sum of all the values entered needed for the average (what is the sum before any value is

entered?)

a variable to hold the input. Prompt the user to

"Enter a series of values (0 to quit):"

4.Read in the rst value with the Scanner and store this in the variable used to hold the input.

5. Initialize the variable that holds the largest and smallest values to the rst input just read in.

Note: These must be initialized to the rst value entered to work properly. If they were just

initialized to 0 then if the user never entered any negative numbers, the minimum would remain

0. Similarly, if the user never entered any positive numbers, the maximum would remain 0.

Part 2: Setting up the while

Now we are ready for the loop.

Set up a while loop to continue while the input is not equal to 0, the sentinel value.

{

For this sentinel problem we will use a while loop (though other loops could be used, particularly

the do while with an extra if).

{

We will have multiple statements inside the while loop. These must go in braces. For example:

while (input not equal to 0) {

// Put the instructions below in here

}

Part 3: Inside the while loop

This is where we will calculate all the needed values. The order these are done inside the loop is not

important. You should add these one at a time and check to make sure the result is working correctly before

adding more.

1. First nd the maximum and minimum values.

The idea is given in the if statements in the while loops in Section 4.7.5. For the maximum (the

minimum can be found with a small change):

if (input greater than maximum)

maximum = input // update max

2. Find the number of even integers entered.

To nd if the current input value is even or odd you can use the remainder operator with 2

(input % 2)

. If the result is 0, the number is even. Otherwise it is odd. The logic for this part

then is:

if (input is even) // that is input % 2 is 0

increase number of evens by 1

otherwise

increase number of odds by 1

3. Calculate the updated sum.

Add the current input to the sum and save the result back in the sum. You can get an idea from

section 4.7.1 or the sentinel average program example.

4. Increase the counter by one since one more input has been seen.

This is as done in the Average example in section 4.7.1 or the sample video with the counter line.

5. Finally, read in the next input value.

This updates the loop control variable to be ready to process the next input.

Part 4: After the Loop

This section goes after the close brace

'}'

of the while loop. Here we will print the results if any data

has been entered or print a message that

"No data was entered"

. The Average example in 4.7.1 and the

Sentinel Average video sample show checking to make sure input was entered.

1. If any data has been entered (What would the count be if only 0 was entered?)

Print out the results stating the max, min, number of integers entered, number even and odd.

(See sample output below)

Calculate the average, by dividing the sum by the count and display the results. Be careful of

integer division if both the sum and count are integer variables.

2.Otherwise, only the sentinel was entered, so output a warning that

"No data was entered"

.

Sample Output

Below is an example of what your output should roughly look like when this lab is completed. All text in

bold represents user input.

Sample Run 1:

Enter a series of integers (zero to quit):

3

-4

5

12

-7

0

The smallest integer is -7

The largest integer is 12

Total number of integers entered is 5

Total even numbers entered is 2

Total odd numbers entered is 3

The average value is 1.8

Sample Run 2:

Enter a series of integers (zero to quit):

-10

-2

-6

0

The smallest integer is -10

The largest integer is -2

Total number of integers entered is 3

Total even numbers entered is 3

Total odd numbers entered is 0

The average value is -6.0

You'll get a 25.0KB .DOC file.