
-----------------------------------
michaelp
Sat Apr 10, 2010 1:32 pm

How's my Java code?
-----------------------------------
I'm just beginning to learn Java, as a student who was going to participate in a regional programming competition has other commitments on the day of the competiton, so the teacher who is helping out the team is thinking about putting me in their place.
Most of the students, AFAIK, are at a higher level of programming skill than I am, and also use Java, which I have not looked at until yesterday. He suggested I look at Java, because if I got stuck and was not using Java, they would most likely not be able to help me with the problem.
So, this weekend, I am looking at Java. Because I have experience with C-style languages, it's been pretty easy so far.
If someone could critic the code for a solution I wrote for a problem, that would be great, as I'm not really sure if the things I am doing are the best way to do them.

The problem:
Write a subprogram that tracks marks for a test. The subprogram should ask the user for the number of marks to be entered. This entry should be checked for a valid number, or else give the user an error message and ask them to reenter the mark. Next, the user should begin to enter the marks (0-100). Any invalid marks should be rejected and an error message should appear and the user should reenter the mark. As the marks are entered they should be classified according to letter grades( A 100-80, B 79-70, C 69-60, D 59-50, F 49-0). Lastly, the subprogram should display the number of marks in each letter grade and overall class average.

My solution:
import java.io.*;
public class HelloWorldApp { 
	public static void main(String

Some things I'm wondering:
-Is Integer.parseInt() the correct way to read integer input from the command line?
-In this line:
[code]average = (float)total / (float)numMarks;[/code]
Is it necessary to cast total and numMarks to floats? I do this because I read something that said it would perform integer division because both operands are integers.
-Is the array use okay? (I am aware of ArrayList. Would ArrayList be the best way to keep track of data, or are there are other containers?)

Links to tutorials on Java would be nice as well.

Comments much appreciated.

-----------------------------------
unoho
Sat Apr 10, 2010 7:15 pm

RE:How\'s my Java code?
-----------------------------------
this is similar to array exercise we r doing in class :P
i wonder if we r frm the same school :)

-----------------------------------
michaelp
Sat Apr 10, 2010 7:32 pm

RE:How\'s my Java code?
-----------------------------------
Possibly. :P
I go to Thornlea SS. How about you?

-----------------------------------
rdrake
Sat Apr 10, 2010 8:38 pm

Re: How's my Java code?
-----------------------------------
-Is Integer.parseInt() the correct way to read integer input from the command line?Well, the cin.readLine() is actually doing the reading in from the command line.  Integer.parseInt() is indeed the correct way to grab an integer from a string.

-In this line:
You only really need to cast one of them.

-Is the array use okay? (I am aware of ArrayList. Would ArrayList be the best way to keep track of data, or are there are other containers?)Arrays are fine, magic numbers are not.  Instead of using 0, 1, etc, I'd use a enumeration.


enum Grades { A, B, C, D, F };

...

if( mark = 80 ) 
{ 
    marksIt's been a while since I've been subjected to Java, but that or something similar should work.

-----------------------------------
DemonWasp
Sat Apr 10, 2010 8:50 pm

Re: How's my Java code?
-----------------------------------
Critiques:

It's usually poor form to import something like java.io.*, though it is more expedient. It's usually better to spell out each import (java.io.BufferedReader, java.io.InputStreamReader...) separately. Competent IDEs (Eclipse is an excellent one) will do this for you. This is a minor consideration.
Integer.parseInt() will throw a NumberFormatException if the input isn't an integer (for example, "abcd" or "1.25"), which isn't caught by your code. You want to use a try { ... } catch ( NumberFormatException e ) { ... } block to deal with this.
Look into using java.util.Scanner, which is newer and provides a lot of helpful facilities like nextInt() and hasNextInt().
If you know the size of storage required, as you do in this problem, then arrays are great to use. If you don't necessarily know the size of the storage before you need the storage, then you want to use the List interface. ArrayList implements this interface, which means you can do either of the following:
Typically, your while ( count < numMarks ) loop would be implemented as a for loop, though this is a style consideration.
Your code can make use of the short-circuit properties of if statements - the first if/else if that evaluates to true is executed and no others. In the if ( mark >= 0 && mark 