Please keep the code as simple as possible for this! Arrays Lab : Parallel Array
ID: 3823691 • Letter: P
Question
Please keep the code as simple as possible for this!
Arrays Lab: Parallel Arrays:
As part of ‘Teach for America’, you have been recruited to teach in a remote Appalachian community. To help analyze students’ performances on an assessment exam, you must write a program that displays each student’s name and then prompts you for that student’s test score. The program will calculate the class average (mean). Additionally, for each student, your program will display the student’s name, his test score, and the deviation of that test score from the mean (the students score minus the average).
Currently there are seven students in your class named: Bashful, Doc, Dopey, Grumpy, Happy, Sleepy, and Sneezy.
Program Specifications: This program will require you to create 3 parallel arrays , one for the students’ names (data already supplied), one for their test scores (to be read-in from the keyboard), and one for their score's deviation from the mean (which you will calculate).
Please create methods to:
read-in the data from the keyboard
compute the class average
compute the deviation from the mean
display the output in 3 well-aligned columns
SAMPLE RUN:
Enter Doc’s score: 90
Enter Grumpy’s score: 50
Enter Happy’s score: 70
Enter Sleepy’s score: 40
Enter Dopey’s score: 30
Enter Sneezy’s score: 60
Enter Bashful’s score: 80
The average of the scores is 60.0
Name
Grade
Mean Deviation
Doc
90
(30)
Grumpy
50
(-10)
Happy
70
(10)
Sleepy
40
(-20)
Dopey
30
(-30)
Sneezy
60
(0)
Bashful
80
(20)
Recommendations for the Simplest Approach:
The most straightforward way to approach this program is to create 3 parallel arrays in your main method. (One array can be allocated for the student names, one for their scores, and one for the deviations from the mean).
Next you can create a method to handle the input, a method to compute the average, a method to handle the deviation calculation, and lastly a method to display the output. Please pass any needed arrays into the methods (from main) via the parameter list. (NOTE: You are not required to set up the output in table format, but just be sure to align columns so that the output is neat, well- organized and easily readable.)
Submission Guidelines: Please submit in the iCollege Assignments drop box:
The Java source code for your program (do not forget documentation(comments) where appropriate)
Screen shots of the program running
Please keep the code as simple as possible for this!
Name
Grade
Mean Deviation
Doc
90
(30)
Grumpy
50
(-10)
Happy
70
(10)
Sleepy
40
(-20)
Dopey
30
(-30)
Sneezy
60
(0)
Bashful
80
(20)
Explanation / Answer
Deviation.java
import java.util.Scanner;
public class Deviation {
static Scanner sc=null;
public static void main(String[] args) {
//Creating an String array
String names[]={"Doc","Grumpy","Happy","Sleepy","Dopey","Sneezy","Bashful"};
//calling the methods
double grades[]=getValues(names);
double deviation[]=calDeviation(names,grades);
display(names,grades,deviation);
}
//This method will display the output in table format
private static void display(String[] names, double[] grades,
double[] deviation) {
System.out.println(" Name Grade Mean Deviation");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]+" "+grades[i]+" "+deviation[i]);
}
}
//This method will calculate the deviation
private static double[] calDeviation(String[] names, double[] grades) {
double avg=calAverage(grades);
System.out.println(" The Average of the Score is :"+avg);
double deviation[]=new double[grades.length];
for(int i=0;i<names.length;i++)
{
deviation[i]=grades[i]-avg;
}
return deviation;
}
//This method will calculate the average of the grades[] array
private static double calAverage(double[] grades) {
double sum=0.0,avg=0.0;
for(int i=0;i<grades.length;i++)
{
sum+=grades[i];
}
return sum/grades.length;
}
//This method will read the grade values entered by the user
private static double[] getValues(String[] names) {
sc=new Scanner(System.in);
double grades[]=new double[names.length];
for(int i=0;i<names.length;i++)
{
System.out.print("Enter "+names[i]+"’s score: ");
grades[i]=sc.nextInt();
}
return grades;
}
}
_____________
Output:
Enter Doc’s score: 90
Enter Grumpy’s score: 50
Enter Happy’s score: 70
Enter Sleepy’s score: 40
Enter Dopey’s score: 30
Enter Sneezy’s score: 60
Enter Bashful’s score: 80
The Average of the Score is :60.0
Name Grade Mean Deviation
Doc 90.0 30.0
Grumpy 50.0 -10.0
Happy 70.0 10.0
Sleepy 40.0 -20.0
Dopey 30.0 -30.0
Sneezy 60.0 0.0
Bashful 80.0 20.0
____________Thank You