• Post Reply Bookmark Topic Watch Topic
  • New Topic

method help  RSS feed

 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

I need help with these method. I need to refactore them so they follow the smells code. the first method allows the user to enter name of human player and the second method allows the user to enter the name of the computer player. can someone show me how I can make these methods better..

Thanks Alot


[ October 20, 2006: Message edited by: steve low ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's go one tiny step at a time. One way to start is to look for duplication. What did you do twice? Create a reader, read a line, check the length. See if you could move the bits that happen twice to new method like this:

Test the new method with strings that are too short or too long. What do you want it to do in those cases? Show us what you make!
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

could you show me an example of how I would do this
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ive been doing what you said created a different method for refactored duplicate lines out into a reuseable method.im having problem. if a method is a string how would i change the method outputAsstring so that it can take string text. with the below example.

Thanks alot



};
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had this before results = in.readLine(); which is just what you need. Your prompting method might wind up looking like

Then your current loop to get names might look like

With all the buffered readers and exceptions and such taken out of that loop, see how much better it tells us what the program is about? In about three lines it says "Now I'm filling the player name array from some reader"

See how that works out.
[ October 21, 2006: Message edited by: Stan James ]
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi almost done what you said. but still having a few problems. like done. what is that suppose to do...? and is that what needed to go in the readString method....then what would i need to put in the nameofplayer method. I know i had to take out the new buffer and read line.


 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The little snippet I put is not compilable Java, obviously, but enough of a hint to get you going. One idea here is to NOT put the prompt about "Enter name for player n" inside this method, but to pass it in as a parameter. That way we can use the same method to prompt for anything else.

Step away from your larger program for a minute. See if you can make a tiny program that just works that prompt method:

Really make this run. Try it with 4 characters, with 6, with 15. Make sure it handles the good lengths and bad lengths the way you want.

Tiny programs that only prove one thing at a time are valuable tools. If nothing else, you can make them work more often which is just plain more fun.
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan


Im sorry to keep asking you guys...but that example i cant understand it .. I can never understand things in java when somone shows me another example of java that the one i have. could you please show me the example of how i would use readstring with public static void nameofplayer()

i have another 8 classes to do..If you do me that one i can understand it better please
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't like to do homework for people here at JavaRanch, because it deprives you of the chance to learn what you're supposed to learn. But we'll gladly help you learn.

So: What exactly don't you understand? Which of the lines Stan posted make sense to you and which don't?
 
steve low
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer

this is not homework or assignment work....this is just reserch. I was givin a big method which ive refactored into smaller methods...what i dont understand is how i can use the readstring method with the public static void nameofplayer() method....Im not sure what needs to be removed from the nameofplayer() method and be added to the read string method.. If you can show me example of this I would appriciate it. From stans example im not sure were test come from or how it applies to name of player method...if you can show me an example i would appriciate it..and it would be very helpful to me..
[ October 23, 2006: Message edited by: steve low ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trust me, make readString() work, we'll plug it into loading the player name array next. Small steps == frequent success.

You had all the working bits you need for readString() already. You just had them twice. Take a shot at it. I'm confident you'll get it going.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!