PDA

View Full Version : JAVA help



§håd0w
02-02-2005, 01:54 AM
This is mainly for editing and whatnot, but I'm programming some stuff for my AP Computer Science course, and well I need the feedback from another programmer(s) to see if I have made any mistakes anywhere. Attached is the game Nim, here are the rules:

* Two players can play, being able to enter their names
* 4 piles to chose from
* Each pile has a randomly generated number between 15-30
* Players chose either 1-3 stones from any pile
* The person who takes the stone loses

That's the gist of it right there. I didn't seem to find anything wrong, but then again, I programmed it. Can someone go through and see if there's anything that I could fix, code that I could add/cut, etc... Since this is for a grade, I'd appreciate serious/constructive comments only. Thanks.

*** Note: You do need Java installed to run these ***

Baloki
02-02-2005, 02:11 AM
I'm only on basic Java at the moment and wish I could help but I could only find very basic problems at most but I will post to bump you to the top of the forum ;)

Dr Unne
02-02-2005, 05:50 AM
You don't have any comments. Especially for homework, you need comments. In real life you probably need them even more. Most programs worth using are going to be too complex to understand withtout effort or comments, and the less effort the better. Good to get in the habit. Your variable names are good and descriptive though, that's a good thing.

Now, for example:


stones1 = rand.nextInt(16) + 15;
stones2 = rand.nextInt(16) + 15;
stones3 = rand.nextInt(16) + 15;
stones4 = rand.nextInt(16) + 15;


This isn't a complex program, but that kind of thing is best done with an array. If you find yourself copying and pasting lines over and over, you're probably better off abstracting it into a data structure (array, hash, queue) or function. For example, you can define a constant called NUMBER_OF_PILES or something, and do things like


for(int c=0; c< NUMBER_OF_PILES; c++) {
/*Do some stuff with the piles*/
}

And if you decide one day you want to change your game to have 47 piles, you change it in one place at the top of your program and it works.

Same thing with takeStones1, takeStones2, etc. Better to have a function called takeStones, and pass it an int parameter to tell which pile to take from. Do a check on the parameter to make sure it's not greater than NUMBER_OF_PILES so you don't throw an array out of bounds error.

That will also let you replace things like


ans = console.readInt();
switch(ans)
{
case 1: takeStones1(); break;
case 2: takeStones2(); break;
case 3: takeStones3(); break;
case 4: takeStones4(); break;
}

with something like


ans = console.readInt();
takeStones(ans);

Flying Mullet
02-02-2005, 03:04 PM
I agree with all that Dr. Unne said. You MUST have comments for others to be able to follow what you are doing. My professors were always sticklers on comments and I'm very grateful for it now. I write Java for a living and there is nothing worse then having to wade through someone else's code that doesn't have comments and try to figure out what is going on. Some good advice that one of my profs once gave me is that for every line of code you write you should write a comment. Now that might be a tad overkill, but you get the idea. Your code shoudl have enough comments so that it reads like a book so that one doesn't have to actually read the logic to understand what's happening. This makes it much easier for others to navigate your code if they have to go in and fix code later or find a certain spot in the code for reference.

The only other thing that I can see is you should grab the player names with-in the same method. The only reason you need to break it out into two methods is if you have a lot of logic in each for maintainability's sake or you would be calling each of the name methods from several different places. Also, you only need to return the names if you need the resulting String in the code that calls the method. As you're storing them as members of the class you don't have to return them.
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">Code:</div>
<div class="alt2" style="color: #000000; background-color: #ffffff; margin:0px; padding:5px; border:1px inset; width:500px; height:110px; overflow:auto"><code>public void getNames()
{
System.out.print("Player one's name: ");
player1 = console.readWord();
System.out.print("Player two's name: ");
player2 = console.readWord();
}</code></pre>
</div></div>

Sepho
02-02-2005, 04:19 PM
I'll just third the part about comments that has already been mentioned.

Write comments, and be liberal with them. Even if you don't think anyone else is going to be reading your code, put them in there for yourself, because a couple months down the line, you might look at your own program and think "What's that supposed to do?". You should take the time to write in comments, because you're not supposed to be writing out your code as fast as you can as soon as it comes to mind. The idea is to plan and evaluate exactly what your program is supposed to do before you actually beging writing code.

I have no experience writing Java, so I can't offer up any language-specific problems, nor can I help you with your Java code, because I'm not familiar with Java syntax.

Samuraid
02-02-2005, 06:19 PM
While comments are <i>very</i> important, I respectfully disagree on the amount. Using comments for every line is often redundant and in the real world can make your code far too verbose. A better suggestion would be to comment each logical block of code. (which would approximately be equal to a comment every five lines or so) That way someone can still read your code and understand exactly what is going on but isn't lost in a mess of tons of redundancy and obvious comments.

§håd0w
02-03-2005, 01:24 AM
Thanks for the input; I can probably reduce a good amount of coding with Unne's and Mullet's changes. I can see why there should be comments at places, I'm very conservative with them, and they rarely seem to show up. :rolleyes2 I'll put those in in v.2. Thanks again, I'll be sure to come back if I need more help.