Question

In: Computer Science

Revise the MeanMedian class so that the user can enter any number of values up to...

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. Allow the user to enter 9999 to quit entering numbers.   

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

public class Median2
{
   public static double getMedian(double[] nums)
   {
       double median = 0.0;
       int[] middle = new int[2];
      
       if (nums.length % 2 == 0)
       {
           middle[0] = (nums.length / 2) - 1;   // nums.length counts from "1" instead of "0"
           middle[1] = middle[0] + 1;            // get the other middle value in an even length array
           median = nums[middle[0]] + nums[middle[1]];
           median = median / 2.0;
       }
       else
       {
           middle[0] = nums.length / 2;
           median = nums[middle[0]];
       }
      
       return median;
   }
  
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
      
       final byte LIMIT = 20;        // Maximum number of values to accept.
                                   // Is a byte because why not save a few bytes of memory
      
       int i = 0;                    // iterator for the input loop
      
       String userInput = "";
       ArrayList<String> numberList = new ArrayList<String>();
      
       double[] numbers;
      
       System.out.println("Please enter up to " + LIMIT + " numbers.\nTo stop just enter 9999");
       do
       {
           System.out.print("Number " + (i + 1) + "/" + LIMIT + ": ");
           userInput = input.nextLine().toLowerCase();
           if (!userInput.equals("9999"))
           {
               try
               {
                   Double.parseDouble(userInput);
                   numberList.add(userInput);
                   i++;
               }
               catch (Exception e)
               {
                   System.out.println("Error: " + userInput + " is not a valid number");
                   if (userInput.indexOf(' ') > -1)
                       System.out.println("Please use a single line for each number.");
               }
           }
       } while (!userInput.equals("9999") && i < LIMIT);
      
       if (numberList.size() == 0)
       {
           System.out.println("\nNothing was entered exiting...");
           return; // Kills program if not even one value was entered
       }
       else
       {
           numbers = new double[numberList.size()]; // Create an array the same size as the List
       }
      
       for(int c = 0; c < numberList.size(); c++)
           numbers[c] = Double.parseDouble(numberList.get(c).toString());           // Transfer list values to the array
      
       Arrays.sort(numbers);
      
       System.out.print("\n\nYou entered: ");
       for(Double num : numbers)
           System.out.print(num + " ");
      
       System.out.println("\nThe median value is " + getMedian(numbers));
      
       input.close();
   }
}

Kindly revert for any queries

Thanks.


Related Solutions

Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
4. Prompt user to enter a double number from console. Round this number so it will...
4. Prompt user to enter a double number from console. Round this number so it will keep 2 places after the decimal point. Display new number. For example, number 12.3579 will round to 12.36; number 12.4321 will round to 12.43 This is meant to be written in Java 14.0
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Write an application that allows a user to enter any number of student quiz scores, as...
Write an application that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average.
Write an application that allows a user to enter any number of student quiz scores until...
Write an application that allows a user to enter any number of student quiz scores until the user enters 99. If the score entered is less than 0 or more than 10, display an appropriate message and do not use the score. After all the scores have been entered, display the number of scores entered, the highest score, the lowest score, and the arithmetic average. Save the file as QuizScoreStatistics.java. I am struggling with looping in Java and do not...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
Im asked to edit the C++ code given to me so that the user can enter...
Im asked to edit the C++ code given to me so that the user can enter as many courses as they would like. I've already built the array to house the courses, but Im a tiny bit stuck on writing the user input to the array every time a new line Is made. Code: //CSC 211 Spring 2019 //Program Description: // // Originally From: // CSC 211 Spring 2018 // Dr. Sturm // This program... // #include<iostream> #include<string> #include<fstream> using...
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
Provide two text boxes for the user to enter in 2 values. Once the user clicks...
Provide two text boxes for the user to enter in 2 values. Once the user clicks off the second box, the sum of the two numbers displayed as a header level 2. here is what i have , and i cant find a way to display the sum of the numbers in each box in the header along with the other text. <h1>Problem 6</h1> <input type="text" id="txt1" onkeyup="sum();" /> <input type="text" id="txt2" onkeyup="sum();" /> <h2 id="txt3" result=""> The sum of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT