Question

In: Computer Science

Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user...

Java Program

Please Read all directions carefully

Write a method named smallToLarge that asks the user to enter numbers, then prints the smallest and largest of all the numbers typed in by the user and the average (rounded to 2 decimal places). You may assume the user enters a valid integer number for the number of numbers to read. Here is an example dialogue:

/* initialize smallest and largest variables with the 1st user input for Number */

How many numbers do you want to enter? 3 
Number 1: 7
Number 2: 11
Number 3: -2

Smallest = -2
Largest = 11

Average = 5.33

Solutions

Expert Solution

Java code:

import java.util.Scanner;
public class Main
{
    //defining smallToLarge function
    static void smallToLarge(){
        Scanner input=new Scanner(System.in);
        //asking for count of numbers
        System.out.print("How many numbers do you want to enter? ");
        //initializing min,max,count and sum of numbers
        int max,min,count=0,sum=0;
        //accepting total number of numbers
        int no=input.nextInt();
        //checking if total number of numbers is 0 or less
        if(no<=0)
        //exiting
            return;
        //printing Number 1
        System.out.print("Number 1: ");
        //accepting it
        int num=input.nextInt();
        //setting it as max
        max=num;
        //setting it as min
        min=num;
        //incrementing count
        count++;
        //setting it as sum
        sum=num;
        //looping from 2 to number
        for(int i=2;i<=no;i++){
            //asking for Number
            System.out.print("Number "+i+": ");
            //accepting it
            num=input.nextInt();
            //adding it to sum
            sum+=num;
            //incrementing count
            count++;
            //checking if current number is max
            if(num>max)
            //setting max as num
                max=num;
            //checking if current number is min
            if(num<min)
            //setting min as num
                min=num;
        }
        //printing Smallest
        System.out.println("\nSmallest = "+min);
        //printing Largest
        System.out.println("Largest = "+max);
        //printing Average
        System.out.printf("\nAverage = %.2f",((float)(sum)/count));
    }
    public static void main(String[] args){
        //calling smallToLarge function
        smallToLarge();
    }
}


Screenshot:


Input and Output:


Related Solutions

Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT