SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:6 | ISBN:9780132830317 | Edition: 5

Question

Write a grading program for a class with the following grading policies:

a. There are three quizzes, each graded on the basis of 10 points.

b. There is one midterm exam, graded on the basis of 100 points.

c. There is one final exam, graded on the basis of 100 points.

The final exam counts for 40% of the grade. The midterm counts for 35% of the grade. The three quizzes together count for a total of 25% of the grade. (Do not forget to convert the quiz scores to percentages before they are averaged in.)

Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and any grade below 60 is an F. The program should read in the student’s scores and output the student’s record, which consists of three quiz scores and two exam scores, as well as the student’s overall numeric score for the entire course and final letter grade.

Define and use a class for the student record. The class should have instance variables for the quizzes, midterm, final, overall numeric score for the course, and final letter grade. The overall numeric score is a number in the range 0 to 100, which represents the weighted average of the student’s work. The class should have methods to compute the overall numeric grade and the final letter grade. These last methods should be void methods that set the appropriate instance variables. Your class should have a reasonable set of accessor and mutator methods, an equals method, and a toString method, whether or not your program uses them. You may add other methods if you wish.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package weightedgrades;


public class studentRecord { 
    private int quiz1;
    private int quiz2;
    private int quiz3; 
    private int midterm;
    private int exam;
    
    public String toString()
    {
        double numericGrade = finalWeightedGrade();
        return("Student Record: \nQuiz 1: " + quiz1 + " \nQuiz 2: " 
             + quiz2 + " \nQuiz 3: " + quiz3 + " \nMidterm: " + midterm + " \nFinal Exam:  " + exam
             + " \nClass Numeric Grade " + numericGrade
             + " \nLetter Grade: " + letterGrade(numericGrade));
    }   
    
    public void studentRecord()
    {   
        quiz1 = 0;
        quiz2 = 0;
        quiz3 = 0;
        midterm = 0;
        exam = 0;        
    }        
    
    public studentRecord(int q1, int q2, int q3, int mt, int ex)
    {   
        midterm = mt;
        exam = ex;
        
        if(q1 > 100 || q1 < 0)
        {    
            System.out.println("Invalid quiz score: must be out of 100");
            System.exit(0);
        }
        else
        {
            quiz1 = q1;
        }
        if(q2 > 100 || q2 < 0)
        {    
            System.out.println("Invalid quiz score: must be out of 100");
            System.exit(0);
        }
        else
        {
            quiz2 = q2;
        }   
        if(q3 > 100 || q3 < 0)
        {    
            System.out.println("Invalid quiz score: must be out of 100");
            System.exit(0);
        }
        else
        {
            quiz3 = q3;
        }
        if(mt > 100)
        {
            System.out.println("Invalid midterm score: must be out of 100");
            System.exit(0); 
        }
        else
        {
            midterm = mt;
        }
        if(ex > 100)
        {
            System.out.println("Invalid final Exam score: must be out of 100");
            System.exit(0); 
        }
        else
        {
            exam = ex;
        }     
    }
    
    public double getQuizGrade()
    {
        return((quiz1 + quiz2 + quiz3) / 3) * 0.25;
    }
    
    
    public double getMidterm()
    {
        return midterm * 0.35;
    }
    
    public double getExam()
    {
        return exam * 0.4;
    }
    
    public double finalWeightedGrade()
    {   
        
        return getQuizGrade() + getMidterm() + getExam(); 
    }
    
    public String letterGrade(double numericGrade)
    {
        if (numericGrade >= 90)
        {
            return "A";
        }
        else if (numericGrade < 90 && numericGrade >= 80)
        {
            return "B";
        }
        else if (numericGrade < 80 && numericGrade >= 70)
        {
            return "C";
        }
        else if (numericGrade < 70 && numericGrade >= 60)
        {
            return "D";
        }
        else
        {
            return "F";
        }
    }
}

 

package weightedgrades;

import java.util.Scanner;

public class WeightedGrades {
    
    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       
       System.out.println("Enter in the first quiz score (out of 100): ");
        int quiz1 = (input.nextInt());
       
       System.out.println("Enter in the second quiz score (out of 100): ");
       int  quiz2 = input.nextInt();
       
       System.out.println("Enter in the third quiz score (out of 100): ");
        int quiz3 = input.nextInt();
       
       System.out.println("Enter in the Midterm score (out of 100): ");
        int midterm = input.nextInt();
       
       System.out.println("Enter in the Final Exam score (out of 100): ");
        int exam = input.nextInt();
       
       studentRecord newGrade = new studentRecord(quiz1, quiz2, quiz3, midterm, exam);
       System.out.println(); 
       System.out.println(newGrade);
        
       
    }
}

 

0 0

Discussions

Post the discussion to improve the above solution.