In: Computer Science
Define a class for the student record. The class should instance variables for Quizzes, Midterm, Final and total score for the course and final letter grade.
The class should have input and output methods. The input method should not ask for the final numeric grade, nor should it ask for final letter grade. The classes should have methods to compute the overall numeric grade and the final letter grade. These two methods will be void methods that set the appropriate instance variables.
Make sure to add default constructor.
Your program should use all the described methods. You should have set of Accessor(getter) and mutator(setter) methods for all instance variables, whether your program uses them or not.
The program should read in the students’ scores two quizzes, midterm and final and display the student’s records, which consists of all the above +final number and final letter grade.
Overall numeric grade is calculated as 50%-final exam, 25%-midterm and two quizzes together count for 25%.
For letter grade
90-100 is A
80-89 is B
70-79 is C
60-69 is D
And any grade <60 is F grade
When taking input, quiz grades are out of 10 points and midterm and final are out of 100 points.
import java.util.Scanner;
class Student { //student class
Scanner sc = new Scanner(System.in);
private int quiz1; //all the variables are declared as private
private int quiz2;
private int midTerm;
private int finalTerm;
private double numericGrade;
private char letterGrade;
public Student() { //default constructor to initialize the variables
setQuiz1(0);
setQuiz2(0);
setMidTerm(0);
setFinalTerm(0);
setNumericGrade(0.0);
setLetterGrade('\0');
}
//getter and setter methods for all the private variables
public int getQuiz1() {
return quiz1;
}
public void setQuiz1(int quiz) {
this.quiz1 = quiz;
}
public int getQuiz2() {
return quiz2;
}
public void setQuiz2(int quiz2) {
this.quiz2 = quiz2;
}
public int getMidTerm() {
return midTerm;
}
public void setMidTerm(int midTerm) {
this.midTerm = midTerm;
}
public int getFinalTerm() {
return finalTerm;
}
public void setFinalTerm(int finalTerm) {
this.finalTerm = finalTerm;
}
//method to get marks in quizzes, mid term and final from user
public void marksInput()
{
System.out.println("Enter the marks you got in the first quiz");
setQuiz1(sc.nextInt());
System.out.println("Enter the marks you got in the second quiz");
setQuiz2(sc.nextInt());
System.out.println("Enter the marks you got in the Mid Term examination");
setMidTerm(sc.nextInt());
System.out.println("Enter the marks you got in the final term examinations");
setFinalTerm(sc.nextInt());
}
//getter and setter methods for letter grade and numeric grade
public void setLetterGrade(char letterGrade) {
this.letterGrade = letterGrade;
}
public void setNumericGrade(double numericGrade) {
this.numericGrade = numericGrade;
}
public double getNumericGrade() {
return numericGrade;
}
public char getLetterGrade() {
return letterGrade;
}
//method to calculate numeric grade and letter grade using getter and setter methods
public void calculate() {
//calculating numeric grade and storing the value in variable calc-
double calc = (0.5 * getFinalTerm()) +
(0.25 * getMidTerm()) +
((12.5/10) * getQuiz1()) +
((12.5/10) * getQuiz2());
setNumericGrade(calc); //setting the numeric grade
//determining the letter grade with the help of numeric grade-
if(getNumericGrade() >= 90 && getNumericGrade() <= 100)
setLetterGrade('A');
else if(getNumericGrade() >=80 && getNumericGrade() <90)
setLetterGrade('B');
else if(getNumericGrade() >= 70 && getNumericGrade() < 80)
setLetterGrade('C');
else if(getNumericGrade() >= 60 && getNumericGrade() < 70)
setLetterGrade('D');
else
setLetterGrade('F');
}
//method to show the final grade to the user
public void showGrade() {
System.out.println("Your final numeric grade is : " + getNumericGrade());
System.out.println("Your final grade is : " + getLetterGrade());
}
}
//class with main method
public class Main {
public static void main(String[] args) {
Student st = new Student();
st.marksInput(); //calling marksInput() method
st.calculate(); //calling calculate() method
st.showGrade(); //calling showGrade() method
}
}