Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Averaging Grades Assignment

 
Brandon Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I also have this same assignment to do, and I'm stuck. this is what I have currently --

I was following the above code to get some idea's on how to do it, but then I got confused.



If someone can help me out and let me know where my error is that would be great!


Thanks in advance!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall split your question from this thread, because your question deserves a discussion to itself.

Yes, you are stuck. Where on earth did you get that BasketBall method from?
You are getting stuck because you are doing too much all at once. Is that BasketBall thing supposed to be a constructor? If so, it must not have a return type, and its name must be exactly the same as the class. I would suggest you comment out the method completely, and leave it out until you are sure the constructor will work. You will have to comment out the 5 calculate() calls in the main() method, too

That constructor won’t work, by the way. I shall give you a little time to work out why, before letting you out of yoru misery.

For commenting out, read on:
 
Brandon Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks for the reply.

I made some changes to the code, from what I can see, everything should be working correctly...? --



But now i'm getting the two following errors --

Grades.java:26: illegal start of expression
void Grades()
^
Grades.java:26: ';' expected
void Grades()
^
2 errors

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does your Grades constructor end? You're missing some brackets. But once you fix that, it'll just reveal some further problems...

Your finalMark declaration needs some work as well. It should be declared outside the constructor, but initialised inside it.

Your method on line 26 - is that meant to be another constructor (it isn't) or a method (in which case it's a bad choice of name)? And why do you define is as not having a return value and then return a value?

Also - check lines 16 to 19. You're assigning variables to themselves. You need to tell the compiler which are supposed to be the constructor arguments and which are instance variables. The compiler won't guess.

I'd suggest not trying to do so much at once. Start the class simple. Add a bit, and get it to compile and work. Otherwise you can get lost like this.


 
Brandon Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, did some changes again, It now will compile and run, but will only give me this as a return --

null has a Quiz score of 0.0, and a Midterm mark of 0.0, A final Exam mark of 0.0, for a final grade of 0.0
null has a Quiz score of 0.0, and a Midterm mark of 0.0, A final Exam mark of 0.0, for a final grade of 0.0
null has a Quiz score of 0.0, and a Midterm mark of 0.0, A final Exam mark of 0.0, for a final grade of 0.0
null has a Quiz score of 0.0, and a Midterm mark of 0.0, A final Exam mark of 0.0, for a final grade of 0.0
null has a Quiz score of 0.0, and a Midterm mark of 0.0, A final Exam mark of 0.0, for a final grade of 0.0


Here is the code that I changed....

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, may I refer you the fourth paragraph of my previous answer .
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already said that constructor would’t work

Those lines in the Grades method are too long, and I have had to insert new lines to make them legible. Don’t use tabs for indentation. That Grades() method doesn’t do what it says it does. It is doing several things, whereas a method ought to do one thing, despite what the original question said, and its name should reflect that. Grades() (apart from the capital G which is wrong) is not a helpful method name, and is close enough to the class name to cause confusion.
You ought to read the conditions in your if blocks very very carefully.
You ought to end a series of else if blocks with a plain else, and make sure that the blocks are arranged in the correct order. If the 1st block isn’t appropriate, the 2nd block should be tested and then control passes to the 3rd block if the test was false, etc., etc. The final block should be an else, which covers all cases which preceding blocks haven’t caught.

MB is right. You should be doing little bits and getting them working, rather than trying to change lots of code in the hope something will work.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the excellent advice already offered, I suggest: Break your code down into smaller, more focused tasks. The code that calculates the final mark should be separated from the code that assigns the letter grade. The code with the calls to system.out could all be consolidated into a single method that took as parameters the values that vary. These are all different tasks which will be more manageable if they are kept distinct.
 
Brandon Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finished it....


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic