import java.util.Scanner; public class assignment5practice { public static void
ID: 3589889 • Letter: I
Question
import java.util.Scanner;
public class assignment5practice {
public static void main(String []args){
//Create a Scanner
Scanner input = new Scanner(System.in);
//Declaration of Variables
int min = 0;
int max = 0;
int sum = 0, i=1;
double average = 0;
//Ask for user input
System.out.println("Would you like to (1) input or (2) generate numbers");
int choice = input.nextInt();
//Prompt the user to select the sample size of numbers inputed or generated
System.out.println("How large of a sample size would you like?");
int sampleSize = input.nextInt();
//Input numbers
if (choice == 1) {
System.out.println("Please enter " + sampleSize + " numbers");
while (i<=sampleSize) {
sum=sum + i;
i++;
int number = input.nextInt();
}
}
}
}
Write a program that will calculate statistics from a series of numbers. The numbers will either be input by the user or randomly generated. The program will first ask whether the user wishes to input or generate the numbers. Regardless of the choice, the program will then ask how many numbers will be input or generated. The program will then either accept that many numbers from the user, or generate that many numbers. The range of the generated numbers needs to be between 1 and 100. The program will output the following after all values have been entered or generated: All the numbers The largest number of all values The lowest number of all values The sum of all values The average of all values If the user chooses to input values, you can assume all values input will be positive numbers. Assignment Requirements Create a flowchart of a loop used in your program. If you create multiple loops, you can choose which one you want to create a diagram of Create adequate pseudocode for each portion of the code, do not forget your program header The program needs to accept user input to determine whether numbers will be input or generated, then will accept another instance of user input to determine how many numbers will be input or generated The program needs to be in Java.
This is what i have now, i'm having trouble figuring out how to come up with the statistics part.
Explanation / Answer
Note:-------
As it is not mentioned above that whether numbers are integer or double or any other , so i have sonsidered them as integers.
import java.util.Scanner;
import java.lang.*;
class Test {
public static void main(String[] args) {
Scanner in;
Integer choice,n;
Boolean input = false;
in = new Scanner(System.in);
System.out.print("Press 1 to enter numbers and 2 to generate numbers:- ");
choice = in.nextInt();
System.out.print("How many number you want to enter or generate:- ");
n = in.nextInt();
Integer[] values = new Integer[n];
if(choice == 1) {
input = true;
}
if(input) {
System.out.println("Enter " + n + " Numbers");
for(Integer i=0;i<n;i++) {
values[i] = in.nextInt();
}
}
else {
System.out.println("Generating " + n + " Numbers");
for(Integer i=0;i<n;i++) {
values[i] = (int)(Math.random() * 100);
}
}
System.out.println("The Numbers are:---------");
for(Integer i=0;i<n;i++) {
// Print the array
System.out.println(values[i]);
}
Integer sum=0,avg=0,min=values[0],max=values[0];
for(Integer i=0;i<n;i++) {
if(values[i] > max)
max = values[i];
if(values[i] < min)
min = values[i];
sum += values[i];
}
avg = sum / n;
System.out.println("Maximum Number = "+ max);
System.out.println("Minumum Number = "+ min);
System.out.println("Sum of all the numbers = "+ sum);
System.out.println("Average of all the numbers = "+ avg);
in.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.