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 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...
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,...
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.
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
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...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT