Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 5, Problem 5.1PE
Expert Solution & Answer
Check Mark
Program Plan Intro

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.
      • Display the sum and average of integers.
Program Description Answer

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);

}

}

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
08:49
Students have asked these similar questions
Write in verilog coding language
Q4. Consider the following two design alternatives. Walmart Walmart Store locartion 1 * capacity - Associate Name 1 id position Design Alternative "A" Store locartion 1 capacity Associate Name 1 * id position Design Alternative "B" a) Explain the semantic differences between the two designs, if any. b) Explain the differences in how each design alternative may be implemented in Java. c) which design alternative may require more storage requirement in your opinion, and why?
Design a schematic for a compartmental model that includes compartments, flows, and parameters with their respective units, using Figure 1 as a guide. For each flow, determine whether it is best represented by a first-order transfer, a Michaelis-Menten saturable process, or a different method.

Chapter 5 Solutions

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)

Chapter 5.7, Problem 5.7.3CPChapter 5.7, Problem 5.7.4CPChapter 5.7, Problem 5.7.5CPChapter 5.7, Problem 5.7.6CPChapter 5.7, Problem 5.7.7CPChapter 5.8, Problem 5.8.1CPChapter 5.8, Problem 5.8.2CPChapter 5.8, Problem 5.8.3CPChapter 5.8, Problem 5.8.4CPChapter 5.9, Problem 5.9.1CPChapter 5.9, Problem 5.9.2CPChapter 5.11, Problem 5.11.1CPChapter 5.11, Problem 5.11.2CPChapter 5.11, Problem 5.11.3CPChapter 5.11, Problem 5.11.4CPChapter 5.11, Problem 5.11.5CPChapter 5.12, Problem 5.12.1CPChapter 5.12, Problem 5.12.2CPChapter 5.12, Problem 5.12.3CPChapter 5.12, Problem 5.12.4CPChapter 5.13, Problem 5.13.1CPChapter 5.14, Problem 5.14.1CPChapter 5, Problem 5.1PEChapter 5, Problem 5.2PEChapter 5, Problem 5.3PEChapter 5, Problem 5.4PEChapter 5, Problem 5.5PEChapter 5, Problem 5.6PEChapter 5, Problem 5.7PEChapter 5, Problem 5.8PEChapter 5, Problem 5.9PEChapter 5, Problem 5.10PEChapter 5, Problem 5.11PEChapter 5, Problem 5.12PEChapter 5, Problem 5.13PEChapter 5, Problem 5.14PEChapter 5, Problem 5.15PEChapter 5, Problem 5.16PEChapter 5, Problem 5.17PEChapter 5, Problem 5.18PEChapter 5, Problem 5.19PEChapter 5, Problem 5.20PEChapter 5, Problem 5.21PEChapter 5, Problem 5.22PEChapter 5, Problem 5.23PEChapter 5, Problem 5.24PEChapter 5, Problem 5.25PEChapter 5, Problem 5.26PEChapter 5, Problem 5.27PEChapter 5, Problem 5.28PEChapter 5, Problem 5.29PEChapter 5, Problem 5.30PEChapter 5, Problem 5.31PEChapter 5, Problem 5.32PEChapter 5, Problem 5.33PEChapter 5, Problem 5.34PEChapter 5, Problem 5.35PEChapter 5, Problem 5.36PEChapter 5, Problem 5.37PEChapter 5, Problem 5.38PEChapter 5, Problem 5.39PEChapter 5, Problem 5.40PEChapter 5, Problem 5.41PEChapter 5, Problem 5.42PEChapter 5, Problem 5.43PEChapter 5, Problem 5.44PEChapter 5, Problem 5.45PEChapter 5, Problem 5.46PEChapter 5, Problem 5.47PEChapter 5, Problem 5.48PEChapter 5, Problem 5.49PEChapter 5, Problem 5.50PEChapter 5, Problem 5.51PE
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License