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 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 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.
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a java program that perform the following: 1. Ask a user ti enter 10 student...
Write a java program that perform the following: 1. Ask a user ti enter 10 student test score on a test (100 point test and save the score in an array 2. Iterate through the array to find the average and highest of these score, print them out 3 Count how many student score below average and print them out 4 . Instructor decide to curve the student score using square root curving method( take the square root of the...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT