Question

In: Computer Science

Write a java program that will ask the user to enter integers (use loops) until -100...

Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers.

Thanks!!

Solutions

Expert Solution

import java.util.*;
class FindAndDisplay{
    ArrayList<Integer> arr = new ArrayList<Integer>(); //ArrayList to store dynamic values
    public void input(){                              //To take inputs
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n!=-100){                            /*check -100 is entered or not if not then add it to list and enter another the number*/
            arr.add(n);
            n=sc.nextInt();   
        }
    }
    public int sum(){                          //Sum of the numbers
        int s=0;
        for(int i=0;i<arr.size();i++){
            s+=arr.get(i);
        }
        return s;
    }
    public int smallest(){                  //Smallest of numbers
        int minvalue=0;
        for(int i=0;i<arr.size();i++){
            if(i==0){
                minvalue=arr.get(i);
            }
            else{
                if(arr.get(i)<minvalue){
                    minvalue=arr.get(i);
                }
            }
        }
        return minvalue;
    }

    public int greatest(){                     //Greatest of numbers
        int maxvalue=0; 
        for(int i=0;i<arr.size();i++){
            if(i==0){
                maxvalue=arr.get(i);
            }
            else{
                if(arr.get(i)>maxvalue){
                    maxvalue=arr.get(i);
                }
            }
        }
        return maxvalue;
    }

    public double average(){                   //average of numbers
        int s=0;
        for(int i=0;i<arr.size();i++){
           s+=arr.get(i);
        }
        double d1,d2;
        d1=s;
        d2=arr.size();

        return d1/d2;
    }
}
public class driver{
    public static void main(String[] args) {
        FindAndDisplay f=new FindAndDisplay();
        System.out.println("Enter integers ,enter -100 to stop ");
        f.input();
        System.out.println("Results::");
        System.out.println("The Greatest of the numbers: "+f.greatest());
        System.out.println("The Smallest of the numbers: "+f.smallest());
        System.out.println("The Sum of the numbers: "+f.sum());
        System.out.println("The Average of the numbers: "+f.average());
        
    }
}

INPUT&OUTPUT:

Enter integers ,enter -100 to stop
100
400
120
230
254
650
-100
Results::
The Greatest of the numbers: 650
The Smallest of the numbers: 100
The Sum of the numbers: 1754
The Average of the numbers: 292.3333333333333

***Still have doubt ask in comment section***


Related Solutions

Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
Write a program that ask the user for three integers. Use two functions to determine the...
Write a program that ask the user for three integers. Use two functions to determine the largest and smallest number of the three. One function will determine the largest number, and the other function will determine the smallest number. (6 points) In c++ using functions.
Nice Number Programming: Nice program ask user to enter three integers from keyboard (java console), the...
Nice Number Programming: Nice program ask user to enter three integers from keyboard (java console), the three integers represent left bound, right bound and arbitrary digit ‘m’, where left bound is less than right bound. Program should print all nice numbers in the given range that doesn’t contain digit 'm'. The number is nice if its every digit is larger than the sum of digits which are on the right side of that digit. For example 741 is a nice...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT