I am supposed to be writing a CourseGrades class which helps store a set of four grades in an array. We are given a couple of pre-written files, but I need to write the CourseGrades class and demonstrate it using a main file. There are so many files that I don't understand properly which methods to use.
Here are the code files which I'll explain. Every file below besides the GradesMain and CourseGrades(that's what I've written so far) are files that were written and given to us. The assignment wants us to create each method which is in the CourseGrades file (like setLab,setEssay,etc.) should accept a specific object as it's argument and the object that's passed as an argument should already hold the student's score for the lab/essay/final exam/passfail exam. I don't understand how to store grades in the array if the array only holds objects of the GradedActivity class. Are there any hints anyone can give me at all? I know my CourseGrades is incomplete but I understand for the first method setLab, I could use the GradedActivity object and do obj.getScore()...but that's considered an incompatible type because I'm trying to store a double instead of an object?? Also I tried using obj.score the variable but it's considered private and hence I can't use it.
edit: I also chose to extend GradedActivity class, that wasn't required but I chose that because I thought that was the best one...though now I think it might be incorrect. Updated my main file which is supposed to demonstrate the CourseGrades class.
import java.util.Scanner; public class GradesMain{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); //this block is for the lab grade**************************** System.out.println("Please enter student's lab score: "); double lab = sc.nextDouble(); GradedActivity labGrade = new GradedActivity(); labGrade.setScore(lab); //use toString method to return grades //************************************************************* //this block is for the pass/fail exam************************ int questionsOnPFE; int questionsMissedOnPFE; double minPassingScore; System.out.println("Please enter the number of questions on the pass/fail exam: "); questionsOnPFE = sc.nextInt(); System.out.println("Please enter the number of questions the student missed on the pass/fail exam: "); questionsMissedOnPFE = sc.nextInt(); System.out.println("Please enter the minimum passing score for the pass/fail exam: "); minPassingScore = sc.nextDouble(); PassFailExam pfe = new PassFailExam(questionsOnPFE,questionsMissedOnPFE,minPassingScore); //************************************************************** //this block is for the essay grade******************************* System.out.println("Please enter student's essay score: "); double essay = sc.nextDouble(); GradedActivity essayGrade = new GradedActivity(); essayGrade.setScore(essay); //***************************************************************** //this block is for the final exam grade********************* int questionsOnExam; int questionsMissed; System.out.println("Please enter the number of questions on the final exam: "); questionsOnExam = sc.nextInt(); System.out.println("Please enter the number of questions the student missed on the final exam: "); questionsMissed = sc.nextInt(); FinalExam fExam = new FinalExam(questionsOnExam,questionsMissed); //************************************************************** } }
public class CourseGrades extends GradedActivity{ GradedActivity[] grades = new GradedActivity[3]; public void setLab(GradedActivity obj){ grades[0] = obj; } public void setPassFailExam(PassFailExam obj){ grades[1] = obj; //unsure what method to use } public void setEssay(GradedActivity obj2){ grades[2] = obj2; } public void setFinalExam(FinalExam obj){ grades[3]=obj; } public String toString(){ return ""; } }
/** A class that holds a grade for a graded activity. */ public class GradedActivity { private double score; // Numeric score /** The setScore method sets the score field. @param s The value to store in score. */ public void setScore(double s) { score = s; } /** The getScore method returns the score. @return The value stored in the score field. */ public double getScore() { return score; } /** The getGrade method returns a letter grade determined from the score field. @return The letter grade. */ public char getGrade() { char letterGrade; if (score >= 90) letterGrade = 'A'; else if (score >= 80) letterGrade = 'B'; else if (score >= 70) letterGrade = 'C'; else if (score >= 60) letterGrade = 'D'; else letterGrade = 'F'; return letterGrade; } }
/** This class determines the grade for a final exam. */ public class FinalExam extends GradedActivity { private int numQuestions; // Number of questions private double pointsEach; // Points for each question private int numMissed; // Questions missed /** The constructor sets the number of questions on the exam and the number of questions missed. @param questions The number of questions. @param missed The number of questions missed. */ public FinalExam(int questions, int missed) { double numericScore; // To hold a numeric score // Set the numQuestions and numMissed fields. numQuestions = questions; numMissed = missed; // Calculate the points for each question and // the numeric score for this exam. pointsEach = 100.0 / questions; numericScore = 100.0 - (missed * pointsEach); // Call the inherited setScore method to // set the numeric score. setScore(numericScore); } /** The getPointsEach method returns the number of points each question is worth. @return The value in the pointsEach field. */ public double getPointsEach() { return pointsEach; } /** The getNumMissed method returns the number of questions missed. @return The value in the numMissed field. */ public int getNumMissed() { return numMissed; } }
/** This class holds a numeric score and determines whether the score is passing or failing. */ public class PassFailActivity extends GradedActivity { private double minPassingScore; // Minimum passing score /** The constructor sets the minimum passing score. @param mps The minimum passing score. */ public PassFailActivity(double mps) { minPassingScore = mps; } /** The getGrade method returns a letter grade determined from the score field. This method overrides the superclass method. @return The letter grade. */ public char getGrade() { char letterGrade; if (super.getScore() >= minPassingScore) letterGrade = 'P'; else letterGrade = 'F'; return letterGrade; } }
/** This class determines a passing or failing grade for an exam. */ public class PassFailExam extends PassFailActivity { private int numQuestions; // Number of questions private double pointsEach; // Points for each question private int numMissed; // Number of questions missed /** The constructor sets the number of questions, the number of questions missed, and the minimum passing score. @param questions The number of questions. @param missed The number of questions missed. @param minPassing The minimum passing score. */ public PassFailExam(int questions, int missed, double minPassing) { // Call the superclass constructor. super(minPassing); // Declare a local variable for the score. double numericScore; // Set the numQuestions and numMissed fields. numQuestions = questions; numMissed = missed; // Calculate the points for each question and // the numeric score for this exam. pointsEach = 100.0 / questions; numericScore = 100.0 - (missed * pointsEach); // Call the superclass's setScore method to // set the numeric score. setScore(numericScore); } /** The getPointsEach method returns the number of points each question is worth. @return The value in the pointsEach field. */ public double getPointsEach() { return pointsEach; } /** The getNumMissed method returns the number of questions missed. @return The value in the numMissed field. */ public int getNumMissed() { return numMissed; } }