Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 4: . A Full Example: Summing User Input The above code adds to the earlier example. The user inputs an initial value and if

Part 4:

.

A Full Example: Summing User Input

The above code adds to the earlier example. The user inputs an initial value and if it is not

0, we enter the loop body. The value is added to sum and then we ask the user for the next

value. If that value is not 0, this repeats. Once the user enters 0, the loop terminates and

we reach the next instruction after the loop, the output statement which outputs the sum of

all of the numbers entered.

Part 5: While Loop Exercise (Program6A.java)

Enter a full program using the code from part 4 (you will have to add any import statements,

class header, Scanner etc). Make sure that you understand how it works and add

appropriate comments. Save, compile and run it and test it under several situations (input

three non-zero values, input one non-zero value, input 0 initially, input both positive and

negative numbers). When you have tested the program a number of times successfully,

add the following enhancements.

(1) In addition to displaying the sum of the entered values, display the

number

of non-zero

values that were entered. You will need to add another variable, call it

count

, and

since any non-zero will execute the loop body, all you need to do is

count++;

in the

loop body to count yet another non-zero input. Output count after you output the sum.

(2)

If at least one non-zero value was entered, then also display the

average

of the non-

zero values.

This will require that you test to make sure count is not zero after the

loop and if it is not, then compute and output the average. If count is 0, output a

message such as average undefined.

(3) Add instructions to also count and display the number of user input values that are odd.

You can test whether

value

is odd by testing that

value % 2 is equal to 1

. This will

require yet another counter variable to count the number of odd inputs call it

oddcount

.

Initialize it to 0, and add an if statement in the loop body to test if value entered is odd

and increment this variable. Finally, add an output statement after the loop to output

the number of odd values input.

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer value (0 to quit): ");

int value = input.nextInt();

int sum = 0;

while (value != 0) {

sum += value;

System.out.print("Enter next value (0 to quit): ");

value = input.nextInt();

}

System.out.println("The sum of the entered values is " + sum);

The statement

sum += value;

is equivalent to the

statement

sum = sum + value;

CSC 260L: Java Programming Lab 6

(4) Add another variable which will be input prior to the loop which asks the user for a

target

number. With another if statement in the loop body, test if the input value equals

this target and if so, add one to

yet another counter variable

name this one

targetcount

.

After the loop, output both the target value and the number of times the user entered

target.

(5) Add another variable,

max

, initialized to

Integer.MIN_VALUE

, to store the largest

value entered. In the loop body, determine if the new input, value, is greater than max

(

value > max

) and if so, set max to the newly entered value. After the loop, output

the max value as part of your output.

Sample Input and Output

Lets assume you run the program entering 10 for target followed by this list of inputs: 5

10 11 1 3 8 10 4 1 6 7 10 7 0. The output of your program might look like this. Notice

that 0 will not be counted as part of the input.

The sum of the input values is 83

The number of inputs is 13

The average of the input values is 6.38

The number of odd values input is 7

The value 10 was input 3 times

The maximum value entered was 11

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago