Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program #1 (10 points): Design and implement a Java program for programming exer

ID: 3757778 • Letter: P

Question

Program #1 (10 points): Design and implement a Java program for programming exercise 5.1, page 192, from the textbook (name it PositiveNegative ). Design the program such it allows the user to re-run the program with a different inputs in the same run. Do cument your code, and organize and space the outputs as shown in the textbook. Use escape characters and formatting objects when applicable.

(5.1) (Count positive and negative numbers and compute the average of numbers)

Write a program that reads an unspecified number of integers, determines how many

positive and negative values have been read, and computes the total and average of

the input values (not counting zeros). Your program ends with the input 0. Display

the average as a floating-point number. Here is a sample run:

Enter an integer, the input ends if it is 0: 1 2 -1 3 0

The number of positives is 3

The number of negatives is 1

The total is 5.0

The average is 1.25

Enter an integer, the input ends if it is 0: 0

No numbers are entered except 0

Explanation / Answer

import java.io.*;
class PositiveNegative{
public static void main(String args[])throws Exception
{
int n,pos=0,neg=0;
double sum=0,avg=0;
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
System.out.println("Enter an integer, the input ends if it is 0:");
do
{
n=Integer.parseInt(br.readLine());
if(n<0)
neg++;
else
pos++;
sum = sum+n;
}while(n!=0);
avg=sum/((pos-1)+neg);
System.out.println(" The number of positives is "+ (pos-1));
System.out.println(" The number of negatives is "+ neg);
System.out.println(" The total is "+ sum);
System.out.println(" The average is "+ avg);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote