File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes grade program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "grade program" Watch "grade program" New topic

grade program

John Swain

Joined: Feb 12, 2005
Posts: 16
Hello, I was wondering if someone could tell me how my program looks so far? If im on the right track or if its all hosed up?? This is what the program need to output:
There are two quizzes, each graded on the basis of 10 points.
There is one midterm exam, and one final exam, each graded on the basis of 100 points.
The final exam counts for 50 % of the grade, the midterm counts for 25%, and the two quizzes together count for a total of 25%.
I have to have several Accessor and Mutator methods, I have the Mutator Methods but im not sure how to use the Accessor Methods. All so were would I insert the doe to get the actual input from the keyboard?
Here is my code so far:

Please tell me what im doing right and wrong.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Does it compile?

If you calculate the results manually, do you get the correct answers with your application?

Usually class names begin with Capital Letters like GradesProgram rather than gradesProgram.

Java is case sensitive, so midtermExam is not the same as midTermExam.
[ April 01, 2005: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
John Swain

Joined: Feb 12, 2005
Posts: 16
I corrected alot of stupid mistakes, but there is one cant figure out.
It says "illegal start of expression" and point to this line "void getName()" I dont see the problem with it, all my other methods are the same? here is the updates code
I think everything else is ok, but i also have to use the return statement and im completly lost on how that would apply to this.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
The compiler is complaining because you defined a method inside another method.
John Swain

Joined: Feb 12, 2005
Posts: 16
I still dont understand, I thought i had the problem. I didnt have a { after the getLetterGrade method, so i placed one before the if/else if statment and still get the sane error. Or is it that i cant make the if/else if statment a method? More help please.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
It looks like you are missing a '}' at the end of your main method.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Horatio is correct and demonstrates that when the compiler points out an error, if you can't immediately see the problem, look backwards in the file to find the error. Often times a missing semi-colon or unmatched { } pair is the culprit.
Clay Adkerson

Joined: Mar 16, 2005
Posts: 27
I may be wrong as im a newbie to this stuff but one thing i noticed was where you had all your If statements, say the final score was a 85, the grade would be a D because it will change the grade everytime the If statement is true. I think they should be if-else loops. Just my 1 cent.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Horatio Westock:
It looks like you are missing a '}' at the end of your main method.

Well, that's half of it. Even if you stuck that brace in there, your compiler would still complain.

The main (so to speak) problem is, as Marilyn mentioned, you're defining all of your other methods inside the main() method.

Really, all of your methods, including main(), should be defined inside your class.

Once you fix that, you'll next run into some trouble with your variables, because they're inside main(), and so your other methods won't know anything about them. And then you'll find a logic error in at least one spot. Those are the most fun to figure out, IHMO.

If I were you, I'd start over, and do things in very small steps. In other words, write a program that gets the name and scores from the keyboard and simply prints them back out. When you've got that working, add the part that figures out what their semester average is, and prints that out. Make sure that works. Then add the part that figures out the letter grade. Make sure that works.

Or, if the input part is giving you trouble, you can just assign some scores to your variables in the code, temporarily. Get everything else working, then figure out the keyboard input last.

If you just punch in the whole big, giant program at once, and it doesn't work, it's probably because a gazillion things are wrong, and those things could be anywhere!

But if you do things in little tiny chunks, and if you compile, run, and test often, then when errors do come up, they will be relatively few, and you'll know that they're most likely in the last thing you added or changed.

Good luck!

- Jeff
[ April 02, 2005: Message edited by: Jeff Jetton ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I totally agree with Jeff that trying to write the whole program at once is typically a bad idea. As you see, you often get a boat load of compiler errors that are difficult to track down. Jeff suggested one way of breaking the program into little steps. Feel free to break it down in a different manner, but the idea is to get a little part working before moving to the next part. This means that you should write a FEW lines of code, compile it, run it, test it, then go back and write a FEW more lines of code. As a rule of thumb, you should probably write less than 10 lines of code at a time before attempting to compile your program.

Also, when I code, I find it best to type in sets of matching braces, parentheses, brackets, etc. FIRST and then go back and fill in the details. For example, I usually start by writing the class like this:

Notice that I'll type BOTH braces, then use the arrow keys to go back and fill in the main() method:

Notice that I again type in BOTH braces before going back to fill in the code inside of main(). I suggest that you get in the habit of ALWAYS doing this. It helps avoid errors due to mismatched braces.

You should also adopt some coding conventions AND STICK TO THEM RELIGIOUSLY. The exact conventions aren't the issue here; these will change over time and eventually will be dictated by a company's policy (if you are heading towards a coding job in your career, that is). However, consistent use of the coding convention is essential, in my opinion, to make your code readable, for yourself more than for others.

Probably the first two things you should decide in your coding convention is where to place the opening brace for a block of code. It looks like you are already consistently placing it on its own line. That's good.

Another coding convention deals with indentation. First you need to decide how large to make your indentation. I think Sun's suggested convention is 4-space indentation. Secondly, you need to figure out WHERE to indent. It's common practice to indent code inside a set of curly braces, but to leave the curly braces at the same level as any leading code (such as a method signature or an if statement). As an example, notice in my code above, the main method (which is inside the curly braces for the class) is indented one level and its curly braces are lined up with the method signature. If I were to insert code inside of main() it would be indented another level.

Adopting such conventions, and religiously sticking to them, will help you avoid many common errors, both at compile-time as well as run-time.

So my suggesting is just as Jeff says: start over and write a little bit of code at a time. As you do, follow some kind of conventions to help keep your code readable.

I apologize if this doesn't answer your questions directly. I hope that this helps you avoid bad programming practices now so you don't have to unlearn them later.

Keep Coding!


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: grade program
It's not a secret anymore!