Argh...what am i doing wrong here??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Argh...what am i doing wrong here??

  1. #1
    Join Date
    May 2005
    Posts
    115

    Argh...what am i doing wrong here??

    Fairly new to Java. I have been struggling with this problem for some time...hope u guys can help me...

    AnibotTester.java
    -----------------
    public class AnibotTester
    { public static void main (String[] args)
    { Anibot horse = new Anibot("horse","canter",2.0,2.5);
    horse.report();

    Anibot spider = new Anibot("spider","crawl",1.3,0.2);
    spider.report();

    Anibot snake = new Anibot("snake","slither",3.4);
    snake.report();

    Anibot duck = new Anibot("duck","waddle");
    duck.report();
    duck.setSpeed(0.75);
    duck.report();

    Anibot cat = new Anibot("cat");
    cat.report();

    Anibot anibot = new Anibot();
    anibot.report();
    anibot.move(1.0);
    anibot.setName("worm");
    anibot.setMove("inch");
    anibot.setSpeed(0.087);
    anibot.setLocation(45.3221);
    anibot.report();
    anibot.move(5.3);

    duck.report();
    duck.move(2.0);
    duck.report();

    int i;

    for (i=0; i<2; i++)
    { Anibot duck2 = new Anibot("duck", "waddle", 1.5, 0.75);
    if (duck2.equals(duck))
    System.out.println("The two ducks are equal.");
    else
    System.out.println("The two ducks are different.");

    System.out.println("Duck 1 is " + duck);
    System.out.println("Duck 2 is " + duck2);

    duck.setSpeed(0.76);
    }

    }
    }
    -------------------------------------------------------------------------
    Anibot.java
    -----------
    public class Anibot{

    private String name;
    private String move;
    private double location;
    private double speed;

    //---------Constructors----------------
    public Anibot(String name, String move, double location, double speed){
    this.name=name;
    this.move=move;
    this.location=location;
    this.speed=speed;
    }
    public Anibot(String name, String move, double location){
    this(name, move,location,1.0);
    }
    public Anibot(String name, String move){
    this(name, move,0.0,1.0);
    }
    public Anibot(String name){
    this(name,"move",0.0,1.0);
    }
    public Anibot(){
    name="anibot";
    move="move";
    location=0.0;
    speed=1.0;
    }

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

    public void setMove(String move){
    this.move=move;
    }

    public void setLocation(double location){
    this.location=location;
    }

    public void setSpeed(double speed){
    this.speed=speed;
    }

    public void report(){
    System.out.println("The " +name+ " is " +location+ " meters from the start of ");
    System.out.println("the track and its speed is " +speed+ " meters per second.");
    }

    public void move(double n){
    double newLoc=n;
    double moves=newLoc*speed;

    int abc=(int)moves;
    for(int i=abc;i>=0;i--){
    System.out.println(move);
    }

    location+=moves; //location=location+moves;

    System.out.println("The " +name+ " is now " +location+ " meters");
    System.out.println("from the start of the track");
    }

    public boolean equals(Anibot a){
    if(a==null)
    return false;
    else
    if(name.equals(a.name)&&move.equals(a.move)&&(a.location==location)&&(a.speed==speed)){
    return true;
    }

    }
    }

    what am i doing wrong with my 'equals'? For the last few lines of AnibotTester i keep getting the address or duck1 & duck2 as output instead of (notice last 5 lines)

    Correct output
    --------------
    The horse is 2.0 meters from the start of the track and its speed is 2.5 meters per second.
    The spider is 1.3 meters from the start of the track and its speed is 0.2 meters per second.
    The snake is 3.4 meters from the start of the track and its speed is 1.0 meters per second.
    The duck is 0.0 meters from the start of the track and its speed is 1.0 meters per second.
    The duck is 0.0 meters from the start of the track and its speed is 0.75 meters per second.
    The cat is 0.0 meters from the start of the track and its speed is 1.0 meters per second.
    The anibot is 0.0 meters from the start of the track and its speed is 1.0 meters per second.
    move
    The anibot is now 1.0 meters from the start of the track.
    The worm is 45.3221 meters from the start of the track and its speed is 0.087 meters per second.
    inch
    inch
    inch
    inch
    inch
    The worm is now 45.7832 meters from the start of the track.
    The duck is 0.0 meters from the start of the track and its speed is 0.75 meters per second.
    waddle
    waddle
    The duck is now 1.5 meters from the start of the track.
    The duck is 1.5 meters from the start of the track and its speed is 0.75 meters per second.
    The two ducks are equal.
    Duck 1 is Anibot name: duck move: waddle speed: 0.75 Location: 1.5
    Duck 2 is Anibot name: duck move: waddle speed: 0.75 Location: 1.5
    The two ducks are different.
    Duck 1 is Anibot name: duck move: waddle speed: 0.76 Location: 1.5
    Duck 2 is Anibot name: duck move: waddle speed: 0.75 Location: 1.5

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Though I'm not sure if this is your exact question, the way that you customize the way that objects are written as strings is by overloading the toString() method. For example you might try something like this in the Anibot class:
    Code:
    public String toString() {
      return "Anibot name: " + name + " move: " + move + " speed: " + speed + " Location: " + location;
    }
    If you don't overload toString(), java will print out the internal name of the object which is a combination of the class type and the memory location it is being stored at.

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    May 2005
    Posts
    115
    Ic, i'll try that..

    But if i overload the toString() method, if lets say if i want to print some other strings, does it mean that i cant print it in other way?

  4. #4
    Join Date
    May 2005
    Posts
    115
    hey evlich, ur right! i finally got the corrent output..thanks man..for reading my lengthy code

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I fixed it...

    ...but i didn't see that it had already been (almost) answered.

    This is how the public boolean equals method should be implemented

    Code:
    /**
       * The equals method should use Object as parameter in order to be utilized
       * by the standard java classes (e.g. ArrayList lookup functions).
       */
      public boolean equals(Object ob) {
        boolean eq = false;
        if (ob != null && ob instanceof Anibot) {
          Anibot a = (Anibot) ob;
          if (name.equals(a.name) && move.equals(a.move) && (a.location == location) &&
              (a.speed == speed)) {
            eq = true;
          }
        }
        return eq;
      }
    eschew obfuscation

  6. #6
    Join Date
    May 2005
    Posts
    115
    Quote Originally Posted by sjalle
    /**
    * The equals method should use Object as parameter in order to be utilized
    * by the standard java classes (e.g. ArrayList lookup functions).
    */
    Ur suggesting that i put Object instead of Anibot as argument so that other objects other that Anibot objects can also share the equals method, right?

    Quote Originally Posted by sjalle
    Anibot a = (Anibot) ob;
    Are u casting ob into an Anibot type? Why do u have to cast ob into Anibot again?
    Last edited by Ant_Magma; 07-19-2005 at 08:03 AM.

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Partially right, a method is recognized by its signature, that is the name, the
    parameter type(s) and their sequence if there are more than one parameter.
    This is necessary for, say, being able to use the lookup functions in ArrayList; you
    don't have to loop the list to find an object, as the ArrayList class will use the
    the method with this exact signature:

    public boolean equals (Object ob)

    if its found for the objects contained in the list.

    If not it will compare by address (or reference as some insists to call it). That
    will only give a match for identical objects, e.g. the same memory location. Its
    tha same as:
    if (anObject==anotherObject).

    In other oop languages you can implement operator overloading, and
    infact code the comparison process that is to be done for the if statement
    above. Java has left this feature out, although it woldn't be a bad idea in
    my opinion. Instead java has the option offered by the equals method.

    What you want is comparing the objects contents.
    ----

    I'm not casting ob into an Anibot instance more than once in the method. The
    Object parameter is there to override the public boolen equals(Object ob)
    method of the Object class; the root of all java classes.
    Last edited by sjalle; 07-19-2005 at 08:46 AM. Reason: yak yak
    eschew obfuscation

  8. #8
    Join Date
    May 2005
    Posts
    115
    Oic..thx sjalle!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center