Welcome to Dream.In.Code
Become a Java Expert!

Join 150,384 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,158 people online right now. Registration is fast and FREE... Join Now!




Can't find methods/variables?

 
Reply to this topicStart new topic

Can't find methods/variables?, Comparable

Revitalized
8 Feb, 2008 - 09:29 AM
Post #1

New D.I.C Head
*

Joined: 11 Sep, 2007
Posts: 17


My Contributions
So the assignment was to make a class that uses compareTo to compare two different Monsters. If Monster 1 is larger it returns 1, if Monster 2 is larger it should return -1. (We just got into the Comparable, so it's still somewhat new.)

We started with a skeleton of a class and I've filled it in as best I could.

CODE

import static java.lang.System.*;

public class Monster implements Comparable
{
    private int myWeight;
    private int myHeight;
    private int myAge;

    //write a default Constructor
    public Monster()
    {
        myWeight=0;
        myHeight=0;
        myAge=0;
    }
    

    //write an initialization constructor with an int parameter ht
    public Monster(int ht)
    {
        setMonster(0,ht,0);
    }


    //write an initialization constructor with int parameters ht and wt
    public Monster(int ht, int wt)
    {
        setMonster (wt, ht, 0);
    }



    //write an initialization constructor with int parameters ht, wt, and age
    public Monster(int ht, int wt, int age)
    {
        setMonster(wt,ht,age);
    }


    //modifiers - write set methods for height, weight, and age
    public void setMonster(int ht, int wt, int age)
    {
        myWeight=0;
        myHeight=0;
        myAge=0;
    }
    
    
    //accessors - write get methods for height, weight, and age
        public int getHeight()
        {
            return myHeight;
        }
        public int getWeight()
        {
            return myWeight;
        }
        public int getAge()
        {
            return myAge;
        }
    
    
    //creates a new copy of this Object
    public Object clone()
    {
       return new Monster();
    }

    public boolean equals( Object obj )
    {
        Monster temp = (Monster)obj;
        return (myWeight == obj.getWeight() && myHeight == obj.getHeight() && myAge== obj.getAge());
    }

    public int compareTo( Object obj )
    {
        Monster rhs = (Monster)obj;
        if(myHeight > rhs.myHeight)
        {
            return 1;
        }
        else if(myHeight < rhs.myHeight)
        {
            return -1;
        }
        else
        {
            if(myWeight > rhs.myWeight)
            {
                return 1;
            }
            else if(myWeight < rhs.myWeight)
            {
                return -1;
            }
            else
            {
                if(myAge > rhs.myAge)
                {
                    return 1;
                }
                else if(myAge< rhs.myAge)
                {
                    return -1;
                }
            }
        }
        return -1;
    }

    //write a toString() method
    public String toString()
    {
        return(""+myAge);
    }
    
}


as you can see the comments from the original skeleton are still there, so I've followed instructions I think.

However when I compile, it gives me "Cannot find Symbol method getWeight()" and the same over for getHeight() and getAge(). I've also tried changing them to myWeight, myHeight, and myAge, but it still can't seem to find the variables.

What's wrong? blink.gif

This post has been edited by Revitalized: 8 Feb, 2008 - 09:33 AM
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Can't Find Methods/variables?
8 Feb, 2008 - 10:25 AM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
public Monster(int ht)
{
setMonster(0,ht,0); <-get rid of those and instead say myHeight = ht;
so when you run this construct it will set myHeight to whatever and the rest will still be 0
}
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Can't Find Methods/variables?
8 Feb, 2008 - 10:25 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Here's your answer:

CODE

public boolean equals( Object obj ) {
    // you create a temp var so you can properly cast your object
    Monster temp = (Monster)obj;
    // but you NEVER use it :p
    // change this:
    //return (myWeight == obj.getWeight() && myHeight == obj.getHeight() && myAge== obj.getAge());
    // to this:
    return (myWeight == temp.getWeight() && myHeight == temp.getHeight() && myAge== temp.getAge());
}


You did it right in compareTo; sort of. You're assuming that obj is of type Monster. That is not a guarentee. You want to check if obj is null and if it's the right type before you even get moving.

The compareTo has a big problem; it never returns a 0. If it did return a 0, then you could just have your equals look like return this.compareTo(obj)==0;.

Hope this helps.

This post has been edited by baavgai: 8 Feb, 2008 - 10:30 AM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month