Computer Science Canada How's my Java code? |
Author: | michaelp [ Sat Apr 10, 2010 1:32 pm ] | ||||
Post subject: | 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: Quote: 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:
Some things I'm wondering: -Is Integer.parseInt() the correct way to read integer input from the command line? -In this line:
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. |
Author: | unoho [ Sat Apr 10, 2010 7:15 pm ] |
Post subject: | RE:How\'s my Java code? |
this is similar to array exercise we r doing in class ![]() i wonder if we r frm the same school ![]() |
Author: | michaelp [ Sat Apr 10, 2010 7:32 pm ] |
Post subject: | RE:How\'s my Java code? |
Possibly. ![]() I go to Thornlea SS. How about you? |
Author: | rdrake [ Sat Apr 10, 2010 8:38 pm ] | ||||
Post subject: | Re: How's my Java code? | ||||
michaelp @ Sat Apr 10, 2010 1:32 pm wrote: -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.
michaelp @ Sat Apr 10, 2010 1:32 pm wrote: -In this line:
You only really need to cast one of them.
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. michaelp @ Sat Apr 10, 2010 1:32 pm wrote: -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.
|
Author: | DemonWasp [ Sat Apr 10, 2010 8:50 pm ] | ||||
Post subject: | Re: How's my Java code? | ||||
Critiques:
Sun's Java Tutorials are very comprehensive, accurate, and easy to understand. You should probably also bookmark the Java API Addendum: if you are currently using Ready to Program, stop that immediately and get a better solution. Notepad + cmd.exe is a superior solution, though I would recommend something like Dr. Java or Eclipse or Netbeans or Notepad++ and the command-line. |
Author: | wtd [ Sun Apr 11, 2010 12:41 am ] | ||
Post subject: | RE:How\'s my Java code? | ||
Do you need both the if and the do...while? |
Author: | michaelp [ Sun Apr 11, 2010 9:19 am ] | ||
Post subject: | Re: How's my Java code? | ||
Thank you for all the comments. rdrake: The enumeration does not work exactly like that, because when putting Marks.A in the brackets, there is an error about it not being an integer, which I don't know how to solve. Instead, I used 5 constants. DemonWasp: -One reason why I used import java.io.* was because I wasn't really sure of the names of what I should import for using BufferedReader and InputStreamReader. -Thanks for the lists syntax, I'm sure it will come in handy in the future. -I used a counter and a while loop because I only wanted to increment count when a proper value was entered. I felt like using a for loop and decrementing the counter when a bad value was entered wasn't right/proper. -Currently, I'm using Ubuntu and use gedit and the command line. wtd: Not really. The reason why I did that was so that a new message pops up when invalid input is entered instead of the same prompt every time. My new code( for the part wtd mentioned ) looks like this:
|
Author: | DemonWasp [ Sun Apr 11, 2010 2:12 pm ] | ||
Post subject: | RE:How\'s my Java code? | ||
You can, for reference, leave out any part of the for loop. For example, you can manually increment:
|