I haven't used java for a while and for some reason I can't figure out how to initialize my methods and objects without errors. These are the two I'm having troubles with:
// setLetterGrades Method
public static setLetterGrades(ArrayList<Integer> scores){
ArrayList<Integer> grades = new ArrayList<Integer>();
for (int i = 0; i < scores.size(); i++){
if (score >= 90 && score < 101){
String grade = "A";
}
else if (score >= 80 && score < 90){
String grade = "B";
}
else if (score >= 70 && score < 80){
String grade = "C";
}
else if (score >= 60 && score < 70){
String grade = "D";
}
else if(score > 60 && score <= 0){
String grade = "F";
}
else {
String grade = "Invalid test score.";
}
grades.add(grade);
}
return grades;
}
I'm getting 'Syntax error, insert "EnumBody" to complete BlockStatement' on the first line:
public static setLetterGrades(ArrayList<Integer> scores){
Similarly, I'm getting 'Syntax error on token "public", new expected' on the first line of this object:
// GradeBook Object
public GradeBook(ArrayList<Integer> scores, testName){
String test = testName;
ArrayList<Integer> testScores = ArrayList<Integer> scores;
setLetterGrades();
}
How do I make this second one more "object-y" and correct the error? Thank you for the help!
You forgot a return type for the method:
public static ArrayList<String> setLetterGrades(ArrayList<Integer> scores){
And you forgot the new keyword for the ArrayList constructor. This ain't Kotlin!
ArrayList<Integer> testScores = new ArrayList<>();
But I think what you were trying to do is this:
ArrayList<String> testScores = setLetterGrades(scores);
// setLetterGrades Method
// Specifies the return type ## public static ArrayList<String> ##
public static setLetterGrades(ArrayList<Integer> scores){
// The grade variable is of type String, so we need a list of Strings.
ArrayList<Integer> grades = new ArrayList<Integer>();
for (int i = 0; i < scores.size(); i++){
// Define the score variable
if (score >= 90 && score < 101){
String grade = "A";
}
else if (score >= 80 && score < 90){
String grade = "B";
}
else if (score >= 70 && score < 80){
String grade = "C";
}
else if (score >= 60 && score < 70){
String grade = "D";
}
else if(score > 60 && score <= 0){
String grade = "F";
}
else {
String grade = "Invalid test score.";
}
// Here, when calling the grade variable, it is not initialized.
grades.add(grade);
}
return grades;
}
my answer
// setLetterGrades Method
public static ArrayList<String> setLetterGrades(ArrayList<Integer> scores){
ArrayList<String> grades = new ArrayList<String>();
String grade = "";
for (int i = 0; i < scores.size(); i++){
int score = scores.get(i);
if (score >= 90 && score < 101){
grade = "A";
}
else if (score >= 80 && score < 90){
grade = "B";
}
else if (score >= 70 && score < 80){
grade = "C";
}
else if (score >= 60 && score < 70){
grade = "D";
}
else if(score > 60 && score <= 0){
grade = "F";
}
else {
grade = "Invalid test score.";
}
grades.add(grade);
}
return grades;
}
and my opinion on the second
// GradeBook Object
public GradeBook(ArrayList<Integer> scores, testName){
String test = testName;
ArrayList<String> grades = setLetterGrades(scores);
}
Related
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
String message = "Welcome to Simple Gradebook!";
promptForInt(message, minValue, maxValue);
// Declaring variables for the loop & the sentinel variable
int score = 0;
boolean doneYet = false;
do
{
// Input Validation
if (score < minValue || score > maxValue)
{
System.err.printf("Invalid value. The acceptable range is"
+ " between %d and %d\n"
+ "Please try again\n", minValue, maxValue);
}
else
{
doneYet = true;
}
} while (doneYet == false);
}
public static int promptForInt(String message, int minValue, int maxValue)
{
// Declaring variables for the loop & the sentinel variable
int sum = 0;
int numStudents = 0;
int score = 0;
System.out.println(message);
//Creating the sentinel loop
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Integer.parseInt(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
} while (score != -1);
double avgScore = (double) sum / numStudents;
//Passing method to this method to convert grade to letter
convertToLetter(avgScore);
System.out.println("The average score is: " + avgScore
+ " which equates to a " + avgScore);
return 0;
}
public static char convertToLetter(double avg)
{
char avgScore = 0;
// Identifying the ranges for the grade letter
if (avgScore >= 90)
{
System.out.println("A");
}
else if (avg >= 80)
{
System.out.println("B");
}
else if (avg >= 70)
{
System.out.println("C");
}
else if (avg >= 60)
{
System.out.println("D");
}
else
{
System.out.println("F");
}
return avgScore;
}
}
How can I get my print statement to work to where it prints the average score and the correlating letter grade? Like this... “The average score is 90.5, which equates to an A”
My input validation is not working like it should for some reason. If a number lower that -1 or higher than 100 is entered, it should give the error message and begin the loop again.
You are not assigning and returning the grade letter from the function, convertToLetter correctly.
You are not printing the return value of the function, convertToLetter.
Write the function as shown below:
public static char convertToLetter(double avg) {
char gradeLetter;
// Identifying the ranges for the grade letter
if (avg >= 90) {
gradeLetter = 'A';
} else if (avg >= 80) {
gradeLetter = 'B';
} else if (avg >= 70) {
gradeLetter = 'C';
} else if (avg >= 60) {
gradeLetter = 'D';
} else {
gradeLetter = 'F';
}
return gradeLetter;
}
and then change the print statement as System.out.println("The average score is: " + avgScore + " which equates to a " + convertToLetter(avgScore));
You’re not doing anything with the result of convertToLetter(avgScore).
Assign the result to a variable:
char grade = convertToLetter(avgScore);
Then refer to that variable when printing:
System.out.println("The average score is: " + avgScore
+ " which equates to a " + grade);
—--
You have a similar problem when calling promptForInt(message, minValue, maxValue); you need to assign the result to a variable:
score = promptForInt(message, minValue, maxValue);
Another major bug you have is your input checking code. If score is not in range, your do while loop will loop infinitely because the value of score is not changed anywhere inside the loop.
Add this line after printing the error message:
score = promptForInt(message, minValue, maxValue);
—--
In general, you must realise that variables have scope. You assigning a value to the avgScore variable declared inside the convertToLetter method has no effect whatsoever on the variable of the same name declared in your main method - they are different variables, whose scope is limited to the method in which they are declared.
This code works. Explanations after.
import java.util.Scanner;
public class Calculat {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
String message = "Welcome to Simple Gradebook!";
System.out.println(message);
// Declaring variables for the loop & the sentinel variable
int score = 0;
int sum = 0;
int numStudents = 0;
while (true) {
score = promptForInt(keyboard, (numStudents + 1), minValue, maxValue);
if (score == -1) {
break;
}
sum += score;
numStudents++;
}
if (numStudents == 0) {
System.out.println("No data received.");
}
else {
double avgScore = (double) sum / numStudents;
char letter = convertToLetter(avgScore);
System.out.printf("The average score is: %.3f which equates to a %c%n",
avgScore,
letter);
}
}
public static int promptForInt(Scanner keyboard, int numStudent, int minValue, int maxValue) {
// Declaring variables for the loop & the sentinel variable
int score = 0;
boolean isValidScore = false;
// Creating the sentinel loop
do {
System.out.printf("Enter the score for student #%d (or -1 to quit): ", numStudent);
String line = keyboard.nextLine();
try {
score = Integer.parseInt(line);
if (score < minValue || score > maxValue) {
System.out.printf("Invalid value. Acceptable range is between %d and %d%n",
minValue,
maxValue);
}
else {
isValidScore = true;
}
}
catch (NumberFormatException xNumberFormat) {
System.out.printf("Not a number: %s%n", line);
}
} while (!isValidScore);
return score;
}
public static char convertToLetter(double avg) {
char avgScore = 0;
// Identifying the ranges for the grade letter
if (avg >= 90) {
avgScore = 'A';
}
else if (avg >= 80) {
avgScore = 'B';
}
else if (avg >= 70) {
avgScore = 'C';
}
else if (avg >= 60) {
avgScore = 'D';
}
else {
avgScore = 'F';
}
return avgScore;
}
}
Method promptForInt() should accept a single score from the user and return it. It should return only a valid score.
Method main() contains the loop for gathering up all the scores.
You need to check that at least one score was entered, otherwise you can't calculate the average.
Method convertToLetter() was always assigning the value zero to avgScore, essentially making avgScore the null character.
Here is a sample run of the program.
Welcome to Simple Gradebook!
Enter the score for student #1 (or -1 to quit): 66
Enter the score for student #2 (or -1 to quit): 66
Enter the score for student #3 (or -1 to quit): 68
Enter the score for student #4 (or -1 to quit): -1
The average score is: 66.667 which equates to a D
enter image description hereI'm working on the convertToLetterGrade portion. I'd like to figure out how to pass my array into the string in order to output the minimum and maximum grade as a letter, the comments here are helpful, and I also need the gpa passed through in order to output a letter grade for that as well
public class WesternGPACalc {
public static void main(String[] args) {
//TODO:
//declare an array to hold the grades you earned in your classes
//feel free to use fake values
//declare at least 4 grades
//use a 0.0 - 4.0 scale...
//4.0 = A, 3.5 = A/B
//3.0 = B, 2.5 = B/C
//2.0 = C
//1.0 = D
//0.0 = F
//for example...
double[] grade = new double[4];
grade[0] = 4.00; // A
grade[1] = 2.50; // B/C
grade[2] = 2.00; // C
grade[3] = 1.00; // D
//TODO:
//use a method to print the min and max grades as letter grades
//printGradeStats(grade);
printGradeStats(grade);
//TODO:
//declare an array to hold the credits each class was worth
//the indices must match the grade[i] indices for the class
//for example...
int[] credit = new int[4];
credit[0] = 3;
credit[1] = 3;
credit[2] = 3;
credit[3] = 3;
//TODO:
//use a method to calculate and print the GPA as both
//a number and a letter grade
//pass both the credit and grade arrays to the method
//printGPA(grade, credit);
printGPA(grade, credit);
}
//TODO:
//Finish this method which will accept an array of grades
//and print the min and max letter grade
private static void printGradeStats(double[] grade){
//TODO:
//First determine the min and max letter grade...
double minGrade = 0;
double maxGrade = 0;
for(int i = 0; i < grade.length; i++) {
if(grade[i] < minGrade) {
minGrade = grade[i];
}
if(grade[i] > maxGrade) {
maxGrade = grade[i];
}
}
//TODO:
//convert the min and max grades to a letter grade...
//using your convertToLetterGrade(grade) method
//For example...
//String maxLetterGrade = convertToLetterGrade(maxGrade);
String maxLetterGrade = convertToLetterGrade(maxGrade);
String minLetterGrade = convertToLetterGrade(minGrade);
//TODO:
//Output them...
System.out.println("Max grade earned in a class was " + maxLetterGrade);
System.out.println("min grade earned in a class was " + minLetterGrade);
}
//TODO:
//Finish this method which will convert
//a grade on the 4.0 scale and return a letter grade
//Use the following scale...
//A = 4.0
//4.0 > A/B >= 3.5
//3.5 > B >= 3.0
//3.0 > B/C >= 2.5
//2.5 > C >= 2.0
//2.0 > D >= 1.0
//F < 1.0
private static String convertToLetterGrade(double grade){
String letterGrade = "F";
return letterGrade;
}
//TODO:
//Finish this method which will accept an array of grades and credits
//and print the cumulative GPA as a letter grade
private static void printGPA(double[] grade, int[] credit){
//Recall...GPA is just a weighted average...
//Cumulative GPA is the sum of all grade points -- grade[i] * credit[i]
//divided by the sum of all credits[i]
double sumCredits = 0;
//TODO:
//Calculate cumulative GPA
for (int n = 0; n < credit.length; n++) {
sumCredits += credit[n];
}
double gpa = ((grade[0] * credit[0]) + (grade[1] * credit[1]) + (grade[2] * credit[2]) + (grade[3] * credit[3])) / sumCredits;
//TODO:
//Output Cumulative GPA as both a number and a grade
System.out.println("Cumulative GPA " + gpa);
System.out.println("Cumulative GPA as a letter grade: ");
}
}
you can achieve it by using if-else block as following
private static String convertToLetterGrade(double grade){
String letterGrade="";
if(grade == 4.0){
letterGrade="A";
}else if(grade >=3.5 && grade < 4){
letterGrade="A/B";
}else if(grade >= 3.0 && grade < 3.5){
letterGrade="B";
}else if(grade >= 2.5 && grade < 3.0){
letterGrade="B/C";
}else if(grade >= 2.0 && grade < 2.5){
letterGrade="C";
}else if(grade >= 1.0 && grade < 2.0){
letterGrade="D";
}else if(grade < 1.0){
letterGrade="F";
}
return letterGrade;
}
I see you have an answer, but let me enhance the code a little bit.
When you are calculating the cumulative GPA, You hard code the summation. You can actually use the same loop you have used to get the sumCredits for that, and it is the correct way to do it (Otherwise you can only check/consider 4 elements/grades).
double gpa = 0.0;
for (int n = 0; n < credit.length; n++) {
gpa += grade[n] * credit[n];
sumCredits += credit[n];
}
gpa /= sumCredits;
One thing in Logic,
double minGrade = 5.0; // Or any max value, because you are going to find minimum values that this default one
double maxGrade = 0;
for(int i = 0; i < grade.length; i++) {
if(grade[i] < minGrade) {
// if you set `double minGrade = 0` this if never becode true since every grade is greater than `0`
minGrade = grade[i];
}
if(grade[i] > maxGrade) {
maxGrade = grade[i];
}
}
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);
}
}
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
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 ints to it.
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
}
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.
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';
}
}
}
I'm trying to make a simple program that reads the user inputted integer and categorizes it based on the value until the counter reaches 70.
Code:
int counter = 0;
int value;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int i = 0;
Scanner scan = new Scanner(System.in);
while (counter != 70) {
value = scan.nextInt();
if (value >= 45 && value <= 55) {
a++;
counter++;
} else if (value >= 56 && value <= 66) {
b++;
counter++;
} else if (value >= 67 && value <= 77) {
c++;
counter++;
} else if (value >= 78 && value <= 88) {
d++;
counter++;
} else if (value >= 89 && value <= 99) {
e++;
counter++;
} else if (value >= 100 && value <= 110) {
i++;
counter++;
} else {
}
}
System.out.println(a + "-" + b + "-" + c + "-" + d + "-" + e + "-" + i);
Obviously, this code is inefficient and I know for a fact that there are less time consuming ways to replicate this. I feel pretty dumb right now so please tell me the most efficient way.
Also if doing a certain number of time, the I feel a for loop is clearer.
consider using an array of ints rather than a-i
int [] values = new int [6];
Scanner scan = new Scanner(System.in);
for (int counter = 0; counter < 70; counter++)
{
int value = scan.nextInt();
if (value >= 45 && value <= 55) {
values[0]++;
} else if (value >= 56 && value <= 66) {
values[1]++;
} else if (value >= 67 && value <= 77) {
values[2]++;
} else if (value >= 78 && value <= 88) {
values[3]++;
} else if (value >= 89 && value <= 99) {
values[4]++;
} else if (value >= 100 && value <= 110) {
values[5]++;
} else {
counter--; // out of bounds - try again
}
}
Try this.
int counter = 0;
int[] a = new int[6];
Scanner scan = new Scanner(System.in);
while (counter < 70) {
int value = scan.nextInt();
if (value >= 45 && value <= 110) {
a[(value - 45) / 11]++;
counter++;
}
}
System.out.print(a[0]);
for (int i = 1; i < a.length; ++i)
System.out.print("-" + a[i]);
System.out.println();
I've been thinking how to make this program with loops so this is what I came up with:
int min = 45;
int max = 55;
int counter = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(scan.nextInt());
}
while (min <= 100) {
for (int value : list) {
if (value >= min && value <= max) {
counter++;
}
}
System.out.print(counter + "-");
min += 11;
max += 11;
counter = 0;
I really have no idea if this allocates memory faster or not but to me, it looks tidier although it may not be efficient.
One way to improve readability is to change variables names:
a becomes from45to55
b becomes from56to66
You can also move repeated operations into one method:
public static void increment(int value, int counter) {
value++;
counter++;
}
And use try with resources block to close Scanner. Full code:
public static void main(String[] args) {
int counter = 0;
int value;
int from45to55 = 0;
int from56to66 = 0;
int from67to77 = 0;
int from78to88 = 0;
int from89to99 = 0;
int from100to110 = 0;
try (Scanner scan = new Scanner(System.in)) {
while (counter != 70) {
value = scan.nextInt();
if (value >= 45 && value <= 55) {
increment(from45to55, counter);
} else if (value >= 56 && value <= 66) {
increment(from56to66, counter);
} else if (value >= 67 && value <= 77) {
increment(from67to77, counter);
} else if (value >= 78 && value <= 88) {
increment(from78to88, counter);
} else if (value >= 89 && value <= 99) {
increment(from89to99, counter);
} else if (value >= 100 && value <= 110) {
increment(from100to110, counter);
} else {
}
}
}
}
public static void increment(int value, int counter) {
value++;
counter++;
}
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
System.out.println("best: ");
int best1 = best(grades);
System.out.print(best1);
// Print Grade
System.out.println("Grade: ");
char [] grade = (char[]) best(grades);
for (char i = 0; i < grade.length; i++) {
String output= " ";
output += "Student " + i + " score is " +
grades[i] + " and grade is " + grade + "\n";
}
}
private static char gradeLetter(int[] grades) {
char grade = 0;
for (int i = 0; i < grades.length; i++) {
if (grades[i] >= best(grades) - 10)
grade = 'A';
else if (grades[i] >= best(grades) - 20)
grade = 'B';
else if (grades[i] >= best(grades) - 30)
grade = 'C';
else if (grades[i] >= best(grades) - 40)
grade = 'D';
else
grade = 'F';
}
return grade;
}
public static int best(int[] grades) {
System.out.println("The best scores is: ");
int best = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
I am trying to show the output like this
Student 1 score is 40 and grade is C
Student 2 score is 55 and grade is B
Student 3 score is 70 and grade is A
Student 4 score is 58 and grade is B
But I am having problems with printing the gradeLetter method.
Perhaps this is what you are trying to do :
public class Hw7Pr2 {
public static void main(String[] args) {
int[] grades = { 40, 55, 70, 58 };
int best = best(grades);
System.out.println("The best scores is: " + best);
// Print Grade
System.out.println("Grade: ");
for (int i = 1; i <= grades.length; i++) {
char grade = gradeLetter(grades[i-1], grades);
String output = " ";
output += "Student " + i + " score is " + grades[i-1] + " and grade is " + grade + "\n";
System.out.println(output);
}
}
private static char gradeLetter(int grade, int[] grades) {
char charGrade = 0;
if (grade >= best(grades) - 10)
charGrade = 'A';
else if (grade >= best(grades) - 20)
charGrade = 'B';
else if (grade >= best(grades) - 30)
charGrade = 'C';
else if (grade >= best(grades) - 40)
charGrade = 'D';
else
charGrade = 'F';
return charGrade;
}
public static int best(int[] grades) {
int best = grades[0];
for (int i = 1; i < grades.length; i++) { //will save a compare operation
if (grades[i] > best)
best = grades[i];
}
return best;
}
}
char [] grade = (char[]) best(grades);
Its a invalid part in your code. You can not convert an int to char[] array.
Just wanted to show you some other way of looking at it...
import java.util.Scanner;
public class Hw7Pr2 {
public static void main(String[] args) {
// Search the array for min, max and average
int[] grades = {40, 55, 70, 58};
gradeStats(grades);
}
static void gradeStats(int[] array) {
final int A = 90;
final int B = 80;
final int C = 70;
final int D = 60;
int minimumValue = 100;
int maximumValue = 0;
double gradeSum = 0;
// Get max and min grades
for (int i = 0; i < array.length; i++) {
if (array[i] < minimumValue) {
minimumValue = array[i];
}
if (array[i] > maximumValue) {
maximumValue = array[i];
}
}
// Get sum of grades
for (int i = 0; i < array.length; i++) {
gradeSum = gradeSum + array[i];
}
for (int i = 0; i < array.length; i++) {
if (array[i] >= A) {
System.out.println("Grade \"" + array[i] + "\" is an A.");
} else if (array[i] >= B) {
System.out.println("Grade \"" + array[i] + "\" is an B.");
} else if (array[i] >= C) {
System.out.println("Grade \"" + array[i] + "\" is an C.");
} else if (array[i] >= D) {
System.out.println("Grade \"" + array[i] + "\" is an D.");
} else {
System.out.println("Grade \"" + array[i] + "\" is an F.");
}
}
System.out.println("The lowest grade is a " + minimumValue + ".");
System.out.println("The highest grade is a " + maximumValue + ".");
System.out.printf("The sum is %d.\n", (int) gradeSum);
System.out.println("The average is " + gradeSum / array.length + ".");
}
}