Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application that allows you to enter five integers and displays the val

ID: 3556662 • Letter: W

Question

Write an application that allows you to enter five integers and displays the values, their mean, and their median. Save the file as MeanMedian.java.

b. Revise the MeanMedian class so that the user can enter any number of values up to 20. If the list has an even number of values, the median is the numeric average of the values in the two middle positions. Save the file as MeanMedian2.java.

This is what I have so far:

import java.util.*; public class MeanMedian { public static void main(String[] args) { int sum, avg, mode; final int MAX = 5; int[] num = new int[5]; Scanner keyboard = new Scanner(System.in); for (int i = 0; i < MAX; ++i) { System.out.println("Enter number "); num[i] = keyboard.nextInt(); } keyboard.close(); Arrays.sort(num); sum = (num[0]+num[1]+num[2]+num[3]+num[4]);//need to find an easier and shorter way to do this avg = (int)(sum/5); System.out.println("Numbers: " + num[0] + " " + num[1] + " " + num[2] + " " + num[3] + " " + num[4]);//and this System.out.println("Avg: " + avg); System.out.println("Median: " + num[2]);

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class MeanMedian
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int c, d, t,mean,median,n,sum=0;
        int[] array = new int[5];// set up array of 5
        int[] temp = new int[5];
       
        InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       System.out.println("enter size of array");
       try{
           String s = br.readLine();
           n= Integer.parseInt(s);
       }
       catch(IOException e){
           n=5;
       }
       for (int i = 0; i < n; i++) {
           System.out.println("Enter numbers into array."); // get a number
           int x; // number you entered
           try{
               String s = br.readLine();
               x = Integer.parseInt(s);
               array[i] = x; // put the number in the array
           }
           catch(IOException e){
               x = 0;
           }
       }
       //Duplicating the array to perform sorting in temporary array
       for(c = 0;c<n;c++){
           temp[c] = array[c];
       }
       //performing sorting in the array
       for (c = 1 ; c <= n - 1; c++){
       d = c;
       while ( d > 0 && temp[d] < temp[d-1]) {
           t = temp[d];
           temp[d] = temp[d-1];
           temp[d-1] = t;
           d--;
       }
       }
       System.out.println("the list of elements in array are");
       for (c = 0; c <= n - 1; c++) {
       System.out.println(array[c]);
       }
//Finding Mean value;
       for(c=0;c<n;c++){
           sum = sum + temp[c];
       }
        mean = sum/n;
       System.out.println("Mean of the array is "+mean);

       median = temp[n/2];
       System.out.println("Median of the array for size 5 is "+median);
   }
}

_____________________________________________________________________________________

//MeanMedian2.java

import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class MeanMedian2
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int c, d, t,mean,median,n,sum=0;
        int[] array = new int[20];// set up array of 20
        int[] temp = new int[20];
       
        InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       System.out.println("enter size of array");
       try{
           String s = br.readLine();
           n= Integer.parseInt(s);
       }
       catch(IOException e){
           n=20;
       }
       for (int i = 0; i < n; i++) {
           System.out.println("Enter numbers into array."); // get a number
           int x; // number you entered
           try{
               String s = br.readLine();
               x = Integer.parseInt(s);
               array[i] = x; // put the number in the array
           }
           catch(IOException e){
               x = 0;
           }
       }
       //Duplicating the array to perform sorting in temporary array
       for(c = 0;c<n;c++){
           temp[c] = array[c];
       }
       //performing sorting in the array
       for (c = 1 ; c <= n - 1; c++){
       d = c;
       while ( d > 0 && temp[d] < temp[d-1]) {
           t = temp[d];
           temp[d] = temp[d-1];
           temp[d-1] = t;
           d--;
       }
       }
       System.out.println("the list of elements in array are");
       for (c = 0; c <= n - 1; c++) {
       System.out.println(array[c]);
       }
//Finding Mean value;
       for(c=0;c<n;c++){
           sum = sum + temp[c];
       }
        mean = sum/n;
       System.out.println("Mean of the array is "+mean);

if(n%2 == 0){

       median = (temp[n/2]+temp[(n-1)/2])/2;

}

else {

median = temp[n/2];

}
       System.out.println("Median of the array is "+median);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote