53.8k views
4 votes
Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods: • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if the length of the first is the same as the width of the other and vice versa, that counts as having equal dimensions.) • toString - returns a String with details about the LandTract object in the format: LandTract object with length 30 and width 40 (If, for example, the LandTract object had a length of 30 and a width of 40.)

User Garett
by
5.5k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

Java Code:

class LandTract {

private int length;

private int width;

public LandTract(int length, int width) {

this.length = length;

this.width = width;

}

public int areaa(){

return length*width;

}

public int getLength() {

return length;

}

public void setLength(int length) {

this.length = length;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

@Override

public String toString() {

return "LandTract{" +

"length=" + length +

", width=" + width +

'}';

}

@Override

public boolean equals(Object o)

}

Code Explanation:

Main part of above code is equals method. First check that the passed object in parameter in equals method is equals to current object. If yes then return true.

Otherwise check the class of parameter object is similar to current class. If not then it means the class of parameter object is different so we return false.

Otherwise explicitly convert the object to Our LandTrack class.

Then check both condition where the length and width are equals to length and width, if yes then return true.

Example Main Method

public static void main(String []args){

LandTract landTract1 = new LandTract(21,19);

LandTract landTract2 = new LandTract(19,21);

System.out.println(landTract1.toString());

System.out.println(landTract2.toString());

System.out.println(landTract1.equals(landTract2));

}

Expected Output

LandTract{length=21, width=19}

LandTract{length=19, width=21}

true

User Salim Fadhley
by
5.1k points