• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOP: Why is everything printing out as 0?  RSS feed

 
Ada Bradburry
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably something really simple, but I just can't seem to figure it out Any advice to send me in the right direction? (Also I'm not sure I've put the nested if else statements to determine the letter grade in the right place...)
The Class I coded:


And here's the given driver (can't change anything about it):

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just took a quick look, but it seems like you are assigning your input to local variables, which go away after the method, rather than to the fields with the same names.
 
Ada Bradburry
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! That definitely helped thank you. Any suggestions to fix the total score and final grade? They're printing out as 0.0 and just blank. I thought maybe using this.setTotalScore and this.getTotalScore would fix it, but I just get an "method setTotalScore in class StudentRecord cannot be applied to given types" :/
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The total score should be the sum of the scores of all the tests, right? Somewhere in your code there has to be something that adds the scores together and assigns the result to the totalScore field.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at lines 4 to 9. There you have a number of member variables declared in class StudentRecord. That's where you'll want to store the data that is entered.

Then, look at the readInput() method, lines 61 to 71. There you are declaring new, local variables named quizOne, quizTwo, midTermExam, finalExam. Those variables have the same names as some of the member variables.

In the readInput() method, you store the values in new, local variables, and not in the member variables. When the readInput() method ends, the local variables are thrown away, and so is the input that you've just put into them.

Instead of declaring new variables inside readInput(), write the values in the existing member variables. For example:


You have to call setTotalScore and setFinalGrade somewhere. Why do these methods have parameters 'inTotalScore' and 'inFinalGrade'? These parameters are not used at all, you don't need them.
 
Ada Bradburry
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all it's working perfectly now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!