0

Not to sure why the integers lowRange and highRange are not going between these classes.

package guessnumber;
public class GuessNumber 
{
    static public int computerGenedNumber;
    static public int lowRange;
    static public int highRange;
    static public int playerGuess;
    public static void main(String[] args) 
    {
        Input.range(lowRange, highRange);
        Rand.number(lowRange, highRange, computerGenedNumber);
        Input.guess();

        Give.result();
    }
}

Next Class:

package guessnumber;
import javax.swing.JOptionPane;
class Input 
{
    public static void range(int lowRange, int highRange) 
    {
        String rawUserInput;
        rawUserInput = JOptionPane.showInputDialog("Please enter the range you wish to guess. (EX: 1-10)", "1-10");
        for(int i = 0; i < rawUserInput.length(); i++)
        {
            if(rawUserInput.charAt(i) == '-')
            {
                lowRange = Integer.parseInt(rawUserInput.substring(0, i));
                highRange = Integer.parseInt(rawUserInput.substring(i + 1, rawUserInput.length()));
            }
        }
    }
    static void guess() 
    {

    }
}

And the last relevant one:

package guessnumber;
class Rand 
{
    static public void number(int lowRange, int highRange, int computerGenedNumber) 
    {
        computerGenedNumber = (int)(Math.random() * (highRange - lowRange) + lowRange);
    } 
}

The rest of the classes are currently blank so I don't think I need to put them here too.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
lukeb28
  • 179
  • 1
  • 2
  • 12

2 Answers2

1

Here is a simplified piece of code which reproduce your problem, and make sure you understand why it is causing problem and the solution:

class Foo {
  public static void square(int a, int result) {
    result = a*a;
  }
}

class Bar {
  public static void main(String[] args) {
    int a=2;
    int result = 0;

    Foo.square(a, result);
    System.out.println("result " + result);
  }
}

This should be fundamental understanding of Java. Checkout what is the meaning of "pass-by-value"

In brief, the parameter passed in the method is a copy of the argument. Therefore when you are changing the parameter in your method, you are just changing another piece of data, and your change is not reflected to caller.

One way to fix is to change the method and return your result, which looks like:

class Foo {
  public static int square(int a) {
    return a*a;
  }
}

class Bar {
  public static void main(String[] args) {
    int a=2;
    int result = 0;

    result = Foo.square(a);
    System.out.println("result " + result);
  }
}

Another common solution is to pass in a "holder object" as the result. Although the object reference is passed by value, that copy of object reference is still pointing to the same object as caller. I won't go too deep into this as it is less common and you should be able to get the proper way doing so once you have better understanding on how value (including object reference) is passed around.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
0

Parameters are passed "by value" in Java. What that means is that when you call

input.range(lowRange, highRange);

it gives the current values of those variables to input.range, but it doesn't give input.range a way to modify them. In the range method:

public static void range(int lowRange, int highRange) 

the parameters lowRange and highRange (which have no connection with the variables in GuessNumber, even though the names are the same) are copies of what you pass in. When you assign lowRange = ... in the method, it changes the copy but has no effect at all on the lowRange and highRange in GuessNumber.

You need to write a range method that returns two values. This needs a little bit of work, but I'd write a Range class that has low and high members, and then change your method to

public static Range range()

That method would have to create a new Range object. I think it's OK for low and high to be public members of Range:

class Range {
    public int low;
    public int high;
    public Range(int low, int high) { 
        this.low = low;
        this.high = high;
    }
}

Normally, public data in a class is a bad thing, but for a class whose only purpose is to let a method return multiple values, it's OK in my opinion.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Could also create a new instance (i.e. `GuessNumber gn = new GuessNumber()`, pass that (`input.range(gn)`), and set the variables within `range()` using `gn.lowRange = foo;`. – patterned Jan 14 '14 at 01:22
  • I'm a little confused on where I need to make the changes. – lukeb28 Jan 14 '14 at 01:26
  • @lukeb28 Change the `range` method to return a `Range`; in that method, `lowRange` and `highRange` will be local variables, not parameters, and the method will need to create a `new Range` object to return the values. To call it, something like `Range r = input.range()`, and then `r.low` and `r.high` will be the range bounds you want. – ajb Jan 14 '14 at 01:41