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

JAVA PROGRAM 1. Please make a program to compute mean, median, and modus with th

ID: 3781862 • Letter: J

Question

JAVA PROGRAM 1. Please make a program to compute mean, median, and modus with the following conditions (submit the answer as Numbersort.java): A. The program consists of 4 menus: 1. Input Number 2. Ascending Sorting 3. Descending Sorting 4. Mean, Median, and Modus 5. Exit B. If user choose menu 1 (Input Number), thenthe program will: Ask user to input of long sequence number that he/she want to process. Validate theinput must be between 1 and 30. Ask user to input numbers as many as long sequence number that he/she input before. Then the program will return to the initial menu. C. If user choose menu 2 (Ascending Sorting), then the program will display sequence number that user input and sorting the number with ascending order D. If user choose menu 3 (Descending Sorting), then the program will display sequence number that user input and sorting the number with descending order E. If user choose menu 4 (Mean, Median and Modus), then the program will display mean, median, and modus from the numbers that he/she input before. Mean, median, and modus are processed with these following conditions Mean Sum of numbers Number of numbers Median o If number of numbers which user input before is odd, then Median the number in the middle of the set of the inputted numbers. o f number of numbers which user input before is even, then Median J average of two numbers that are located in the middle of the set of the inputted numbers. Modus the most frequently occuring value. Result from modus maybe more than one. If the number of occurring of each number is one then display a message "there is no modus in the sequence numbers" and sorting the modus by ascending order F. If user choose menu 5 (Exit), then the program will ends.

Explanation / Answer

import java.util.*;
public class Hw2 {

public static void main(String[] args) {
  
try
{
int [] arr=null;
int c;
Scanner sc= new Scanner(System.in);
  
System.out.println(" Menu:");
while(true)
{
System.out.println(" 1: Input No. 2: Ascending Order 3: Descending Order 4: Mean, Median, Modus 5: Exit");
c=sc.nextInt();

if(c==1)
arr=inputNumbers();
else if(c==2)
aesc(arr);
else if(c==3)
desc(arr);
else if(c==4)
mean_med_mod(arr);
else if(c==5)
break;
else System.out.println("Invalid Input");

}
}
catch (InputMismatchException exception)
{
System.out.println("The array entry failed. The program will now halt.");
}
}
  
public static int[] inputNumbers() {
  

Scanner sc= new Scanner(System.in);
int n,p;
System.out.print("Input length of sequence number [1 - 20] ");
n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
System.out.print("Eter the "+(i+1)+" number: ");
p=sc.nextInt();
if(p<1 || p> 30)
{
System.out.println("Invalid no "); i--;
}
else arr[i]=p;
}

return arr;
}
  
// Desc
public static void aesc(int[] arr) {

int t;
System.out.println(" Displaying the sequence in aesecding order");
for(int i=0;i<arr.length;i++)
{
for(int j=i;j<arr.length;j++)
{
if(arr[i] > arr[j])
{
t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}

  
}
  
for(int j=0;j<arr.length;j++)
{
System.out.print(arr[j]+" ");
}
}
public static void desc(int[] arr) {

int t;
System.out.println(" Displaying the sequence in descesending order");
for(int i=0;i<arr.length;i++)
{
for(int j=i;j<arr.length;j++)
{
if(arr[i] < arr[j])
{
t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}

  
}
  
for(int j=0;j<arr.length;j++)
{
System.out.print(arr[j]+" ");
}
}
public static void mean_med_mod(int[] arr) {

System.out.println(" Displaying Mean, Median and Modus of the sequence ");
float s=0;
for(int i=0;i<arr.length;i++)
{
s=s+arr[i];

}
System.out.println(" Mean of sequence is: "+s/arr.length);
  
// Sort the list
int t;
float med;
for(int i=0;i<arr.length;i++)
{
for(int j=i;j<arr.length;j++)
{
if(arr[i] > arr[j])
{
t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}

  
}
  
int mid=(arr.length/2) -1;
if(arr.length%2==0)
{

med= (arr[mid]+arr[mid+1])/2;
}
else med= arr[mid];
  
System.out.println(" Median of sequence is "+med);
  
  
////////Finding modus
  
int[] mod= new int[31];
for(int i=0; i<arr.length;i++)
{
mod[arr[i]]++;
}
  
int index=0, h=0;
for(int i=0; i<=30;i++)
{
if(mod[i] > h)
{
h=mod[i];
index=i;
}
}
  
if(h>1) System.out.println(" Modus of sequence is "+index);
else System.out.println(" There is no modus");
}

}