Inheritance trouble understanding how to use it?
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.
Code :
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);
//**************************************************************
}
}
Code :
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 "";
}
}
Code :
/**
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;
}
}
Code :
/**
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;
}
}
Code :
/**
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;
}
}
Code :
/**
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;
}
}
Re: Inheritance trouble understanding how to use it?
Quote:
Originally Posted by
orbin
... 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?
Sure, your answer is in your question.
Quote:
Originally Posted by
orbin
... 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??
Sounds good to me.
-edit- By "good" I mean, correct, you can not store a double in a slot for an object.
Quote:
Originally Posted by
orbin
Also I tried using obj.score the variable but it's considered private and hence I can't use it.
Is there another way to access the value of the variable from something with greater than private visibility?
Re: Inheritance trouble understanding how to use it?
I think I figured it out for the most part but I don't understand why I'm getting a nullpointerexception?
Here is my main and coursegrades
Code :
import java.util.Scanner;
public class GradesMain{
public static void main(String[] args){
CourseGrades cg = new CourseGrades();
System.out.println("Report of all grades in array: \n" + cg.toString());
}
}
Code :
public class CourseGrades{
private GradedActivity[] grades = new GradedActivity[3];
public void setLab(GradedActivity obj){
grades[0] = new GradedActivity();
grades[0].setScore(85);
}
public void setPassFailExam(PassFailExam obj){
grades[1] = new PassFailExam(10,2,70);
}
public void setEssay(GradedActivity obj2){
grades[2] = new GradedActivity();
grades[2].setScore(92);
}
public void setFinalExam(FinalExam obj){
grades[3]=new FinalExam(50,6);
}
public String toString(){
return "Lab Score: " + grades[0].getScore()
+ "\nLab Grade: " + grades[0].getGrade()
+ "\nPass/Fail Exam Score: " + grades[1].getScore()
+ "\nPass/Fail Exam Grade: " + grades[1].getGrade()
+ "\nEssay Score: " + grades[2].getScore()
+ "\nEssay Grade: " + grades[2].getGrade()
+ "\nFinal Exam Score: " + grades[3].getScore()
+ "\nFinal Exam Grade: " + grades[3].getGrade();
}
}
This is the output.
Code :
--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at CourseGrades.toString(CourseGrades.java:31)
at GradesMain.main(GradesMain.java:10)
Process completed.
Re: Inheritance trouble understanding how to use it?
java.lang.NullPointerException is saying you are trying to read a variable that has not been initialized.
If you read the error output closely it will tell you which variable and when.
Hope that helps!
Re: Inheritance trouble understanding how to use it?
I finished the program and demonstrated it, but I just wanted to say polymorphism is a tough concept to wrap your mind around! There's so many layers to keep track of. Thanks again.
Re: Inheritance trouble understanding how to use it?
Glad to hear you finished your project!
Before long, polymorphism will just feel natural in your code..
Keeping track of the many layers as your projects get bigger and bigger will remain easy, as long as you get your outline/pseudo-code down first. Allowing the outline to keep track of what the code is supposed to do will keep your mind on what you are working on rather than trying to keep the entire project in your head. Good luck!