Question

Write a JAVA program to read a list of nonnegative integers and to display the largest...

Write a JAVA program to read a list of nonnegative integers and to display the largest integer, the smallest integer and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest and average values. The average should be a value of type double so that it is computed a fractional part.

* Java program free from syntax, logic and run time errors

* Code optimisation: no unnecessary assignment of variables and calculations

* (one of them has 0 input). Descriptive block and inline comments included.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:
OUTPUT:

CODE:


//for taking input from the console
import java.util.Scanner;

//class name and public is for publically accesible
public class Main {

    /*
     * 1. public : it is a access specifier that means it will be accessed by
     * publically. 2.static : it is access modifier that means when the java program
     * is load then it will create the space in memory automatically. 3.void : it is
     * a return type i.e it does not return any value. 4.main() : it is a method or
     * a function name.
     */
    public static void main(String[] args) {

        // Integer.MAX_VALUE gives max integer value
        // Smallest set to Integer.MAX_VALUE, we don't have to worry about
        // any special case where smallest
        // doesn't have a value .It helps to avoid edge cases.
        int smallest = Integer.MAX_VALUE;
        int largest = 0, count = 0;
        double average = 0, total = 0;
        int number;
        // creating object of Scanner class
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter integers :");
        while (true) {
            // Taking input
            number = sc.nextInt();
            // when number is smaller than 0, stop taking input(break the loop)
            if (number <= -1) {
                break;
            } else {
                // storing total number of elements
                count = count + 1;
            }
            // if number is smaller than smallest, then put the number into smallest
            if (number < smallest) {
                smallest = number;
            }
            // if number is larger than largest, then put the number into largest

            if (number > largest) {
                largest = number;
            }

            total = total + number;
            // finding average by sum of elements by total numbers
            average = (total / count);
        }
        // closing the scanner object to ensure to prevent any resource leak
        sc.close();
        // printing the outputs
        System.out.println("The smallest number : " + smallest);
        System.out.println("The largest number : " + largest);
        System.out.println("The average is : " + average);
    }

}

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program to read a list of nonnegative integers and to display the largest...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads a string from keyboard and tests whether it contains a valid...

    Write a program that reads a string from keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be 1 from to a value that is appropriate...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program that first gets a list of integers from the input and adds them...

    Write a program that first gets a list of integers from the input and adds them to an array. The input begins with an integer indicating the number of integers that follow (Your program should work for any size of the array). Then, get the last value from the input, and output all integers less than or equal to that value by iterating through the array. Ex: If the input is: Enter the number of integers: 5 Enter integer 1:...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • IN JAVA Write a program that first gets a list of integers from input. The input...

    IN JAVA Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75...

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

  • Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long,...

    Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long, represents integers up to 9,223,372,036,854,775,807. Imagine that you want to represent even larger integers. For example, cryptography uses integers having more than 100 digits. Design and implement a class Huge of very large nonnegative integers. The largest integer should contain at least 30 digits. Use a deque to represent the value of an integer. Provide operations for the class that * Set the value...

  • Write a program named program44.py that can be used to determine the average of some integers....

    Write a program named program44.py that can be used to determine the average of some integers. The number of integers can vary, and this should not be initially set by user input (see sample output). Instead, use a while loop and a sentinel value of zero to cease integer input. Display the average accurate to two decimal places.

  • To make telephone numbers easier to remember, some companies use letters to show their telephone number....

    To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies must see more than seven letters. For example, 255-5466 can be displayed as CALL HOME, which uses eight letters. Write a program that prompts the user to enter a telephone expressed in letters and outputs the corresponding telephone number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT