Homework: Class to record Student Grades - java

Homework Question
Write a grading program for a class with the following grading policies:
a) 3 quizzes graded out of 10 points, weighted 25%
b) 1 midterm graded out of 100 points, weighted 35%
c) 1 final graded out of 100 points, weighted 40%
Your class requires a set of accessor and mutator methods, full constructor, default constructor, equals method, and a to String method. The class should have instance variables for the quizzes, midterm, and final only.
Then, write a tester program that should read the student's scores and output the student's scores and the student's record. as well as the student's overall numeric score for the entire course and final letter grade.
I keep getting errors no matter what I do! All the errors are "error: cannot find symbol" I would be very glad if someone could help me figure out what is wrong. I have spent hours on this.
These are my methods...
import java.util.Scanner;
public class StudentRecord
{
/***DECLARATIONS**/
private int quizScore1;
private int quizScore2;
private int quizScore3;
private int midtermScore;
private int finalScore;
/***ACCESSOR METHODS**/
public int getQuizScore()
{
return quizScore1;
return quizScore2;
return quizScore3;
}
public int getMidtermScore()
{
return midtermScore;
}
public int getFinalScore()
{
return finalScore;
}
/***MUTATOR METHODS**/
public void setQuizScore(int quizScore1, int quizScore2, int quizScore3)
{
if (quizScore1 < 0 || quizScore1 > 10)
{
System.out.println("Error: Invalid Quiz Grade");
}
if (quizScore2 < 0 || quizScore2 > 10)
{
System.out.println("Error: Invalid Quiz Grade");
}
if (quizScore3 <0 || quizScore3 > 10)
{
System.out.println("Error: Invalid Quiz Grade");
}
else
{
this.quizScore1 = quizScore1;
this.quizScore2 = quizScore2;
this.quizScore3 = quizScore3;
}
}
public void setMidtermScore(int midtermScore)
{
if (midtermScore < 0 || midtermScore > 100)
{
System.out.println("Error: Invalid Midterm Score");
}
else
{
this.midtermScore = midtermScore;
}
}
public void setFinalScore(int finalScore)
{
if (finalScore < 0 || finalScore > 100)
{
System.out.println("Error: Invalid Final Score");
}
else
{
this.finalScore = finalScore;
}
}
/***OTHER METHODS***/
public void studentRecord()
{
quizScore1 = 0;
quizScore2 = 0;
quizScore3 = 0;
midtermScore = 0;
finalScore = 0;
}
public void studentRecord(int quizScore1, int quizScore2, int quizScore3, int midtermScore, int finalScore)
{
System.out.println("Quiz Score 1: " + quizScore1);
System.out.println("Quiz Score 2: " + quizScore2);
System.out.println("Quiz Score 3: " + quizScore3);
System.out.println("Midterm Score: " + midtermScore);
System.out.println("Final Score: " + finalScore);
}
public boolean equals (StudentRecord otherStudentRecord)
{
return (quizScore1 == otherStudentRecord.quizScore1);
return (quizScore2 == otherStudentRecord.quizScore2);
return (quizScore3 == otherStudentRecord.quizScore3) ;
return (midtermScore == otherStudentRecord.midtermScore);
return (finalScore == otherStudentRecord.finalScore);
}
public int getGradeScore()
{
int gradeScore;
gradeScore = (((quizScore1 + quizScore2 + quizScore3) / 3) / 10 * 25) + (midtermScore / 100 * 35) + (finalScore / 100 * 40);
}
public char getLetterGrade()
{
int gradeScore;
char letterGrade;
if (gradeScore >= 90 && gradeScore <= 100)
{
return 'A';
}
else if (gradeScore >= 80 && gradeScore <= 89)
{
return 'B';
}
else if (gradeScore >= 70 && gradeScore <= 79)
{
return 'C';
}
else if (gradeScore >= 60 && gradeScore <= 69)
{
return 'D';
}
else
{
return 'F';
}
}
public String toString()
{
return ("Grade Score is " + gradeScore + "and letter grade is " + letterGrade);
}
}
...and this is my tester program...
import java.util.Scanner;
public class CalculatingStudentGrade
{
public static void main(String[] args)
{
Scanner keyboard;
int quizScore1, quizScore2, quizScore3, midtermScore, finalScore;
keyboard = new Scanner(System.in);
System.out.println("Enter Quiz 1 score:");
quizScore1 = keyboard.nextInt();
System.out.println("Enter Quiz 2 score:");
quizScore2 = keyboard.nextInt();
System.out.println("Enter Quiz 3 score:");
quizScore3 = keyboard.nextInt();
StudentRecord.getQuizScore();
StudentRecord.getMidtermScore();
StudentRecord.getFinalScore();
StudentRecord.setQuizScore();
StudentRecord.setMidtermScore();
StudentRecord.setFinalScore();
StudentRecord.StudentRecord();
StudentRecord.getGradeScore();
StudentRecord.getLetterGrade();
}
}

you can not return multiple statement here.I think it will be sum of all quiz. so make it sum and then return.
public int getQuizScore()
{
return quizScore1;
return quizScore2;
return quizScore3;
}
instead of
public int getQuizScore()
{
return quizScore1+quizScore2+quizScore3;
}

Related

How do I continue to loop until the user wants to quit?

Java
How do I continue to loop until the user wants to quit? I have tried inserting a do while loop but no matter where I put it, it doesn't seem to work. it keeps giving me an error message. I want to loop this until the user doesn't want to enter a score anymore. so it will continue to prompt for another letter grade until they type a "Y" or "N". Thank you!
import java.util.Scanner;
public class gradeConverter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score = 0;
int minimum = 0;
int maximum = 100;
int grade = 0;
System.out.println("Enter letter grade 0-100: ");
score = scan.nextInt();
if (!isValidNumber(score,1,100))
{
System.exit(0);
}
getLetterGrade(score);
}
private static boolean isValidNumber(int number, int minimum, int maximum)
{
return number > minimum && number<=maximum;
}
private static String getLetterGrade(int score)
{
String grade=null;
if(score >=80 && score <90) {
System.out.println("B");//java code tells you that your letter grade is a B if you input a score that is between and includes 90 and 80
grade = "B";
}
else if(score <= 100 && score >= 90) {
System.out.println("A");//java code tells you that your letter grade is a A if you input a score that is between and includes 100 and 90
grade = "A";
}else if(60>score){
System.out.println("F");
grade = "F";
}
return grade;
}
}
You can simply use while loop until the user wants to quit. Add a condition on user prompt input and if the user tries to quit, just break the loop.
Also, you're exiting the system if the user gives invalid input. What you can do is, you can continue the program to make the user give a valid input.
while (true) {
System.out.println("Enter letter grade 0-100: ");
score = scan.nextInt();
if (!isValidNumber(score, minimum, maximum)) {
System.out.println("The input is not in valid range!");
continue;
}
letterGrade = getLetterGrade(score);
System.out.println("The corresponding letter grade is: " + letterGrade);
if (!doContinue(scan)) {
break;
}
}
The do continue function just checks whether the user wants to continue or not. Returns a boolean:
private static boolean doContinue(Scanner sc) {
System.out.println("Do you want to continue? (y/n): ");
String input = sc.next();
return input.toLowerCase().equals("y");
}
Also, instead of printing the grades while checking condition, you can just return it, just as I've done in the above segment:
private static String getLetterGrade(int score) {
if (score >= 80 && score < 90) {
return "B";
} else if (score >= 90 && score <= 100) {
return "A";
} else if (score < 60) {
return "F";
} else {
return "Unknown Grade";
}
}
And I guess you can change the min max range too, for a proper practical input:
private static boolean isValidNumber(int number, int minimum, int maximum) {
return number >= minimum && number <= maximum;
}
import java.util.Scanner;
public class gradeConverter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char opt;
int score = 0;
int minimum = 0;
int maximum = 100;
int grade = 0;
do {
System.out.println("Enter letter grade 0-100: ");
score = scan.nextInt();
if (!isValidNumber(score, 1, 100)) {
System.exit(0);
}
getLetterGrade(score);
System.out.println("Do yu wish to continue?: ");
opt = Character.toLowerCase(scan.next().charAt(0));
} while (opt == 'y');
}
private static boolean isValidNumber(int number, int minimum, int maximum) {
return number > minimum && number <= maximum;
}
private static String getLetterGrade(int score) {
String grade = null;
if (score >= 80 && score < 90) {
System.out.println("B");// java code tells you that your letter grade is a B if you input a score that
// is between and includes 90 and 80
grade = "B";
} else if (score <= 100 && score >= 90) {
System.out.println("A");// java code tells you that your letter grade is a A if you input a score that
// is between and includes 100 and 90
grade = "A";
} else if (60 > score) {
System.out.println("F");
grade = "F";
}
return grade;
}
}
The do ... while loop works for this but you might want to refactor your variable declarations to change with newer scores being inputted.
I refactored your code a bit, and added the possibility to say a grade or N in the same field.
I also added the possibility to loop until an incorrect value or a N value is typed, using a simple while condition
Just have a look and feel free to modify it again
import java.util.Scanner;
public class gradeConverter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int minimum = 1;
int maximum = 100;
String grade;
boolean keep_going = true;
while(keep_going) {
System.out.println("Enter letter grade 0-100: ");
grade = scan.nextLine();
if (!isValidChoice(grade, minimum, maximum)) {
System.out.println("Invalid choice!");
keep_going = false;
break;
}
if(grade.toLowerCase().equals("n"))
keep_going = false;
else
getLetterGrade(Integer.parseInt(grade));
}
}
private static boolean isValidChoice(String value, int minimum, int maximum)
{
if(value.toLowerCase().equals("n"))
return true;
try {
int letter = Integer.parseInt(value);
return letter > minimum && letter<=maximum;
} catch (NumberFormatException e) {
return false;
}
}
private static String getLetterGrade(int score)
{
String grade=null;
if(score >=80 && score <90) {
System.out.println("B");//java code tells you that your letter grade is a B if you input a score that is between and includes 90 and 80
grade = "B";
}
else if(score <= 100 && score >= 90) {
System.out.println("A");//java code tells you that your letter grade is a A if you input a score that is between and includes 100 and 90
grade = "A";
}else if(60>score){
System.out.println("F");
grade = "F";
}
return grade;
}
}
You can scan in a loop and check the entered data for the need to exit it.
I also changed the type of the input value to support this check.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input;
int minimum = 0;
int maximum = 100;
int grade = 0;
System.out.println("Enter letter grade 0-100: ");
input = scan.nextLine();
while (!input.equals("y") || !input.equals("n")) {
int score = Integer.parseInt(input);
if (!isValidNumber(score,1,100)) {
getLetterGrade(score);
}
input = scan.nextLine();
}
System.exit(0);
}

Java code will not quit when using -1 and numbers do not transfer to alphabetic grades

When using -1 to quit it does not work and also it does not turn the grades from numerical or alphabetic. This is just a simple grade book for a college class.
public class Assignment3
{
public static void main(String[] args)
{`enter code here`
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt(String message, int minValue,int maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("The average score is: " + avgScore
+ " which equates to a " + avg);
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 void convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 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");
}
}
}
You need to fix you function calls and call the convertToLetter after the user inputs scores.
Try this code:
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
convertToLetter(avgScore);
System.out.println("The average score is: " + avgScore
+ " which equates to a " + avgScore);
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 void convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 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");
}
}
}
There was some logic error on your part. I have corrected the code.
This will convert the marks into grades and not run into any infinite loops too. Comment if you have any doubts.
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
promptForInt("", minValue, maxValue);
}
public static void promptForInt(String message,int minValue,int maxValue)
{
System.out.println(message);
//convertToLetter(double avg);
// Declaring variables for inital value in the loop
float sum = 0;
int numStudents = 0;
float score = 0;
boolean doneYet = false;
//Sentinel-controlled do-while loop, don't know how many times will run
do
{
System.out.printf("Enter the score for student #%d"
+ "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Float.parseFloat(keyboard.nextLine());
if (score != -1)
{
sum += score;
numStudents += 1;
}
}while (score != -1);
double avgScore = (double) sum / numStudents;
System.out.println("The average score is: " + avgScore
+ " which equates to a " + convertToLetter(avgScore));
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 String convertToLetter(double avg)
{
// Identifying the ranges for the grade letter
if (avg >= 90)
{
return "A";
}
else if (avg >= 80)
{
return "B";
}
else if (avg >= 70)
{
return "C";
}
else if (avg >= 60)
{
return "D";
}
else
{
return "F";
}
}
}

Java - Printing n times a string which is included in an ArrayList of objects

There's an exercise I'm doing which asks the user for an integer (points) until the user input is -1. It then wants me to display the average of these points, the average of the passing points (those >50), the pass percentage, and finally the grade distribution which is the part that I'm facing the problem.
The Grade Distribution part basically awards a grade from 0 to 5, based on how many points each input is. For example, an input integer <50 points will get a 0 grade, while one >50 && <60 will get the grade 1 etc. After the user has finished submitting the points, it then calculates how many people have scored a specific grade and distributes that number as a string (stars). So say that the user submits five times the points integer 49, there will be five stars at the grade 0.
After hours of trying, I managed to solve it but probably in the most unreliable way ever. I have three java files. One for the main, one for the class Points and one for the class Grades. In the Grades class, I just have two instance variables, a string(stars) and the integer grade. In the Points class, where I calculate the averages etc, I also create a new ArrayList and through a method I call in the main program, named createFiveGrades, I add 5 seperate Grades objects. Then through the method pointsToGrade (that I again call in the main) I convert the points to grades and add stars in the objects of the aforementioned ArrayList when applicable.
The class Points:
public class Points {
private int points;
private int counter;
private int passingPoints;
private int passingCounter;
private ArrayList<Grades> distr;
public Points() {
this.points = 0;
this.counter = 0;
this.passingPoints = 0;
this.passingCounter = 0;
this.distr = new ArrayList();
}
public void addPoints(int points) {
//code
}
public double totalPointsAverage() {
//code
}
public int getPassingPoints() {
//code
}
public double passingPointsAverage() {
//code
}
public double passPercentage() {
//code
}
public void createFiveGrades() {
this.distr.add(new Grades());
this.distr.get(0).setGrade(0);
this.distr.add(new Grades());
this.distr.get(1).setGrade(1);
this.distr.add(new Grades());
this.distr.get(2).setGrade(2);
this.distr.add(new Grades());
this.distr.get(3).setGrade(3);
this.distr.add(new Grades());
this.distr.get(4).setGrade(4);
this.distr.add(new Grades());
this.distr.get(5).setGrade(5);
}
public void pointsToGrade(int po) {
if (po < 50) {
this.distr.get(0).addStars("*");
} else if (po >= 50 && po < 60) {
this.distr.get(1).addStars("*");
} else if (po >= 60 && po < 70) {
this.distr.get(2).addStars("*");
} else if (po >= 70 && po < 80) {
this.distr.get(3).addStars("*");
} else if (po >= 80 && po < 90) {
this.distr.get(4).addStars("*");
} else if (po >= 90 && po <= 100) {
this.distr.get(5).addStars("*");
}
}
public ArrayList<Grades> distribution() {
return this.distr;
}
The class Grades:
public class Grades {
private int grade;
private String stars;
public Grades() {
this.grade = 0;
this.stars = "";
}
public void setGrade(int n) {
this.grade = n;
}
public void addStars(String str) {
this.stars += str;
}
public String toString() {
return this.grade + ": " + stars;
}
}
The main:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Points points = new Points();
points.createFiveGrades();
System.out.println("Enter points in totals, -1 stops:");
while (true) {
int input = Integer.valueOf(scanner.nextLine());
if (input == -1) {
System.out.println("Point average (all): " + points.totalPointsAverage());
if (points.getPassingPoints() > 0) {
System.out.println("Point average (passing): " + points.passingPointsAverage());
} else {
System.out.println("Point average (passing): -");
}
System.out.println("Pass percentage: " + points.passPercentage());
System.out.println("Grade distribution: ");
for (int i=points.distribution().size()-1; i>=0; i--) {
System.out.println(points.distribution().get(i));
}
break;
} else if (input >= 0 && input <= 100) {
points.addPoints(input);
points.pointsToGrade(input);
}
Question: What other ways would be better to implement the Grade Distribution feature of the program? Please do not give me the solution, but rather something to think of. Thanks!
Instead of using a custom class for the grades, you could simply use a map from the grade (Integer) to the number of occurrences of this grade (Integer). Then you can build the star string only at print time based on the values stored in the map.

Grade Check java

I wrote this program to convert a number grade into a letter grade but I keep getting the same errors. Could someone help me figure out what I'm doing wrong?
import static java.lang.System.*;
import java.util.Scanner;
public class Grade
{
private int numGrade;
public Grade()
{
Grade test;
}
public void setGrade(int grade)
{
numGrade = grade;
if (grade >= 90)
{
System.out.println("A");
}
{
System.out.println("B");
}
public String getLetterGrade( ) {
String letGrade="A";
if (grade>= 90)
{
return letGrade;
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
Seems like you tried to attack the same thing from many different positions.
first of lets start with converting numerical grades into letter grades, so before engaging to inputs, start with asking the kind of grade the user wishes to convert
char choise;
choise = reader.nextChar(); //ask for N or L for numerical or letter
next i'll show a sample code for letter to numerical convertion
public int getNGrade(char grade)
{
if (grade == 'A')
return 90;
else if (grade == 'B')
return 80; //and so on
}
same way can be used for the numerical to letter convertion
in the main class u call the function:
charGrade = reader.nextChar();
System.out.println("Your grade in numbers is " + getNGrade(charGrade));
i'm guessing that's what u meant, hope i was helpful.
Your code can be this and it works perfectly:
public class Grade {
private int numGrade;
public Grade(int grade) {
numGrade = grade;
}
public int getGrade() {
return numGrade;
}
public void setGrade(int grade) {
numGrade = grade;
}
public String getLetterGrade() {
if(numGrade <0 || numGrade > 100) throw new IllegalArgumentException("No such a grade!");
else if(numGrade>=90) return "A";
else if (numGrade >= 80) return "B";
else if(numGrade >= 70) return "C";
else if(numGrade >= 60) return "D";
else return "F";
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
You can include in the same class a main method or create a separate class for testing:
public static void main(String[] args) {
Grade g = new Grade(75); //you can enter the grade manually or simply using a Scanner object
System.out.println(g);
}
The syntax used is incorrect.
Attached is a sample code to do the conversion
public class Grade {
private int numGrade;
public void setGrade(int grade) {
numGrade = grade;
if (grade >= 90) {
System.out.println("A");
} else {
System.out.println("B");
}
}
public String getLetterGrade() {
String letGrade = "B";
if (numGrade >= 90) {
return "A";
}
return letGrade;
}
public String toString() {
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
import java.util.Scanner;
class Tutorial {
public static void main(String args[]){
Scanner input = new Scanner(System.in); // calling Scanner method
String restart = "Y"; //initialising the restart variable
while (restart.equals("Y")) // testing the conditon(if Y is equals then it continues)
{
int grade;
System.out.println("WELCOME TO ABD GRADING SYSTEM.");
System.out.println("Enter your Score(between 1 - 100) : "); // Displaying a message on screen
grade = input.nextInt(); // Accept Input from the user
if(grade<=39)
System.out.println("Your grade is F9");
else if(grade==40 || grade<=49)
System.out.println("Your grade is D7");
else if(grade==50 || grade<=59)
System.out.println("Your grade is C6");
else if(grade==60 || grade<=69)
System.out.println("Your grade is C5");
else if(grade==70 || grade<=79)
System.out.println("Your grade is B2");
else if(grade==80 || grade<=100)
System.out.println("Your grade is A1");
else
{
System.out.println("Input Correct score between (1 - 100).");
}
System.out.println("THANK YOU.");
System.out.println("Would you like to Calculate again? Y/N ");
restart = input.next();
}
}
}

String Cannont Be Converted to Char

I do not understand why my program will not allow me to convert my variable into char. Any help would be greatly appreciated!!! I am getting a total of 7 errors all either based around my test score grades or around the test score keyboard entry.
public class lab13
{
public static void main(String[] args)
{
// declare variables
int average;
int sum;
int i;
char grade;
int testScore;
//Create a Scanner Object
Scanner keyboard= new Scanner(System.in);
sum = 0;
i = 1;
while (i <= 4)
{
System.out.print("Enter a test score:");
testScore=keyboard.nextLine();
if (testScore < 60)
{
grade =(F);
}
else
{
if (testScore < 70)
{
grade =("D");
}
else
{
if (testScore < 80)
{
grade =("C");
}
else
{
if (testScore < 90)
{
grade =("B");
}
else
{
grade =("A");
}
}
}
}
System.out.print("Test score:"+testScore);
System.out.print("Letter grade:"+grade);
sum = sum + testScore;
i = i + 1;
}
average = sum / 4;
System.out.print("Test score average = " + average);
} // close main
} // close lab13
A char constant can be expressed with '' like
grade = 'F';
and
grade = 'D';
and
grade = 'C';
and
grade = 'B';
and
grade = 'A';
char can only hold a single character, which is defined using single quotes, as in 'C'
Insted of keyboard.nextLine() use keyboard.nextInt() and for character value assignment directly use character with single quota like 'A' instead of ("A")
import java.util.Scanner;
public class lab13
{
public static void main(String[] args)
{
// declare variables
int average;
int sum;
int i;
char grade;
int testScore;
//Create a Scanner Object
Scanner keyboard= new Scanner(System.in);
sum = 0;
i = 1;
while (i <= 4)
{
System.out.print("Enter a test score:");
testScore=keyboard.nextInt();
if (testScore < 60)
{
grade ='F';
}
else
{
if (testScore < 70)
{
grade ='D';
}
else
{
if (testScore < 80)
{
grade ='C';
}
else
{
if (testScore < 90)
{
grade ='B';
}
else
{
grade ='A';
}
}
}
}
System.out.println("Test score:"+testScore);
System.out.println("Letter grade:"+grade);
sum = sum + testScore;
i = i + 1;
}
average = sum / 4;
System.out.print("Test score average = " + average);
} // close main
} // close lab13

Categories