cautiously asking for help on making methods

Hello,

I am going to take a risk and post this code. I am starting to learn how to solve some basic problems in java, however OOP is still a frontier. I produced some code that is all in the main method. I would really like to see how it may be done with several different methods.

I risk getting flamed but hey, sometimes you gotta say WTH. The point is that I have trouble know how to break down code like this into smaller methods so that OOP is utilized Anyone in the mood to be a Samaritan?

import java.util.Random;

import java.util.Scanner;

publicclass RockPaperScissors{

/* HAVE COMPUTER RANDOMLY PICK

*/

publicstaticvoid main(String[]args){

int num1 =0;

int user=0;

int tie =0;

int you =0;

int comp=0;

String userS =null;

String numS =null;

String result =null;

Random generator =new Random();

/* HAVE USER PICK*/

System.out.println("Pick 0 for ROCK, 1 for PAPER and 2 for SCISSORS");

System.out.println("Press 3 if you dont want to play anymore OK?");

System.out.println("Then I will tally your results");

Scanner scan =new Scanner(System.in);

user = scan.nextInt();

num1 = generator.nextInt(3);

while(user!=3){

switch (user){

case 0:

userS ="rock";

break;

case 1:

userS ="paper";

break;

case 2:

userS ="scissors";

break;

}

/*DETERMINE WINNER */

switch(num1){

case 0://computer rock

numS ="rock";

if (user==0)//computer Rock and user Rock

{

result ="tie";

tie = tie +1;

}

if (user==1)//computer Rock and user Paper

{

result ="you win";

you = you +1;

}

if (user==2)//computer Rock and user Scissors

{

result ="computer wins";

comp = comp +1;

}

break;

case 1://computer paper

numS ="paper";

if (user == 0)//computer Paper and user Rock

{

result ="computer wins";

comp = comp+1;

}

if (user ==2)//computer paper and user Scissors

{

result ="you win";

you = you+1;

}

if (user == 1&&num1==1)//computer paper and user paper

{

result ="tie";

tie = tie+1;

}

break;

case 2://computer scissors

numS ="scissors";

if (user ==0)//computer scissors user rock

{

result ="you win";

you = you +1;

}

if (user == 1)//computer scissors user paper

{

result ="computer wins";

comp = comp +1;

}

if (user ==2 )//computer scissors user scissors

{

result ="tie";

tie = tie+1;

}

break;

}

//SHOW WINNER

System.out.println("The computer had "+ numS+" you had "+userS);

System.out.println(result);

System.out.println();

System.out.println("============================================");

System.out.println("Pick 0 for ROCK, 1 for PAPER and 2 for SCISSORS");

System.out.println("Press 3 if you dont want to play anymore OK?");

System.out.println("Then I will tally your results");

user = scan.nextInt();

num1 = generator.nextInt(3);

}//end while

//tally

System.out.println("The results are in!");

System.out.println("Computer wins "+ comp);

System.out.println("You wins "+you);

System.out.println("Ties "+tie);

System.out.println("Thanks for playing, have a nice day.");

}

/*SHOW RESULTS*/

}

[6991 byte] By [pberardi1a] at [2007-11-27 11:06:35]
# 1

When thinking about what needs to be a method, think about how you can break down the program flow into steps. You take user input, process it, then output the results. Those are three steps, and each can be split into a separate method. Create one that prompts the user for input, takes the input and returns it. Then just call that method in place of the code you moved to the method.

hunter9000a at 2007-7-29 13:16:22 > top of Java-index,Java Essentials,New To Java...
# 2

> The point is that I have trouble know how to break down code like this into smaller methods so that OOP is utilized...

First thing to understand that smaller (and typically, more) methods -- while often a worthwhile goal -- does *not* mean that your code is OO.

~

yawmarka at 2007-7-29 13:16:22 > top of Java-index,Java Essentials,New To Java...
# 3

I am starting to understand that instead of looking at the behavior first, one should look at objects first and how to manipulate them. Is that correct?

pberardi1a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 4

> I am starting to understand that instead of looking

> at the behavior first, one should look at objects

> first and how to manipulate them. Is that correct?

Think of objects as encapsulations of data and it's associated behavior. If you're modeling a dog, don't think of a bunch of variables like it's name, owner, etc. Think of a class that maintains that info, as well as the methods that define what a Dog can do, and what can be done to the Dog. Make sense?

hunter9000a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 5

Whenever I see a switch with a lot of cases, I have to think to myself, "is there a way I can implement this with objects and polymorphism". In this case, the answer is a strong "yes".

Also, this smells like the perfect place for an enum. A rockPaperScissors enum. I've done something like this before for this very case, and it works like a charm. You can even have the enum check for who wins. It is a class you know and can have methods.

petes1234a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 6

Start without code and think about the problem.

You're trying to design a two-player game. Each Player chooses between three states (rock, paper, scissors). There are rules to determine who wins for each combination. The payoff matrix is 3x3 and symmetric:

RPS

R tiePR

P PtieS

S RStie

A single game consists of one or more repetitions, where each player chooses a state. A running total of wins is kept for each player. When the game is over, the player with the most wins is declared the winner.

If this is an accurate summary of your game, start thinking about useful objects. Maybe a Player, where each one will keep track of its own wins. Maybe a GameEngine that will determine the outcome given that payoff matrix.

Leave the UI out of it until you've got the objects working on the command line with only text input. Test them thoroughly and then worry about the UI.

That's the way I'd recommend you proceed.

%

duffymoa at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 7

> Start without code and think about the problem.

> ...................

> ...................

> That's the way I'd recommend you proceed.

>

> %

Dmn, what an excellent post. Thanks /P

petes1234a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 8

here's one way to get an enum started that gets rid of many if blocks or switch statements:

enum RPS

{

ROCK("rock"),

PAPER("paper"),

SCISSORS("scissors");

private String text;

private static Random rand = new Random();

private final static int[][] PAYOFF_MATRIX =

{

{0, -1, 1}, // RR, RP, RS

{1, 0, -1}, // PR, PP, PS

{-1, 1, 0}// SR, SP, SS

};

private RPS(String text)

{

this.text = text;

}

/**

* finds winner of individual rock paper scissors game

* @param r1 RPS variable 1

* @param r2 RPS variable 2

* @return 1 if r1 beats r2; -1 if r2 beats r1; 0 if tie

*/

public static int getWinner(RPS r1, RPS r2)

{

int index1 = r1.ordinal();

int index2 = r2.ordinal();

return PAYOFF_MATRIX[index1][index2];

}

..........................

..........................

..........................

Message was edited by:

petes1234

petes1234a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 9

Any method that both performs business logic and displays the results is too busy. Any method that does more than one job is too busy, really

georgemca at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 10

Tell, don't ask

Methods that return a value - while often unavoidable - aren't always appropriate. Think if you can make the object you're calling do the work for you, rather than get the value and then do the work yourself ("yourself" being the method or object that is calling another)

georgemca at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 11

> Any method that both performs business logic

> and displays the results is too busy. Any

> method that does more than one job is too busy, really

which method do you mean?

petes1234a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 12

> > Any method that both performs business logic

> > and displays the results is too busy. Any

> > method that does more than one job is too busy,

> really

>

> which method do you mean?

Any method in the world, ever. In this case, though, main. Is there any other method?

georgemca at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 13

>

> Any method in the world, ever. In this case, though,

> main. Is there any other method?

Sorry, I was not clear, what I meant was: were you talking about my method (getWinner) or the OP's? Thanks

petes1234a at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 14

> >

> > Any method in the world, ever. In this case,

> though,

> > main. Is there any other method?

>

> Sorry, I was not clear, what I meant was: were you

> talking about my method (getWinner) or the OP's?

> Thanks

Oh, right! Neither, really. Just a random bit of methody advice :-)

georgemca at 2007-7-29 13:16:23 > top of Java-index,Java Essentials,New To Java...
# 15

> Oh, right! Neither, really. Just a random bit of

> methody advice :-)

Got it, and so filed away in my brain cabinet under "general methody advice" . Thanks /Pete

petes1234a at 2007-7-29 13:16:28 > top of Java-index,Java Essentials,New To Java...
# 16

Thanks for the pearls of wisdom you guys. I like to hear from all you experienced programmers. Don't ask me why but I really want to get this stuff down. Call it a mission. I'm sure some of you can identify. I might not fully comprehend everything but one day that light is gonna go on and your advice will ring true.

Can anyone suggest a link or a book that goes into "methody advice"? I am pretty certain that having everything in the main method is not only poor programming practice but unsafe for some reason. Any literature on this topic that is easy to read for java beginner would be much appreciated.

I dont want to beat a dead horse but repetition often seems to be the solution to many a problem.

Thanks,

pberardi1a at 2007-7-29 13:16:28 > top of Java-index,Java Essentials,New To Java...
# 17

years ago I read a book by a guy named Gady Booch (I think) on object oriented design that was pretty good. I'm not sure if it's still out there. I also think that the Head-First series has a book on object oriented design. If you like their style, theirs may be the better introductory text.

petes1234a at 2007-7-29 13:16:28 > top of Java-index,Java Essentials,New To Java...
# 18

> Thanks for the pearls of wisdom you guys. I like to

> hear from all you experienced programmers. Don't ask

> me why but I really want to get this stuff down.

> Call it a mission. I'm sure some of you can

> identify. I might not fully comprehend everything

> but one day that light is gonna go on and your

> advice will ring true.

>

> Can anyone suggest a link or a book that goes into

> "methody advice"? I am pretty certain that having

> everything in the main method is not only poor

> programming practice but unsafe for some reason. Any

> literature on this topic that is easy to read for

> java beginner would be much appreciated.

>

> I dont want to beat a dead horse but repetition often

> seems to be the solution to many a problem.

>

> Thanks,

Effective Java by Josh Bloch

The Pragmatic Programmer by Dave Thomas and Andy Hunt, I believe

Java Design by Peter Coad

Java Puzzlers by Josh Bloch (later)

Better Faster Lighter Java by Bruce Tate (much later)

georgemca at 2007-7-29 13:16:28 > top of Java-index,Java Essentials,New To Java...