Showing Graded variable with Inheritance

Bettsie Johnson
Bettsie Johnson used Ask the Experts™
on
Hello Experts at Experts Exchange!

This is an update to the original questions since I cannot find the thread.  There is a base file available that cannot be modified. This problem has to do with inheritance. The sample output is below. The exam score and grade is not reflecting the total amount of numericScore and is producing the same grade regardless of user input. The points each should look like 12.0 not 6.77777(unless small number) Your help would be greatly appreciated.

How many Essay questions are in the course? 10

How many Essay questions did the student miss? 4

Each question counts 10.0points.

The Grammer Score: 18.0

The Spelling Score: 12.0

The Correct Length Score: 12.0

The Content Score: 18.0

The exam score is 60.0

The exam grade is D

The Code BaseFile is here:

//Base Class
public class GradeActivity
{
	private double score;

	public void setScore(double s)
	{
		score = s;
	}
	public double getScore()
	{
		return score;
	}
	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;
	}
}

Open in new window


Attached are the driver and child classes. DriverClass.javaEssay.java
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jan LouwerensSoftware Engineer

Commented:
Your numericScore is not calculated correctly. You have "numericScore=numberCorrect-100;". It should be:
numericScore = numberCorrect * pointsEach;

Open in new window

Jan LouwerensSoftware Engineer

Commented:
Also, in your DriverClass, you have these lines:
GradeActivity exam= new GradeActivity();
Essay details= new Essay(questions, missed, grammarscore, spellingscore, lengthscore, contentscore);

Open in new window


You should not have both. Essay extends GradeActivity, so you only need the Essay object.

Get rid of your "exam" variable, and wherever you are using that variable, replace it with the "details" variable.

Author

Commented:
Hi Jan,

Not sure if I follow. The GradeActivity contains both the numeric and char values needed in order to display, which is why the class has been instanced in the driver.

If I change the methods not containing details to details, it will make the method disappear since getScore and getGrade are not in the child class.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Author

Commented:
grade and score error
This might help. I've changed it as mentioned above and it's still producing the 0's and 'F' character.
Jan LouwerensSoftware Engineer

Commented:
That's what Inheritance is. Everything in the base class is "inherited" by the extending class. So even though the Essay class does not explicitly implement those methods, they are "inherited" from the GradeActivity class.

Author

Commented:
True, which is why extends is use in the child class. I've changed the code as suggested but it's still producing 0's for numeric grades and F's for the char values.

It doesn't look that there is need of a super (getScore()); method needed in the child class.
Jan LouwerensSoftware Engineer

Commented:
Can you post your updated code?
Jan LouwerensSoftware Engineer

Commented:
You are still using the "exam" variable. You should not be instantiating a GradeActivity object at all. You only need one Essay object.

Remove this line from the code:
GradeActivity exam= new GradeActivity();

Then, everywhere you were using the "exam" variable after that, replace it with the "details" variable.
System.out.println("The exam score is: " + details.getScore()); //numericScore in SuperClass Exam
System.out.println("The exam grade is: " + details.getGrade()); //The char grade SuperClass Exam

Open in new window

Author

Commented:
Both exam and parent class has been removed(current) but is still producing same error.
Update
System.out.println("The exam score is: " +details.getScore());
System.out.println("The exam grade is: " +details.getGrade());

Open in new window

Jan LouwerensSoftware Engineer

Commented:
It will be much easier to help if you post your updated code with each comment. Otherwise I'm just guessing at what still could possibly be wrong.

It looks like there's still errors in your math.

Author

Commented:
Hi Jan,

The code is same except for the getScore and getGrade modified, along with the Class call removed.

Something is not connecting between getScore, which is perhaps why getGrade is not displaying correctly.
Jan LouwerensSoftware Engineer

Commented:
Ah, it looks like you lost this line from your Essay constructor (after you calculate numericScore):
setScore(numericScore); //setting numericScore from the superclass method.

Open in new window

Author

Commented:
Wow didn't notice that! See some input change now but the arithmetic needs to be re-worked so its not producing negatives.
Software Engineer
Commented:
These two lines are wrong:
numberPoints = numQuestions*numMissed; //Arithmetic for total points so far
numericScore=100-numberPoints; //Arithmetic of whole grade

Open in new window


Try this instead:
int numCorrect = numQuestions - numMissed;
numericScore = numCorrect * pointsEach;

Open in new window

Author

Commented:
Hi I've tested it with a 5 different runs and the output is where it is suppose to be now.

Thanks!

Author

Commented:
Thank you for debugg. Works well naow.
Jan LouwerensSoftware Engineer

Commented:
Happy to help! Being an assignment, I'm glad that you worked through it, rather than just asking for the answer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial