1
public void setupPlayers() {
    System.out.println("Would you like the Code Maker to be Human or CPU? :");
    String input = mySc.next();
    if (input == "Human") {
        Player CodeMaker = new Human();
    }
    else {
        Player CodeMaker = new CPU();  
    }

public void exampleUse() {
    CodeMaker.getCode();
}

So I'm new to Java and not even sure if this is possible to begin with. The above code is all inside one class. Human is implementing the interface Player. I want to create an object CodeMaker using the class Human in the method setupPlayers, and then use this object in the method exampleUse. Is there a way to do this? If not can anyone think of any alternatives? Cheers.

5 Answers5

3

I think that you must mean the Player is the interface. And remember that if you want to compare the String, you should use equals method instead of == operator.

public class ClassName{
    private Player codeMaker;

    public void setupPlayers() {
        System.out.println("Would you like the Code Maker to be Human or CPU? :");
        String input = mySc.next();
        if (input.equals("Human")) {
            codeMaker = new Human();
        }
        else {
            codeMaker = new CPU();  
        }
    }

    public void exampleUse() {
         codeMaker.getCode();
    } 
}
HowieChih
  • 340
  • 3
  • 11
  • Thank you so much. After I added private Player codeMaker at the top it still wouldn't work, however I changed all the == for equals and it worked perfectly. – George Burslem Dec 22 '15 at 17:34
2

You may store the Player object as an instance variable :

Player CodeMaker = null;

public void setupPlayers() {
    System.out.println("Would you like the Code Maker to be Human or CPU? :");
    String input = mySc.next();
    if (input == "Human") {
        CodeMaker = new Human();
    }
    else {
        CodeMaker = new CPU();  
    }

}

public void exampleUse() {
    CodeMaker.getCode();
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
1

Create a class field variable (private, for good encapsulation) and use "string".equals(object) to check string equality (How do I compare strings in Java?).

private Player codeMaker;

public void setupPlayers() {
    System.out.println("Would you like the Code Maker to be Human or CPU? :");
    String input = mySc.next();
    if ("Human".equals(input)) {
        codeMaker = new Human();
    }
    else {
        codeMaker = new CPU();  
    }
}

public void exampleUse() {
    codeMaker.getCode(); // you need to define a return or consumption HERE
}

Maybe you should wrap input = mySc.next() in a try/catch like this, because input read can throw an Exception.

[...]

String input = null;
try {
  input = mySc.next();
}
catch(Exception ex) {
  throw ex;
}
if ("Human".equals(input) {
    codeMaker = new Human();
}

[...]
Community
  • 1
  • 1
Leo Dutra
  • 162
  • 3
  • 11
0

Just create a member of class: private Human CodeMaker; and initialize it in constructor CodeMaker = new Player();

Don't do Player CodeMaker = new Human(); - this should give you an error because you can't create an object of interface.

0

Please, consider studying Creational Design Patterns. Java's culture is focused on organization and good architecture. You'll be certainly surprised with the world Design Patterns can give to you.