Question

in JAVA program.Use top-down design to design and implement a program to ask the user to...

in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of 10. Also, error handling is not the purpose of this assignment, so you can assume that the user will enter an allowable number. Of course, this will result in wasted space being allocated for the array if the user only wants to store 3 integers. Further, we will be unable to handle lists larger than the pre-determined maximum of 10.

For example:

Hello! This program will produce descriptive statistics for a list of integers.

How many integers are in the list? (max: 10)

10

What are the integers?

4 6 8 3 10 4 9 3 4 3

the Mean is: 5.4

the Median is: 4 Bye!

Note: the Mean is the average value of the list (the sum of the values, divided by the number of values). The Median is the “middle” value of the list, after the list is sorted (if the list has an odd number of elements, then the median is the middle value. If the list has an even number of elements, the median is the average of the two middle numbers).

This application is just big enough that planning will help you get it done in a reasonable amount of time. If you simply start writing Java code, you may spend too much time thinking at the wrong level of abstraction. So don’t!

1. The top level description is given in the question description. Use top-down design (as in Q1) to list the tasks that you need to accomplish. Refine them as necessary, stepwise, until you reach a sensible topping point, where your design can be translated fairly literally to Java.

2. Before you implement, convert all your levels to pseudo-code algorithms. Add pre-conditions, post-conditions, return criteria to everything in your design. Think about how your steps work together. Consider correctness, efficiency, robustness, adaptability, and reusability (CERAR).

3. When you are happy with your design, implement your design by converting to Java. If your design work was good, you should not need a lot of time here.

4. Document your java code by adding in comments.

5. Test your program, by trying a few examples.

Note: Don't re-invent the wheel to sort, use one of the algorithms from the COMP1230. For example, one algorithm for sorting is as follows (recall: what sorting algorithm is this?). You can choose any sorting algorithm you have learnt to do this question.

Problem: sort the elements in an array in ascending order.

Pre-condition: an array arr with n distinct elements

Post-condition: the elements in arr are sorted in ascending order

Return: The sorted array, arr

Sort(arr,n)

for k ← 0 to n-1

position ← k

for i ← k+1 to n-1 by 1

if (arr(i) < arr(position)) position ← i

if (position != k)

temp ← arr(k) //The next three lines swaps

arr(k) ← arr(k) //arr(k) with arr(k)

arr(k) ← temp

return arr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class test{
   public static void main(String args[])throws IOException{
       System.out.println("Hello! This program will produce descriptive statistics for a list of integers.");
       System.out.println("How many integers are in the list? (max: 10)");
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       int n = Integer.parseInt(br.readLine()); //n is the number of elements, assumed less than 10
       double sum=0; //used to count the total sum of elements
       System.out.println("What are the integers?");
       String str=br.readLine(); //accepting as a string
       String s[]=str.split(" "); //splitting the string into an array of strings
       int a[]=new int[s.length];

       //type casting the string into integer type
       for(int i=0;i<s.length;i++){
           a[i] = Integer.parseInt(s[i]);
           sum+=a[i];
       }

       //Selection sort
       for(int k=0;k<n-1;k++){
           int position=k;
           for(int i=k+1;i<n;i++){
               if(a[i]<a[position])
                   position=i;
           }
           int temp = a[k];
           a[k] = a[position];
           a[position] = temp;
       }
      
       double median;
       if(n%2==0)//even number of elements
           median = (a[n/2]+a[n/2 - 1])/2.0;
       else //odd number of elements
           median = a[(n-1)/2];
       double mean=sum/n;
       System.out.println("The Mean is:"+mean);
       System.out.println("The Median is:"+median);

   }
}

First we accept the number of elements,n.

Then we accept the numbers.

We accept the input as one whole string first, which is split into an array of string(which are all numbers). This array is typecasted into integers later.

SImultaneously we add all the elements adn store the total in "sum".

Finally, we perform selection sort, as asked in the question.

We calculate mean and median according to given guidelines.

Add a comment
Know the answer?
Add Answer to:
in JAVA program.Use top-down design to design and implement a program to ask the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to...

    Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value as: mid...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT