Link to home
Start Free TrialLog in
Avatar of Bettsie Johnson
Bettsie Johnson

asked on

Showing Graded variable with Inheritance

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
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

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

Open in new window

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.
Avatar of Bettsie Johnson
Bettsie Johnson

ASKER

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.
User generated image
This might help. I've changed it as mentioned above and it's still producing the 0's and 'F' character.
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.
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.
Can you post your updated code?
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

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

Open in new window

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.
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.
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

Wow didn't notice that! See some input change now but the arithmetic needs to be re-worked so its not producing negatives.
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi I've tested it with a 5 different runs and the output is where it is suppose to be now.

Thanks!
Thank you for debugg. Works well naow.
Happy to help! Being an assignment, I'm glad that you worked through it, rather than just asking for the answer.