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
Bettsie JohnsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0
Jan LouwerensSoftware EngineerCommented:
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.
0
Bettsie JohnsonAuthor 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.
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Bettsie JohnsonAuthor 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.
0
Jan LouwerensSoftware EngineerCommented:
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.
0
Bettsie JohnsonAuthor 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.
0
Jan LouwerensSoftware EngineerCommented:
Can you post your updated code?
0
Jan LouwerensSoftware EngineerCommented:
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

0
Bettsie JohnsonAuthor 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

0
Jan LouwerensSoftware EngineerCommented:
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.
0
Bettsie JohnsonAuthor 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.
0
Jan LouwerensSoftware EngineerCommented:
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

0
Bettsie JohnsonAuthor Commented:
Wow didn't notice that! See some input change now but the arithmetic needs to be re-worked so its not producing negatives.
0
Jan LouwerensSoftware EngineerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bettsie JohnsonAuthor Commented:
Hi I've tested it with a 5 different runs and the output is where it is suppose to be now.

Thanks!
0
Bettsie JohnsonAuthor Commented:
Thank you for debugg. Works well naow.
0
Jan LouwerensSoftware EngineerCommented:
Happy to help! Being an assignment, I'm glad that you worked through it, rather than just asking for the answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.