-1

pretty much I've made a program that goes a little like this

  1. Players are added to an array
  2. they are then all placed against each other(just like a football match, every team plays every team)
  3. They are then randomized
  4. the program should display the end result(if you win a game you get 3 points) etc..

I keep getting a message from Eclipse saying ".setScore has NOT been coded..

TestGame.java

public class TestGame {
    static Players ceri = new Players("Ceri", 0);

    static Players harry = new Players("Harry", 0);
    static Players matthew = new Players("Matthew", 0);
    static Players james = new Players("James",0);
    static Players kwok = new Players("Kwok",0);
    static Players lewis = new Players("Lewis",0 );
    static Game League = new Game();
    int Ceri = 0;

    public static void main(String[] args) {
        League.addPlayers(ceri);
        League.addPlayers(matthew);
        League.addPlayers(james);
        League.addPlayers(kwok);
        League.addPlayers(lewis);
        League.addPlayers(harry);
        League.randomize();
        League.Results();    
    }
}

Game.java

public class Game extends TestGame {

    Scanner s = new Scanner(System.in);
    private Players[] people = new Players[6];
    private int counter = 0;
    List<String> Matches = new ArrayList<String>();

    public void addPlayers(Players obj){
        people[counter] = obj;
        counter++;
        System.out.println(obj.getName());
    }

    public String randomize(){
        for(int i = 0; i < people.length; i++){
            for(int j = i + 1; j < people.length; j++){
                if(people[i].equals(people[j].getName())){                    
                    continue;
                } else {
                    Matches.add(people[i].getName() + " V " + people[j].getName());
                }
            }
        }
        return null;
    }

    public String Results(){
        while(!Matches.isEmpty()){
            int Game = (int)(Math.random() * Matches.size());
            String Verses = (String)Matches.get(Game);

            System.out.println(Verses);
            System.out.println("Who has won?");

            String name = s.nextLine();
            //**ISSUE LIES HERE**    

            if(Verses.contains(name)){
                if(name == "ceri"){
                    ceri.setScore(3);
                } else if(name == "harry"){
                    harry.setScore(3);
                }else if (name == "matthew"){
                    matthew.setScore(3)
                } else if(name == "lewis"){
                    lewis.setScore(3)
                }else if ( name == "james"){
                    james.setScore(3)
                }else if(name == "kwok"){
                    kwok.setScore(3);
                }
            }

            Matches.remove(Game);
            System.out.println(one);

            return null;

        }
        return null;
    }
}

WHERE ECLIPSE SAYS .setScore ISNT.

Players.java

public class Players {
    private String name;
    private int score;

    public Players(String name, int score){
        this.name = name;
        this.score = score;
    }

    public String getName(){
        return this.name;
    }

    public int getScore() {
        return this.score;
    }

    public void setScore(int score) {
        this.score =+ score;
    }
}
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
Z-2
  • 73
  • 1
  • 8
  • 1
    You're using `==` instead of `.equals`. See here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – jhobbie Jul 23 '14 at 18:27
  • @jhobbie I know how .equals works etc but why wont the setter work? – Z-2 Jul 23 '14 at 18:29
  • 1
    Also, what is the type of the variables `ceri, harry, mathew,` etc.? Where and how are they declared? That could be affecting the compiler's errors. – markspace Jul 23 '14 at 18:30
  • @user3870022 it won't work because `name == "ceri"` doesn't do anything. Same thing for all your other names. And by doesn't do anything, I mean it doesn't do what you want it to. – jhobbie Jul 23 '14 at 18:30
  • Well I thought i'd just jump the gun and try to change the 'points' by doing .setScore if that makes sense? @markspace – Z-2 Jul 23 '14 at 18:31
  • @jhobbie It might work in a simple program like this because all string constants are interned. Let's get the program to compile first, then debug runtime issues. (I do totally agree it's a bad habit though.) – markspace Jul 23 '14 at 18:32
  • @jhobbie , I guess i didn't know what it meant haha, so just change it to something like name.equals(ceri.getName)? – Z-2 Jul 23 '14 at 18:32
  • In which class do you have the match logic. Can't understand how the match logic access ceri, harry, mathew, – Vivin Jul 23 '14 at 18:34
  • OK I see the definitions now. You got me. I think there's something else wrong (not shown) that is messing you up. Like package names or similar. Can you produce a short, complete example that shows the problem? Just make one user and call .setScore() in a two line program, does that compile? – markspace Jul 23 '14 at 18:35
  • @Vwin the match logic is in game.java(if thats what you are asking) – Z-2 Jul 23 '14 at 18:36
  • I can't find the variable `Player ceri;` in your code. How are you calking `ceri.setScore`? – Vince Jul 23 '14 at 18:40
  • I think you're trying to reference `ceri` from an object with no reference to it. If you paste the exact error you're getting, it might be helpful – Gus Jul 23 '14 at 18:42
  • the method for setScore is undefined for type Players @Gus – Z-2 Jul 23 '14 at 18:45
  • 1
    Keeping the static keywords in place, try `TestGame.ceri.setScore(3);` etc. for each player. – Russell Uhl Jul 23 '14 at 18:46
  • it comes up "change ceri to static"(which it already is) @RussellUhl – Z-2 Jul 23 '14 at 18:48
  • sorry. also forgot to mention that you should make them `public static` (at least for now), just to rule out any accessibility issues. (That may or may not fix the problem, but it will at least remove a variable from your problem) – Russell Uhl Jul 23 '14 at 18:51
  • unfortunately it hasnt sorted the issue 3 Thank you anyway! @RussellUhl – Z-2 Jul 23 '14 at 18:54
  • @RussellUhl i spoke too soon, it has! – Z-2 Jul 23 '14 at 18:56
  • Good. I'll write it up in answer form for reference – Russell Uhl Jul 23 '14 at 18:57
  • I thank thee so much @RussellUhl – Z-2 Jul 23 '14 at 19:01

3 Answers3

1

All the players are static members of TestGame class. They can access only static methods of Players now. So that is why you are getting the error.

Vivin
  • 1,327
  • 2
  • 9
  • 28
  • ive changed them all from static now i'm getting errors because "League" is static as well, and then i take static off that and more errors – Z-2 Jul 23 '14 at 18:44
  • 1
    While your answer is truthfully correct, it also suggests that making something static makes it invisible to other objects, which is false. – Russell Uhl Jul 23 '14 at 18:45
0

Your code has a couple of issues, but neither of them is related to setScore not being defined.

  1. You are missing semicolons in the following block.

    if(Verses.contains(name)){
        if(name == "ceri"){
            ceri.setScore(3);
        } else if(name == "harry"){
            harry.setScore(3);
        }else if (name == "matthew"){
            matthew.setScore(3)
        } else if(name == "lewis"){
            lewis.setScore(3)
        }else if ( name == "james"){
            james.setScore(3)
            }else if(name == "kwok"){
            kwok.setScore(3);
        }
    }
    
  2. You are not using equals for String comparison in the block above.

Here is a corrected version:

if(Verses.contains(name)){
    if(name.equals("ceri")){
        ceri.setScore(3);
    } else if(name.equals("harry")){
        harry.setScore(3);
    } else if (name.equals("matthew")){
        matthew.setScore(3);
    } else if(name.equals("lewis")){
        lewis.setScore(3);
    } else if ( name.equals("james")){
        james.setScore(3);
    } else if(name.equals("kwok")){
        kwok.setScore(3);
    }
}
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0

First off, there are compile errors. Please see merlin2011's answer for reference.

As to your actual issue, your Players (ceri, harry, matthew, etc.) are static members of TestGame...but are not publicly exposed. This means that other objects can't see them, and so can't access them. Add the public keyword to them:

public class TestGame {
    public static Players ceri = new Players("Ceri", 0);
    public static Players harry = new Players("Harry", 0);
    public static Players matthew = new Players("Matthew", 0);
    public static Players james = new Players("James",0);
    public static Players kwok = new Players("Kwok",0);
    public static Players lewis = new Players("Lewis",0 );

    ...
}

Now that they are publicly exposed, you need to reference them correctly. Your Game class doesn't know what ceri is when you call setScore(3), Since it is not a member of that class. Instead, you need to tell it where ceri is located; in your TestGame class. For reference, I've also used the traditional string equality comparison function.

if(name.equals("ceri")){
    TestGame.ceri.setScore(3);
}

Use this format for the other IF statements you have.

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • one last question, all of a sudden now my program terminates after one match is asked.. hmm – Z-2 Jul 23 '14 at 19:10
  • @user3870022 yep. You have a return null inside your while loop in in your Game class. – Russell Uhl Jul 23 '14 at 20:28
  • @user3870022: one of the best tools at your disposal is the Debugger. Learn how to use it, at least at a basic level. It can allow you to run your code line by line, view and change variables mid-run, and a bunch of other things. If you were to have used the debugger, you would have found that return null very quickly – Russell Uhl Jul 23 '14 at 20:29