Reputation: 13
This is the prompt that I'm asked to follow.
Write a method called average that takes in an array of test grades and returns the letter grade of the class average. The grade ranges are as follows:
average >= 90 -> A
80 <= average < 90 -> B
70 <= average < 80 -> C
60 <= average < 70 -> D
average < 60 -> F
Use the following method header: public static char average(int[] grades)
This is the output example I'm supposed to follow.
How many grades do you want to enter? 10
Enter grade 1: 70 Enter grade 2: 87 Enter grade 3: 95 Enter grade 4: 80 Enter grade 5: 80 Enter grade 6: 78 Enter grade 7: 85 Enter grade 8: 90 Enter grade 9: 66 Enter grade 10: 89
The class average for the test is: B
This is what I have so far but I don't know how to include the method header stated above. public static char average(int[] grades)
import java.util.Scanner;
public class ClassAverage{
//Main Method
public static void main(String[] args){
//Variables
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b >= 90)
average = 'A';
else if(b <= 90 && b >= 80)
average = 'B';
else if(b <= 80 && b >= 70)
average = 'C';
else if(b <= 70 && b >= 60)
average = 'D';
else if(b <= 60)
average = 'F';
else
average = '?';
System.out.println("The class average for the test is: " + average);
}
}
Upvotes: 1
Views: 1441
Reputation: 9650
Another solution:
package test163;
import java.util.Scanner;
public class ClassAverage {
//Main Method
public static void main(String[] args) {
//Variables
char average;
int i;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
average = average(array);
System.out.println("The class average for the test is: " + average);
}
public static char average(int[] grades) {
int sum = 0;
for (int grade: grades) {
sum += grade;
}
int b = sum / grades.length;
if (b >= 90) {
return 'A';
} else if (b >= 80) {
return 'B';
} else if (b >= 70) {
return 'C';
} else if (b >= 60) {
return 'D';
} else {
return 'F';
}
}
}
Upvotes: 0
Reputation: 171
It is very simple code. My answer is
import java.util.Scanner;
public class AverageMethod {
public static void main(String[] args) {
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b<=100 && b>=0){
if(b<=100 && b >= 90)
average = 'A';
else if(b >= 80)
average = 'B';
else if(b >= 70)
average = 'C';
else if(b >= 60)
average = 'D';
else
average = 'F';
System.out.println("The class average for the test is: " + average);
}else{
System.out.println("Not in range");
}
}
}
I am also add my github uploaded coed for you. If you have any trouble please follow my githublink. Please study it for more clarification.
Upvotes: 0
Reputation: 8187
I've written some pseudocode to help you take the code you have now and encapsulate it in the method you are requesting.
You already have the pieces written, you just need to move them from your main
method to this new average
method and make any necessary changes that come with changing scope (declaring or renaming certain variables in the new method).
Then to get the desired output you can call the method from within main
by using average(gradeArray)
and using the char value returned in a print statement.
public static char average(int[] gradeArray) {
// Find the sum of grades in gradeArray
// Use this sum and the size of the array to compute the average
// Use logic to determine which letter grade to print out based on the average
// Return the average
}
Upvotes: 0
Reputation: 136
public class MainClass {
//Main Method
public static void main(String[] args) {
//Variables
char avg;
int i;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
avg = average(array);
System.out.println("The class average for the test is: " + avg);
}
private static char average(int[] array) {
final int sum = Arrays.stream(array).sum();
final int b = sum / array.length;
if (b >= 90) {
return 'A';
} else if (b <= 90 && b >= 80) {
return 'B';
} else if (b <= 80 && b >= 70) {
return 'C';
} else if (b <= 70 && b >= 60) {
return 'D';
} else if (b <= 60) {
return 'F';
} else {
return '?';
}
} }
See that i accepted all the marks into an array and passed that int array into a method where i calculated the sum and grade and returned grade
Upvotes: 1
Reputation: 4811
Your instructor wants you to create a method called average that could be called from within main.
Essentially, all you need to do to complete the assignment is wrap your code inside of that method. So main would call average having passed in the int
s to it.
Upvotes: 0