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
  • why you are using an interface as a variable name? i didn't get the point Commented Dec 22, 2015 at 17:05
  • 1
    @fuzzy28 - as far as I understand, Player is and class impementing interface Human. Anyway, the sentence "an object Player using the Interface Human called CodeMaker" is a little bit confusing. Commented Dec 22, 2015 at 17:10
  • ^ Player is the interface, and Human is the class implementing Player. Commented Dec 22, 2015 at 17:11
  • I thought that "Human" and "CPU" would both implement "Player" .. Commented Dec 22, 2015 at 17:13
  • I've revised my question so hopefully it makes more sense now... Commented Dec 22, 2015 at 17:23

5 Answers 5

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();
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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();
}

Comments

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();
}

[...]

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.