Please help with JAVA

I have this code and I am having errors. I’m a newbie to Java and need your assistance.
Please find my classes and the driver. I don’t know where I am going wrong but my driver says that variable computerChoice may have not been initialized.

package rps;
import java.io.*;
public class computer {
String computerChoice;
int choiceType;
public computer(String ComputerChoice, int ChoiceType)
{
computerChoice=ComputerChoice;
choiceType=ChoiceType;

}
public String knowComputerChoice()
{
return computerChoice;
}
public int knowChoiceType()
{
return choiceType;
}
public String someMethod(String computerChoice) {
if(choiceType==1)
computerChoice=“r”;

else if(choiceType==2)
computerChoice=“p”;

else
computerChoice=“s”;

return computerChoice;

}}

package rps;

public class player {
String playerEntry;

public player(String PlayerEntry)
{
playerEntry=PlayerEntry;
}
public String knowPlayerEntry()
{
return playerEntry;

}

}

package rps;
public class decider {
public static String decideWinner(String computerChoice,String playerEntry)
{
String winner;
if(computerChoice.equals(playerEntry)){
winner = “Draw”;
System.out.println(“Tie!”);
}
else if(computerChoice.equals(“r”)&& playerEntry.equals(“s”)){
winner=“computer”;
System.out.println(“Computer won!”);
}
else if(computerChoice.equals(“p”)&&playerEntry.equals(“r”)){
winner =“computer”;
System.out.println(“Computer won!”);
}
else if(computerChoice.equals(“s”)&&playerEntry.equals(“p”)){
winner = “computer”;
System.out.println(“Computer Won!”);
}
else{winner=“player”;
System.out.println(“Player won!”);
}
return winner;

}}
import java.io.;
import rps.
;
public class RockPaperScissorsDr {
public static void main(String []args) throws IOException
{
computer Computer;
player Player;
decider Decider;
String playerEntry,computerChoice,winner;
int playerWins=0,computerWins=0,ties=0,choiceType=(int)(Math.random()*3);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.println("An invalid entry will close the game!");
System.out.println("Please choose from the menu, the letter you would like to play");
System.out.println("Enter r for rock,p for paper,s for scissors:");
playerEntry=in.readLine().toLowerCase();
if(!(playerEntry.equals("r")&&playerEntry.equals("s")&&playerEntry.equals("p")))

System.out.println(“Invalid Entry! End of game!”);
while(playerEntry.equals(“r”)&&playerEntry.equals(“s”)&&playerEntry.equals(“p”)){
System.out.println("Computer’s choice is: "+choiceType);
decider.decideWinner(computerChoice,playerEntry);
computerWins++;}
playerWins++;
ties++;

      }}

In class Computer, you have a declaration:
String computerChoice;

Change this to:
String computerChoice = “”;

I think this should fix the problem.

Martyn

what access modifier explicitly says that a method or varible of an object can be accessed by code outsiber of the object?
a.private
b.public
c.default
d.static

what access modifier explicitly says that a method or varible of an object can be accessed by code outsiber of the object?
a.private
b.public
c.default
d.static

If you want to use methods or variables outside the class you should use public (or protected for inherited classes), but I recommend using private for variables and using methods for manipulating variables. e:

public class JClass{

private String line = “”;

//… other methods and a constructor

//for reading a variable
public String getLine(){
return line;
}

//and for editing a variable
public void setLine(String anotherLine){
line = anotherLine;
}

}

I hope this was any help for you… If you want to learn more of java check java api & tutorials.

-Juhis

gehinbi tote
javaTips.txt