Problem: Data Analysis +---------------------+ This project involves working wit
ID: 3668479 • Letter: P
Question
Problem: Data Analysis +---------------------+ This project involves working with a set of discrete data and determining their average (arithmetic mean), median, mode, and range. In brief, these operations are explained below. Given a set of the following discrete numbers/grades: 60, 60, 78, 80, 89, 95 1. The "average" or "arithmetic mean" of the grades is the sum of all grades divided by the number of the grades. (60, 60, 78, 80, 89, 95) average = ------------------------------- = 77 6 2. The "median" is the value located in middle of the sequence. If the number of data is odd, the middle value is the median, but if the number of data is even, the average of the two middle values is the median. Since the number of data here is even, the median is (78 + 80) median = ------------- = 79 2 3. The "mode" is the value or grade that has a higher frequency which is 60 since it occurs twice more than any other grade. 4. The "range" is the difference between the highest and lowest values. The highest grade in our example is 89 and the lowest is 60. range = 89 - 60 = 29
Requirements +-----------------------+ You are to write a Java program that handles any number of grades and performs the data analysis operations outlined above. It would be a benefit to you if you add the standard deviation, but it is not required. The specific requirements your work must consider are the following. a. Read the input data from a file. The program must not assume a fixed length of data; it should determine the length dynamically. b. Display the result on a standard display monitor. c. It is not a requirement for this project to use classes, but you are encouraged. d. Use functions for each specific operations; for instance, int average(int[] grades, int size) int median(int[] grades, int size) ... e. Source code must be documented. f. Source code must be formatted consistently.
Explanation / Answer
public class HelloWorld
{
public static void main(String[] args)
{ //program start here
// Display the marks from Marks.class
//int[] marklist = Java1Assign2Task1Marks.getMarks(); //make array
int [] marklist ={30, 35};
System.out.println("The Grades: ");
for (int i = 0; i < marklist.length; i++)
if ((i + 1) % 30 == 0) //30 per line
System.out.println(marklist[i] + " ");
else
System.out.print(marklist[i] + " ");
//Begin main menu rendering and declarations
int choice;
String strChoice;
int running = 1; //using while (pre-test) loop, program must know it is running before actually running it-self
java.util.Scanner input = new java.util.Scanner(System.in); //initialize the input scanner (used for keyboard input later)
while(running == 1)
{ //while the program is meant to be running
System.out.println(" ########## MAIN MENU ########## "); // " " means: add new line
System.out.println("Please press a key corresponding to the option and press enter to continue:");
System.out.println("[1] Range [2] Mean [3] Median [4] Mode " + " [99] Exit Program.");
strChoice = input.nextLine(); //get input and move on when 'enter' is pressed
strChoice = strChoice.trim(); //remove spaces if user accidently put spaces in, ignore them
if(strChoice.length() == 0) //test for an empty line it's invalid
System.out.println("Select a choice:");
choice = Integer.parseInt(strChoice); //"parseInt" converts the string to a number
switch (choice)
{
case 1: //Range
System.out.println(" The Range is:");
System.out.println(calcRange(marklist));
break;
case 2: //Mean
System.out.println(" The Mean is:");
System.out.println(calcAverage(marklist));
break;
case 3: //Median
System.out.println(" The Median is:");
System.out.println(calcMedian(marklist));
break;
case 4: //Mode
System.out.println(" One of the the most common value(s) are(mode):");
System.out.println(calcMode(marklist));
break;
case 99: //exit
System.out.print("Ending program...");
running = 0;
//Thought of using "System.exit(0);" but that thought a while loop would be easier to use/maintain/control
break;
default: System.out.println("Invalid!"); //Number inputed doesn't respond to a choice.
}
if(running == 1)
{ // skip pressing enter on way out, it's not needed
System.out.print("Press enter to go back to main menu.");
input.nextLine(); //a pause is added so that user may review the result of last choice.
}
}
System.out.print("Ended"); //last statement in program.
}
public static int calcMax(int[] anArray)
{ //a method named 'calcMax"
int highestNumber = 0;
for (int i = 0; i < anArray.length; i++) //go through array passed to this method
if (anArray[i] > highestNumber) //if a bigger number is found, replace the highest number variable.
highestNumber = anArray[i];
return highestNumber; //give the highest number to the statement that called it
}
public static int calcMin(int[] anArray)
{ //similar to above
int lowestNumber = 100;
for (int i = 0; i < anArray.length; i++)
if (anArray[i] < lowestNumber)
lowestNumber = anArray[i];
return lowestNumber;
}
public static int calcRange(int[] anArray)
{
int range;
range = calcMax(anArray) - calcMin(anArray); //use the above two methods to find the range
return range;
}
public static int calcAverage(int[] anArray)
{
int total = 0;
for (int i = 0; i < anArray.length; i++)
total += anArray[i]; //get total
int average = total / anArray.length; //total divided by the number of elements yields the average.
return average;
}
public static double calcMedian(int[] anArray)
{
double median;
int[] sortedAnArray;
sortedAnArray = sortArray(anArray); //sort the array first without touching the original array
if(sortedAnArray.length % 2 == 1)
{ //% means divide by the number before it by the number it and the reminder is the result
median = sortedAnArray[Math.round(sortedAnArray.length / 2)];
//if the number of marks is odd then the middle is the median
}
else
{
median = ((double)sortedAnArray[(sortedAnArray.length / 2) - 1] + sortedAnArray[sortedAnArray.length / 2]) / 2;
//if the number of marks is even then the average of the two middle marks is the median
}
return median;
}
public static int calcMode(int[] anArray)
{
int mode = 0, highestCount = 0;
int[] counts = new int[100]; //make an array of the same size of values used, so that each value can be counted
for(int i = 0; i < anArray.length; i++)
counts[anArray[i]]++; //each each value
//find which value is the highest
for(int i = 0; i < counts.length; i++)
if (counts[i] > highestCount){
highestCount = counts[i];
mode = i;
}
return mode;
}
public static int[] sortArray(int[] anArray)
{
int[] sortedAnArray = new int[anArray.length];
System.arraycopy(anArray, 0, sortedAnArray, 0, anArray.length); //make duplicate array
java.util.Arrays.sort(sortedAnArray); //sort the new array
return sortedAnArray;
}
public static char[] grades()
{ //this method is designed to be changable as long as the rules below are followed.
char[] gradeLetters = {'A', 'B', 'C', 'D', 'E'};
// the lower boundary of the grade letter is the number below, in method "gradeDistn".
//eg for 'C' which is awarded for a grade between 74-65, the lower boundary is 65.
//the higher boundary is the number before the next.
//eg. 'C' is 74 because 'B' is 75, so 'C' must be one lower.
return gradeLetters;
}
}
output
Please press a key corresponding to the option and press enter to continue:
[1] Range
[2] Mean
[3] Median
[4] Mode
[99] Exit Program.
2
The Mean is:
32
Press enter to go back to main menu.
########## MAIN MENU ##########
Please press a key corresponding to the option and press enter to continue:
[1] Range
[2] Mean
[3] Median
[4] Mode
[99] Exit Program.
4
One of the the most common value(s) are(mode):
30
Press enter to go back to main menu.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.