Concept explainers
(Count positive and negative numbers and compute the average of numbers) Write a
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
Count positive and negative numbers and compute the average of numbers
Program Plan:
- Include the required import statement.
- Define the class
- Define the main() method using public static main.
- Declare and initialize the required variables.
- Declare the input scanner.
- Read an input from the user.
- Using while loop, check whether the integer is “0” or not
- Check whether the integer is greater than “0”.
- If so, increment the positive counter.
- Check whether the integer is less than “0”.
- If so, increment the negative counter.
- Calculate the sum of integers.
- Read the next input.
- Check whether the integer is greater than “0”.
- Display the sum and average of integers.
- Define the main() method using public static main.
The below program is used to count number of positives and number of negatives which are presented as inputs and finally calculate its sum and average as follows:
Explanation of Solution
Program:
//import statement
import java.util.Scanner;
//class Excersise_1
public class Excersise_1 {
// main function
public static void main(String[] args) {
// declare and initialize the required variables
int count_Positive = 0, count_Negative = 0;
int counter = 0, sum = 0, integer;
// declare the input scanner
Scanner in = new Scanner(System.in);
// print the instruction
System.out.print("Enter an integer, the input ends if it is 0: ");
// read the integer value from user
integer = in.nextInt();
// using while loop, check the integer
while (integer != 0) {
// check if it is positive
if (integer > 0)
/* if so, increment the positive counter */
count_Positive++;
// check if it is negative
else if (integer < 0)
/* if so, increment the negative counter */
count_Negative++;
// calculate the total of integer
sum += integer;
// increment the counter
counter++;
// Read the next integer
integer = in.nextInt();
}
// check the counter is 0
if (counter == 0)
// if so, no inputs are read
System.out.println("No numbers are entered except 0");
else {
// print the number of positive integers
System.out.println("The number of positives is " + count_Positive);
// print the number of negative integers
System.out.println("The number of negatives is " + count_Negative);
// print the sum
System.out.println("The total is " + sum);
// print the overall average
System.out.println("The average is " + sum * 1.0 / counter);
}
}
}
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
The average is 1.25
Want to see more full solutions like this?
Chapter 5 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (8th Edition)
Electric Circuits. (11th Edition)
Concepts Of Programming Languages
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Problem Solving with C++ (10th Edition)
- What should the next three steps be in my machine based home security system after deployment and after the following current steps: Enhancing Security & Privacy Measures User Interface (UI) and Experience (UX) Improvement Machine Learning Model Refinement & Continuous Improvementarrow_forwardI am creating a machine learning home based security system, have completed initial deployment and in the following phases of the project: Expanding device compatibility and integration, preparing for cloud integration, and implementing system reduncancy and disaster recovery. What should the next three phases be?arrow_forwardHands-On Assignments Part II Assignment 1-5: Querying the DoGood Donor Database Review the DoGood Donor data by writing and running SQL statements to perform the following tasks: 1. List each donor who has made a pledge and indicated a single lump sum payment. Include first name, last name, pledge date, and pledge amount. 2. List each donor who has made a pledge and indicated monthly payments over one year. Include first name, last name, pledge date, and pledge amount. Also, display the monthly payment amount. (Equal monthly payments are made for all pledges paid in monthly payments.) 3. Display an unduplicated list of projects (ID and name) that have pledges committed. Don't display all projects defined; list only those that have pledges assigned. 4. Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges. 5. Display all pledges made before March 8, 2012. Include all column data from the DD PLEDGE table.arrow_forward
- Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + day_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forward
- Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning