patternjavaMinor
Student grades program
Viewed 0 times
gradesstudentprogram
Problem
I am just looking for code review and overall critique. I want to know what I can do better and how can I do things more efficiently in Java.
I need particular feedback on the use of my
```
/*
double[][] studentScores ={{90,85,75,85,95},
{85,60,65,95,65},
{75,70,80,85,90},
{95,70,75,80,100},
{75,45,80,60,70}};
String[] studentNames = {"Jakob Baloski",
"Lucinda Pavlov",
"Daniele Hanston",
"Yusef Goldstein",
"Leona Rhee"
};
Your app is going to generate the following output to the console:
Jakob Baloski
Highest Score = 95.0
Lowest Score = 75.0
Mean = 86.0 Grade:B
Mean (lowest dropped) = 88.75
-------------------------------------
Lucinda Pavlov
Highest Score = 95.0
Lowest Score = 60.0
Mean = 74.0 Grade:C
Mean (lowest dropped) = 77.5
-------------------------------------
Daniele Hanston
Highest Score = 90.0
Lowest Score = 70.0
Mean = 80.0 Grade:B
Mean (lowest dropped) = 82.5
-------------------------------------
Yusef Goldstein
Highest Score = 100.0
Lowest Score = 70.0
Mean = 84.0 Grade:B
Mean (lowest dropped) = 87.5
-------------------------------------
Leona Rhee
Highest Score = 80.0
Lowest Score = 45.0
Mean = 66.0 Grade:D
Mean (lowest dropped) = 71.25
-------------------------------------
++++++++++++++++++++++++++++++++++++++++++++
Assignment 1
Highest Score = 95.0
Lowest Score = 75.0
Mean = 84.0 Grade:B
Mean (lowest dropped) = 86.25
-------------------------------------
Assignment 2
Highest Score = 85.0
Lowest Score = 45.0
Mean = 66.0 Grade:D
Mean (lowest dropped) = 71.25
-------------------------------------
Assignment 3
Highest Score = 80.0
Lowest Score = 65.0
Mean = 75.0 Grade:C
Mean (lowest dropped) = 77.5
-------------------------------------
Assignment 4
Highest Score = 95.0
Lowest Score = 60.0
Mean = 81.0 Grade:B
Mean (lowest dropped) = 86.25
-------------------------------------
Assignment 5
Highest Score = 100.0
I need particular feedback on the use of my
ArrayList in my colsToRows method and then how I converted it back to regular arrays in Java. What better step could I have taken there?```
/*
double[][] studentScores ={{90,85,75,85,95},
{85,60,65,95,65},
{75,70,80,85,90},
{95,70,75,80,100},
{75,45,80,60,70}};
String[] studentNames = {"Jakob Baloski",
"Lucinda Pavlov",
"Daniele Hanston",
"Yusef Goldstein",
"Leona Rhee"
};
Your app is going to generate the following output to the console:
Jakob Baloski
Highest Score = 95.0
Lowest Score = 75.0
Mean = 86.0 Grade:B
Mean (lowest dropped) = 88.75
-------------------------------------
Lucinda Pavlov
Highest Score = 95.0
Lowest Score = 60.0
Mean = 74.0 Grade:C
Mean (lowest dropped) = 77.5
-------------------------------------
Daniele Hanston
Highest Score = 90.0
Lowest Score = 70.0
Mean = 80.0 Grade:B
Mean (lowest dropped) = 82.5
-------------------------------------
Yusef Goldstein
Highest Score = 100.0
Lowest Score = 70.0
Mean = 84.0 Grade:B
Mean (lowest dropped) = 87.5
-------------------------------------
Leona Rhee
Highest Score = 80.0
Lowest Score = 45.0
Mean = 66.0 Grade:D
Mean (lowest dropped) = 71.25
-------------------------------------
++++++++++++++++++++++++++++++++++++++++++++
Assignment 1
Highest Score = 95.0
Lowest Score = 75.0
Mean = 84.0 Grade:B
Mean (lowest dropped) = 86.25
-------------------------------------
Assignment 2
Highest Score = 85.0
Lowest Score = 45.0
Mean = 66.0 Grade:D
Mean (lowest dropped) = 71.25
-------------------------------------
Assignment 3
Highest Score = 80.0
Lowest Score = 65.0
Mean = 75.0 Grade:C
Mean (lowest dropped) = 77.5
-------------------------------------
Assignment 4
Highest Score = 95.0
Lowest Score = 60.0
Mean = 81.0 Grade:B
Mean (lowest dropped) = 86.25
-------------------------------------
Assignment 5
Highest Score = 100.0
Solution
I need particular feedback on the use of my ArrayList in my colsToRows method and then how I converted it back to regular arrays in Java. What better step could I have taken there?
The better step could be to just stick with either arrays or
Ignoring that though, your code is quite clean and readable.
Your
Defining a
I might also rename
You could rewrite
And as one last point, moving most of
would make it easier to reuse your code with different students, to run tests, and so on.
The better step could be to just stick with either arrays or
ArrayList, if that's feasible. Converting between them is adding complexity to your code. If studentScores was defined as List>, you could use the existing Collections.max and .min methods:for (List scores : studentScores) {
...
System.out.println("Highest score = " + Collections.max(scores));
...
}Ignoring that though, your code is quite clean and readable.
Your
max and min methods are perfectly idiomatic, although you may want to check that the array passed into them has .length > 0 before attempting to access index 0.Defining a
sum method could help reduce code duplicated between mean and meanLowDrop. You may also want to handle the case of empty arrays or of meanLowDrop receiving an array of size 1:public static double mean(double[] numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("Cannot take mean of empty array.");
}
return sum(numbers) / numbers.length;
}
public static double meanLowDrop(double[] numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("Cannot take mean of empty array.");
} else if (numbers.length == 1) {
return mean(numbers);
}
return (sum(numbers) - min(numbers)) / (numbers.length - 1);
}I might also rename
meanLowDrop to meanDropLowest or meanWithoutLowest.gradeLetter is perfect.You could rewrite
colsToRows to directly return a double[][] instead of a List>, to avoid the dance with .stream().mapToDouble... that you're currently doing when printing:public static double[][] colsToRows(double[][] arr) {
double[][] transposed = new double[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
transposed[i][j] = arr[j][i];
}
}
return transposed;
}And as one last point, moving most of
main into a separate method that looks something like this:public static printStats(double[][] studentScores, String[] studentNames)would make it easier to reuse your code with different students, to run tests, and so on.
Code Snippets
for (List<Double> scores : studentScores) {
...
System.out.println("Highest score = " + Collections.max(scores));
...
}public static double mean(double[] numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("Cannot take mean of empty array.");
}
return sum(numbers) / numbers.length;
}
public static double meanLowDrop(double[] numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("Cannot take mean of empty array.");
} else if (numbers.length == 1) {
return mean(numbers);
}
return (sum(numbers) - min(numbers)) / (numbers.length - 1);
}public static double[][] colsToRows(double[][] arr) {
double[][] transposed = new double[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
transposed[i][j] = arr[j][i];
}
}
return transposed;
}public static printStats(double[][] studentScores, String[] studentNames)Context
StackExchange Code Review Q#114610, answer score: 4
Revisions (0)
No revisions yet.