help with equals method

help with equals method

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 7577 Views - Last Post: 19 July 2010 - 09:01 AM Rate Topic: -----

#1 xploringsound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-June 10

help with equals method

Posted 26 June 2010 - 08:22 PM

I'm working on a problem for school and I'm a bit hung up... wondering if someone can point me in the right direction.

what I'm trying to do is create a class and demo class that receives length and width of two land tracts, displays the area and makes use of an equals method to compare the areas... and throw in a toString just for kicks.

here is my first class:

public class LandTract
{
	private double length;
	private double width;
	double areaA;
	double areaB;
	
	public LandTract()
	{		
	}
	public LandTract(double len, double wid)
	{
		length = len;
		width = wid;
	}
	public double getLength() 
	{
		return length;
	}
	public void setLength(double length) 
	{
		this.length = length;
	}
	public double getWidth() 
	{
		return width;
	}
	public void setWidth(double width) 
	{
		this.width = width;
	}
	public double getArea()
	{
		return length * width;
	}
	public String toString()
	{
		String string = "The area of the land is: " + getArea();
		return string;
	}

}


here is the demo:
import java.util.Scanner; // needed for user input from keyboard

public class LandTractDemo 
{
	public static void main(String[] args) 
	{
		double length;
		double width;
		double areaA;
		double areaB;
		
		LandTract landA = new LandTract(0.0, 0.0); // two instances 
		LandTract landB = new LandTract(0.0, 0.0);
		
		Scanner keyboard =  new Scanner(System.in); // keyboard input
		
		System.out.print("What is the lenth of the 1rst tract of land? "); // get and set data for landA
		length = keyboard.nextDouble();
		landA.setLength(length);
		System.out.print("What is the width of the 1st tract of land? ");
		width = keyboard.nextDouble();
		landA.setWidth(width);
		landA.areaA = length * width;
		
		System.out.println(landA.toString());                              // output the 1st area
		
		System.out.print("\nWhat is the lenth of the 2nd tract of land? "); // get and set data for landB
		length = keyboard.nextDouble();
		landB.setLength(length);
		System.out.print("What is the width of the 2nd tract of land? ");
		width = keyboard.nextDouble();
		landB.setWidth(width);
		landB.areaB = length * width;
		
		
		System.out.println(landB.toString());                              // output 2nd area
		
		if (landA.equals(landB))
			System.out.println("\nThe two landtracts are of equal area.");
		else 
			System.out.println("\nThe two landtracts are not of equal area.");
		
	}

}



I cannot seem to get the equals method to work correctly.

Is This A Good Question/Topic? 0
  • +

Replies To: help with equals method

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10766
  • View blog
  • Posts: 40,087
  • Joined: 27-December 08

Re: help with equals method

Posted 26 June 2010 - 08:27 PM

The equals() method is initially defined in Object, and basically compares the memory addresses of the two objects using the == operator. You will have to override it in your LandTract class to compare the attributes in the invoking object with the param object.
Was This Post Helpful? 0
  • +
  • -

#3 xploringsound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-June 10

Re: help with equals method

Posted 26 June 2010 - 09:03 PM

I appreciate your quick reply, though I'm not familiar with the overriding suggestion that you mentioned, and we haven't covered it in the text yet, so I imagine there must be another way to do what I'm trying to do.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10766
  • View blog
  • Posts: 40,087
  • Joined: 27-December 08

Re: help with equals method

Posted 26 June 2010 - 09:18 PM

If you don't understand overriding and overloading methods, basically just define the equals() method in your LandTract class as such:
public boolean equals(LandTract other){
   //compare the attributes in the class 
   //to the attributes of other
}



Then when you invoke equals(), the LandTracts will be compared on their attributes, not their memory addresses.

Of course, if you didn't want to use equals(), you could manually compare their attributes. But that would be the long and non-OO way of doing things. :)
Was This Post Helpful? 0
  • +
  • -

#5 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: help with equals method

Posted 26 June 2010 - 09:23 PM

No problem man. Basically, you can't just compare landA and landB together because the .equals() method doesn't understand that you want the area variable from each. so it's trying to compare the landA object to the landB object but there are no numbers being compared. What you could do is this:

if (landA.areaA == landB.areaB)
      //and so on...

this compares the area variables of each tract

On a side note, I have a little nitpicking to do about design if you're up for it. Java is meant to be Object-oriented which entails high levels of re-use. You already have a generic land tract class, right? So why do you have to set up an areaA and areaB variable if you already have that data available in the LandTract class via the getArea() method that is set up?

Here is what I mean--you can compare the area like this:

if(landA.getArea() == landB.getArea()){
     //then do something
}



it's not really a huge deal, but it is good practice to be as conservative with data storage as possible :bigsmile: no need for two variable when you already have a method that returns that data for you.
Was This Post Helpful? 2
  • +
  • -

#6 xploringsound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-June 10

Re: help with equals method

Posted 26 June 2010 - 09:41 PM

hey luckless,

thank you very much! I actually appreciate you commenting on the design of the coding, I'm new to this and I want to learn as much as I can, so I'm thankful for your input. Also, thanks for the examples, it helps a lot.

Unfortunately I think the exercise is asking specifically to use the .equals() method and not the ==

Is this possible?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10766
  • View blog
  • Posts: 40,087
  • Joined: 27-December 08

Re: help with equals method

Posted 26 June 2010 - 09:42 PM

Re-read my last post. :)

If you don't understand overriding and overloading methods yet, just define a boolean method equals() in your LandTract class as I demonstrated in my last post.
Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: help with equals method

Posted 26 June 2010 - 09:51 PM

yeah, if you must use an equal() method, design one yourself like mac said. Overriding is fancy rhetoric for creating a method with the exact same name and your new method will take precedence. You simply need to write this method in your LandTract class with the skeleton that Mac provided.

Quote

public boolean equals(LandTract other){
   //compare the attributes in the class 
   //to the attributes of other
   return someBoolean;
}

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: help with equals method

Posted 27 June 2010 - 03:08 PM

Luckless did it for you.. almost

public boolean equals(LandTrack other) {
   return (this.getArea() == other.getArea());  
}


Was This Post Helpful? 1
  • +
  • -

#10 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: help with equals method

Posted 27 June 2010 - 04:23 PM

well, I wanted to make him work for it just a little
Was This Post Helpful? 2
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: help with equals method

Posted 27 June 2010 - 04:39 PM

View PostLuckless, on 27 June 2010 - 05:23 PM, said:

well, I wanted to make him work for it just a little

well understood but seems he needs a bigger boost than that :)
Good show anyhow :^:
Was This Post Helpful? 0
  • +
  • -

#12 xploringsound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 26-June 10

Re: help with equals method

Posted 27 June 2010 - 05:37 PM

Hey fellas, thanks for the help. I am taking my first java class, and I'm not familiar with what you guys are talking about. I don't understand what "other" stands for and how this relates to what I'm trying to do... I have tried reading up on it a bit, but I'm just not getting it right now, I think I will just have to get with my Instructor on this. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: help with equals method

Posted 27 June 2010 - 06:09 PM

View Postxploringsound, on 27 June 2010 - 06:37 PM, said:

Hey fellas, thanks for the help. I am taking my first java class, and I'm not familiar with what you guys are talking about. I don't understand what "other" stands for and how this relates to what I'm trying to do... I have tried reading up on it a bit, but I'm just not getting it right now, I think I will just have to get with my Instructor on this. Thanks again.

other is just a variable name
it is just the name we gave to the "other" LandTrack object that you are comparing with the actual one
Actually the code should have been (as LandTrack does not implement comparable<LandTract>) our mistake I apologize

public boolean equals(Object other) {  
    LandTract otherLandTrack = (LandTrack) other;         // convert Object to LandTrack
    return (this.getArea() == otherLandTrack.getArea());  // then do comparaison   
} 


Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10766
  • View blog
  • Posts: 40,087
  • Joined: 27-December 08

Re: help with equals method

Posted 27 June 2010 - 08:16 PM

I don't believe that Comparable<E> has anything to do with equals(). The Comparator<E> interface comes with an equals() method, which still accepts an Object as a param. I may have had my terminology a little backwards. If you override equals(), you need to redefine it with the Object param. So the header will look like: public boolean equals(Object other). If you want to overload equals(), you can define it with another param, like LandTract. In this case, the header will look like: public boolean equals(LandTract other).
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,887
  • Joined: 06-March 08

Re: help with equals method

Posted 27 June 2010 - 08:47 PM

View Postmacosxnerd101, on 27 June 2010 - 09:16 PM, said:

I don't believe that Comparable<E> has anything to do with equals(). The Comparator<E> interface comes with an equals() method, which still accepts an Object as a param. I may have had my terminology a little backwards. If you override equals(), you need to redefine it with the Object param. So the header will look like: public boolean equals(Object other). If you want to overload equals(), you can define it with another param, like LandTract. In this case, the header will look like: public boolean equals(LandTract other).

Nope, sorry :)
class A {
    public boolean equals(A otherObject) {
    }
}


does NOT overload equals(Object o)
you have to do

class A implements Comparable<A> {
    public boolean equals(A other) {
    ...
    }
}


if you want YOUR equals() method to be called, if you don't you have to overload equals(Object o) and cast o to an instance of the class
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3