-1

Question :

Make a LandTract class that has two fields: one for the tract’s length and one for the width. The class should have a method that returns the tract’s area, as well as an equals method and a toString method. Demonstrate the class in a program that asks the user to enter the dimensions for two tracts of land. The program should display the area of each tract of land and indicate whether the tracts are of equal size.

LandTract class

public class LandTract {

    private double width,
            length;

    public LandTract(double width, double length) {
        this.width = width;
        this.length = length;
    }

    public double getLength() {
        return length;
    }

    public LandTract(double length) {
        this.length = length;
    }


    public double tractArea(double width, double length) {
        return width * length;

    }

    public String equal(LandTract zt, LandTract lt){

        String dec;
        if(zt.equals(lt)){
           dec ="yes";
        }else dec="NO";

        return dec;
    }

    public String toString(){
        return "Area is " + width*length;
    }

}

DemoTract class

import java.util.Scanner;

public class DemoTract {

    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);

        System.out.println("Please Enter First Tract Width");
        double width=scan.nextDouble();
        System.out.println("Please Eter First Trach Length ?");
        double length=scan.nextDouble();

        LandTract  lt1 = new LandTract(width,length);

        System.out.println("Please Enter Secont Tract Width");
        double width2=scan.nextDouble();
        System.out.println("Please Enter Secoinf Tract Length");
        double length2 = scan.nextDouble();

        LandTract lt2 = new LandTract(width2,length2);

        System.out.println("First Tract " + lt1);
        System.out.println("Secon Tract "+ lt2);
        System.out.println(lt2.equal(lt1, lt2));






    }


}

object equal is always false, What is wrong with me ?

Zindik
  • 1
  • 1
    Also, tractArea should not take any argument: it's supposed to return the area of the LandTract. Your equal method should take a single argument: it's supposed to compare the LandTract (i.e. `this`) to another one. And it should return a boolean, not a String. Finally, the point of the equal method is to compare the width and height of `this` with the ones of the other LandTract. equals() doesn't do that (well, it would if you averrode is and made it do that). – JB Nizet Apr 08 '17 at 21:59
  • 1
    `LandTract` needs to override `Object#equals(Object obj)` which returns a `boolean`. Currently `zt.equals(lt)` is using the `.equals(...)` method of `Object` which is just checking for equality between `LandTract` *references* (can't remember the exact term). – Jonny Henly Apr 08 '17 at 22:04
  • What do you mean "equal size"? Do the dimensions have to be the same, or just the area? If the problem is to check whether the areas are equal, it is _not_ good practice to use the `equals` method for that. `equals` should be true only if all publicly available characteristics (by some definition) of the two objects are the same, and here the length is publicly available. Here I think you simply want `zt.tractArea() == lt.tractArea()`, or perhaps compare with an epsilon to avoid floating-point accuracy issues. – ajb Apr 08 '17 at 22:36

1 Answers1

1

In order to check object's equality properly, you need to override equals(Object obj) and hashcode() methods from java.lang.Object to check the object's equality as shown below:

public class LandTract {

    //add your existing code here

    @Override
    public boolean equals(Object object){

        boolean areObjectsEqual = false;

        if(object instanceof LandTract){
           LandTract landTract = (LandTract)object;
            //check the condition on how this & passed objects are equal
           if(this.width == landTract.width && this.length == landTract.length) {
             areObjectsEqual = true;
           } 
        } 
        return areObjectsEqual;
    }

    @Override
    public int hashCode() {
      return Double.valueOf(this.width+this.length).hashCode();
    }
}

In your main() method, you can now check equality of the both of the LandTrack objects using equals() method as shown below:

System.out.println(lt1.equals(lt2));

One more important point here is that as you are using double for width and length, you might face rounding issues (look here for more details) and you might need to consider using BigDecimal, if you have to avoid the problems.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • The question is wrong in so many ways, but I can't help to comment - your solution isn't quite correct - the areas can be same if this.width x this.length == landTract.width x landTract.length which doesn't mean this attributes have the same value on both objects :-) – D00de Apr 08 '17 at 22:18
  • Also, change System.out.println(lt1.equals(lt1,lt2)); to System.out.println(lt1.equals(lt2)); :-) – D00de Apr 08 '17 at 22:29
  • great spot, updated – Vasu Apr 08 '17 at 22:29
  • Thank You so much. I got that. – Zindik Apr 09 '17 at 21:57
  • great, you can accept my answer if this helps – Vasu Apr 09 '17 at 21:58