HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple grade calculating application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatinggradesimpleapplication

Problem

This is a rather simple grade calculating application. Typical assignment or text book project. I've recently finished an introductory course to java, and have been continuously practicing by picking up random projects to see what I can do. I just recently made this Grade calculating project that I'm sure everyone is familiar with. I did this without looking up any help, and although the project is working, I'm looking to see how I can make this more efficient. Perhaps take out some lines of code. Any comments or suggestions will be greatly appreciated.

```
/* A PROGRAM THAT CALCULATES THE GRADE AVERAGE FROM AN ARRAY OF GRADES
* ENTERED BY THE USER, AND PRINTS WEATHER STUDENT PASSED OR FAILED AND
* THEIR RESPECTIVE GRADE.
*
* 1. ASK USER FOR THE NUMBER OF GRADES THEY WISH TO ENTER
* 2. CREATE AN ARRAY THE SIZE OF NUMOFGRADES ENTERED.
* 3. CREATE A DASH LINE AFTER NUMBERS HAVE BEEN ENTERED
* 4. CALCULATE THE SUM AND PRINT
* 5. CALCULATE THE AVERAGE AND PRINT
* 6. DETERMINE WHAT LETTER GRADE WAS ACHIEVED
* 7. PRINT WEATHER STUDENT PASSED OR NOT
* 8. PROMT USER IF THEY WANT TO ENTER MORE GRADES.
*/

import java.util.*;

public class GradeAverage {

public static Scanner kbd = new Scanner(System.in);

//Method that will calculate the sum
public static int sum( int[] gradeArray, int size){
int sum = 0;
int [] temp = gradeArray;//tem array is set equal to original array
for (int i = 0; i100 || grades[i]<0){
System.out.println("GRADES CAN NOT EXCEED 100 or BE LESS THAN 1");
i--;
}

}

total = sum(grades, numGrades);//calls on sum method
gradeAverage = average(grades,numGrades);//calls on average method

DashLine();//prints dashlines

System.out.print("\nThe Total Sum is: " + total + "\n");

System.out.printf("The Average is: %.2f \n", gradeAverage);

String ltrGrade;

ltrGrade= letterGrade(grad

Solution

try-with-resources

Since Java 7, using try-with-resources is recommended to safely and efficiently handle the underlying I/O resource for Scanner:

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        // pass the scanner around where user input is required
    }
}


UX

In my opinion, your prompts shouldn't be SHOUTING AT the user. Also, you shouldn't be enforcing negativity by suggesting YOUR [sic] PRETTY DUMB. I will suggest something more encouraging like "Please try harder the next time".

Java 8

In Java 8, getting statistics for a set of integers can be easily done via IntStream.summaryStatistics(). Hence, instead of looping through your inputs twice to perform the calculations manually, you can have something similar to:

// getInteger(String, Scanner) and getInteger(String, Scanner, int) 
// validates and gets positive integers from the user after prompting,
// with the optional int as the inclusive limit
int[] grades = new int[getInteger("Number of grades: ", scanner)];
for (int i = 0; i < grades.length; i++) {
    grades[i] = getInteger("Enter grade: ", scanner, 100);
}
IntSummaryStatistics summary = IntStream.of(grades).summaryStatistics();
System.out.println("Sum: " + summary.getSum());
System.out.println("Average: " + summary.getAverage());

Code Snippets

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        // pass the scanner around where user input is required
    }
}
// getInteger(String, Scanner) and getInteger(String, Scanner, int) 
// validates and gets positive integers from the user after prompting,
// with the optional int as the inclusive limit
int[] grades = new int[getInteger("Number of grades: ", scanner)];
for (int i = 0; i < grades.length; i++) {
    grades[i] = getInteger("Enter grade: ", scanner, 100);
}
IntSummaryStatistics summary = IntStream.of(grades).summaryStatistics();
System.out.println("Sum: " + summary.getSum());
System.out.println("Average: " + summary.getAverage());

Context

StackExchange Code Review Q#114932, answer score: 3

Revisions (0)

No revisions yet.