-8

I came across a method with a parameter list where the parameter were not separated by comma and no declaration of the variable type:

public int compareWith(Choice anotherChoice){

Later these parameters were called without any further declaration inside the body in an if-else statement in combination with another method:

if ((this.type == 0)&&(anotherChoice.getType() == 1)){
        return -1;

This is a brief summary of the entire class:

public class Choice
{

private int type; 


public Choice(int type)
{
    //initialize the "type" instance varialble
    this.type = type;
}

  public int getType()
{
    return type;
}

 public int compareWith(Choice anotherChoice)
{
   Choice choice1 = new Choice(0);
   Choice choice2 = new Choice(1);
   if ((this.type == 0)&&(anotherChoice.getType() == 1)){
        return -1;

The program goes on. I really don't get the link between anotherChoice, getType() and choice2. It is a task in an online course and the program works as intended, but I don't know why.

David
  • 4,665
  • 4
  • 34
  • 60
GDanCab
  • 3
  • 1
  • 1
    Maybe I am blind; but I can only see methods that are using a single parameter; so why do you expect to see commas in there? (you only need commas to separate argument**s** ... so plural, not singular!) – GhostCat Apr 29 '15 at 15:01
  • 3
    Choice and anotherChoice aren't two parameters. Choice is the type of anotherChoice. – Benjy Kessler Apr 29 '15 at 15:01
  • 1
    Is "public int compareWith(Choice anotherChoice){" your example of a method with parameter list with no commas? You *DO* realize that is only a single parameter, right? – David Apr 29 '15 at 15:01
  • Your question is unclear. Except bad formatting these code fragments seem syntactically correct. What's your problem? – AlexR Apr 29 '15 at 15:01
  • Thank you guys, I did not recognize Choice as the type of the parameter anotherChoice. There is no coloring in my environment. I am knew to programming, so thank you for your quick help – GDanCab Apr 29 '15 at 15:36

3 Answers3

2

To clear up your confusion, in this method declaration:

public int compareWith(Choice anotherChoice){

anotherChoice is the parameter of type Choice.

Ryan
  • 2,058
  • 1
  • 15
  • 29
1

I'm guessing you're new to programming, so I'll give a quick explanation of what's going on. If I've missed the point of your question entirely, I'm sorry.

This line:

public int compareWith(Choice anotherChoice){

is part of the Choice object. It takes another Choice object and compares it with itself. ...or at least, that's what I would expect. The code you provided:

public int compareWith(Choice anotherChoice)
{
   Choice choice1 = new Choice(0);
   Choice choice2 = new Choice(1);
   if ((this.type == 0)&&(anotherChoice.getType() == 1)){
        return -1;

is incomplete and I have no idea what choice1 and choice2 are supposed to be doing. The code I would expect to see would look more like

public int compareWith(Choice anotherChoice)
{
   if (this.type == anotherChoice.getType())
        return 0;
   return -1;
}

or something like that.

Does that help?

David
  • 4,665
  • 4
  • 34
  • 60
  • yep, I am new to programming. Totally missed that Choice is the type defined as a class. The coloring helps a lot. In my development environment it is not colored and I did not recognize that anotherChoice is of the Choice type. Thanks! – GDanCab Apr 29 '15 at 15:33
  • Notepad++ (http://notepad-plus-plus.org) will do code coloring for you, if you like. If this answer helped you out, I would be grateful if you would accept it as the answer. – David Apr 29 '15 at 18:16
-1

This was my solution for the course. Probably not the best solution but it worked and i receved my grade. I'm at this moment stuck with LAB 04 TASK 3.

int player=this.getType();//get value of choice1
int computer=anotherChoice.getType();// this get the value choise2 out of anotherChoise
int som;// this INT i made to make a sum
int result;
result = 0;//i already gave a value on the INT result
som = player - computer;//this is the sum of de values choice1 and choise2

if(player == computer)  //all ties
  result = 0;     
else if(som == -1) //player ROCK and comp. PAPER or PAPER and SCISSOR
  result = -1;
else if(som == 2) //player SCISSOR and comp. ROCK
  result = -1;
else if(som == 1)//player SCISSOR and comp PAPER or PAPER and ROCK
  result = 1;
else if(som == -2)//player ROCK and comp. SCISSOR
  result = 1;
return result; // this line should be modified/removed upon finishing
a14m
  • 7,808
  • 8
  • 50
  • 67
Danny
  • 1