Benji Weiss
Benji Weiss

Reputation: 416

instead of class name being returned, the memory address is returned

I'm trying to have 2 teams play with each other. I call that when i say team1.play(team2); when i generate a number less than 0.5, team2 should win and if its greater than 0.5 then team 1 should win. when team 1 wins, it displays correctly as the knicks, but when team2 wins, it displays the memory address. how can i get it to properly say the nets are winning and not team@78987neu73

public class teams{
    public static void main(String [] argv){
        team team1 = new team("knicks");
        team team2 = new team("nets");
        team1.lose();
        team2.win();
        team2.lose();
        team2.printrecord();
        team1.play(team2);
    }//main
}//teams

class team{
    int wins; 
    int losses;
    String name;

    public team(String n){
        name = n;
        wins = losses = 0;
    }//constructor

    public void lose(){
        losses++;
    }//losses

    public void win(){
        wins++;
    }//wins

    public void printrecord(){
        System.out.println("W-L: " +wins+"-"+losses);
    }

    public void play(team j){
        if((Math.random())<0.5){
            System.out.println("The "+j+" Have Won!");
        }//if
        else 
            System.out.println("The "+name+" Have Won!");
    }

}

Upvotes: 3

Views: 239

Answers (6)

lxcky
lxcky

Reputation: 1668

There are a lot of ways to solve your problem, the simplest one would be to directly get the name of the j team like this:

if((Math.random())<0.5){
    System.out.println("The "+j.name+" Have Won!"); //You should use the name of the j team
}
else {
    System.out.println("The "+name+" Have Won!");
}

Another one would be to override the toString() method in your team class.

public String toString() {
    return name;
}

If you want to be more advanced about it and follow the industry standards, then let's construct your team class properly:

class Team { //class names are PascalCase

    private int wins; //encapsulate fields
    private int losses;
    private String name;

    //constructor
    public Team(String n) {
        name = n;
        wins = losses = 0;
    }

    //getter and setter methods
    public int getWins() {
        return wins;
    }

    public void setWins(int wins) {
        this.wins = wins;
    }

    public int getLosses() {
        return losses;
    }

    public void setLosses(int losses) {
        this.losses = losses;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    

    public void lose() {
        setLosses(getLosses() + 1);
    }

    public void win() {
        setWins(getWins() + 1);
    }

    public void printrecord() {
        System.out.println("W-L: " + getWins() + "-" + getLosses());
    }

    public void play(Team otherTeam) { //more descriptive name
        if ((Math.random()) < 0.5) {
            System.out.println("The " + otherTeam.getName() + " Have Won!");
        }
        else {
            System.out.println("The " + this.getName() + " Have Won!");
        }
    }
}

Upvotes: 3

StackFlowed
StackFlowed

Reputation: 6816

all you need to do is to override the toString functionality in your teams class to return the name ...

just add this code in teams class.

@Override
public String toString() {
    return name;
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201497

Override toString() in team, here's an example

@Override
public String toString(){
    return String.format("%s W-L: %d-%d",name, wins, losses);
}

that way your play(team j) can print the team j. Also, Java naming convention has classes start with a capital letter. team should be Team.

Upvotes: 3

shazin
shazin

Reputation: 21903

You need the following two modifications in your team class

public String getName() {
    return name;
}

public void play(team j){
    if((Math.random())<0.5){
        System.out.println("The "+j.getName()+" Have Won!");
    }//if
    else 
        System.out.println("The "+name+" Have Won!");
}

Upvotes: 5

Abhi
Abhi

Reputation: 744

You have put j directly. Try using following code

public void play(team j){
        if((Math.random())<0.5){
            System.out.println("The "+j.name+" Have Won!");
        }//if
        else 
            System.out.println("The "+this.name+" Have Won!");
    }

Upvotes: 4

aookami
aookami

Reputation: 53

public void play(team j){
    if((Math.random())<0.5){
        System.out.println("The "+j+" Have Won!");
    }//if
    else 
        System.out.println("The "+name+" Have Won!");
}

in here, "j" is an object, not an string. use j.name instead of j.

Upvotes: 3

Related Questions