Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem a (PA2a.java) Write a program that reads in five whole numbers from the user. The program will calculate and output the following statistics about

Problem a (PA2a.java) Write a program that reads in five whole numbers from the user. The program will calculate and output the following statistics about the five numbers:

The sum of all of the positive numbers (greater than zero).

The sum of all of the non-positive numbers (less than or equal to zero).

The sum of all five numbers.

The average of all of the positive numbers (greater than zero).

The average of all of the non-positive numbers (less than or equal to zero).

The average of all five numbers.

Note that your program must accept the five numbers in any order and cannot ask the user to enter the positive and non-positive numbers separately.

You should start by calculating the sum and average of all five numbers. Be sure you get that working correctly and then move on to doing the calculations for the positive and non-positive numbers. You will need many variables to keep track of all the values, sums, and averages. Don't forget that to calculate the average of a set of numbers you sum them all up then divide by the number of values you added together. For example, if your program gets 3 positive numbers and 2 non-positive numbers, then you will divide the sum of the positive numbers by 3 to get the average and then divide the sum of the non-positive numbers by 2 to get the average of those numbers. This means you will have to keep track of how many positive numbers you read in and how many non-positive numbers you read in. Also note that you will need to write a lot of very similar looking pieces of code. Think about how to solve the problem for one or two numbers and then go from there to calculating the statistics for all five numbers. Finally, for sums, you should output just the number (e.g. 5 or 10) without any decimals (the sum of a set of integers is a sum!). However, for all averages, you must format the output to have two decimal places (even if they are both 0), and round if necessary. The following represents a sample run of the program in which the user inputs on a single line -1 1 -2 2 -3 (without quotes). Note that the JUnit tests, and grading, will be very picky about exact spacing, spelling, capitalization, and number formatting.

Enter five whole numbers: -1 1 -2 2 -3

The sum of the 2 positive numbers: 3

The sum of the 3 non-positive numbers: -6

The sum of the 5 numbers: -3

The average of the 2 positive numbers: 1.50

The average of the 3 non-positive numbers: -2.00

The average of the 5 numbers: -0.60

Using If else statement or Loops

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions