Skip to content
Advertisement

I can’t seem to figure out why I keep getting true when I clearly overridden the equality method

I’m trying to figure this out but I can’t seem to get it to compare correctly.

As I try to setup the code whenever I run it the result would end up becoming True when I need it to produce a false test as well. Extensive testing shows it to be always true and I have no idea how to produce a false on it.

import java.util.Scanner;

public class LandTract
{
    // instance variables
    private static double length , width, area;

    /**
     * Constructor for objects of class LandTract
     */
    public LandTract(double length, double width, double area)
    {
        // initialise instance variables
        length = 0;
        width = 0;
    }

    public LandTract(double length, double width)
    {
        this.length = length;
        this.width = width;
    }
    
    public void setLength(double length)
    {
        this.length = length;
    }
    
    public double getLength()
    {
        return length;
    }
    
    public void setWidth(double width)
    {
        this.width = width;
    }
    
    public double getWidth()
    {
        return width;
    }
    
    public double getArea()
    {
        return area = length * width;
    }
    
    public String toString()
    {
        String str = "Length: " + length + "nWidth: " + width;
        return str;
    }
    
    public boolean equals(Object obj)
    {
        LandTract land = (LandTract) obj;
        if (this.length != land.length)
            return false;
        if (this.width != land.width)
            return false;
        if (this.area != land.area)
            return false;
        
            return true;
    }
    
    public static void main(String[] args)
    {
        Scanner key = new Scanner(System.in);
        
        System.out.print("Enter the length of the first tract of land: ");
        length = key.nextDouble();
        key.nextLine();
        System.out.print("Enter the width of the first tract of land: ");
        width = key.nextDouble();
        key.nextLine();
        LandTract land1 = new LandTract(length , width);
        System.out.println("The area of the first tract of land is " + land1.getArea());
        System.out.println();
        
        System.out.print("Enter the length of the second tract of land: ");
        length = key.nextDouble();
        key.nextLine();
        System.out.print("Enter the width of the second tract of land: ");
        width = key.nextDouble();
        key.nextLine();
        LandTract land2 = new LandTract(length, width);
        System.out.println("The area of the second tract of land is " + land2.getArea());
        System.out.println();
        
        if (land1.equals(land2))
            System.out.println("Both tracts of land are the same size.");
        else
            System.out.println("They are different sizes.");
    }
}

Advertisement

Answer

The best example for a confusing & ironically erroneous comment:

// instance variables
private static double length , width, area;

The program works much better, when you:

  1. (Really) Introduce instance variables:

    private double length , width, area;
    
  2. Fix compiler problems in main method (by declaring local variables with the same identifier ..no good style but quick):

    public static void main(String[] args) {
       double length, width;
       // ...
    }
    
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement