Question
I have a two part assignment. The first part asks you to write an application that lets a user input 5 numbers (I used an
I have a two part assignment. The first part asks you to write an application that lets a user input 5 numbers (I used an array). You then display the array how the user entered it, sort the array and display it, find the mean and median and display those. I did the first part of the assignment and it works and is technically acceptable I believe. The second part says to do the same thing but to allow up to 10 integers and and add a way to quit at any point - enter 9999 to quit. How can I implement this into my existing code without changing to much if at all possible? Also I know the median will need fixed because what I have wont work for an array that isn't 5 integers long so how can I fix that?
My JAVA code:
import java.util.Collections; import java.util.*; public class MeanMedian2 { public static void main(String[] args) { //create array int[] numbers = new int[10]; //get user input Scanner keyboard = new Scanner(System.in); //declare variables int a, b, temp; //allows user input for(a = 0; a < numbers.length; ++a) { System.out.print("Enter a whole number: "); numbers[a] = keyboard.nextInt(); } //blank line to look cleaner System.out.print(" "); //display unsorted System.out.print("Unsorted list: "); display(numbers, 0); //sorting method a = 1; while(a < numbers.length) { temp = numbers[a]; b = a - 1; while(b >= 0 && numbers[b] > temp) { numbers[b + 1] = numbers[b]; --b; } numbers[b + 1] = temp; ++a; } //display sorted System.out.print("Sorted list: "); display(numbers, a); //display method for mean mean(numbers); //display method for median median(numbers); } // all methods called upon public static void display(int[] numbers, int a) { for(int x = 0; x < numbers.length; ++x) System.out.print(numbers[x] + " "); System.out.println(); } public static void mean(int[] numbers) { //calculates average int meanAve = 0; int sum = 0; for(int num: numbers) { sum = sum + num; } meanAve = sum / numbers.length; System.out.println("The mean is: " + meanAve); } public static void median(int[] numbers) { //prints the 3rd array spot arrays start at 0 so 2 is really 3 System.out.print("The median is: " + numbers[2]); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started