Skip to content

Instantly share code, notes, and snippets.

@jacobtender
Created April 10, 2019 02:21
Show Gist options
  • Save jacobtender/f815aa140b91ee8f9864beb1cd223730 to your computer and use it in GitHub Desktop.
Save jacobtender/f815aa140b91ee8f9864beb1cd223730 to your computer and use it in GitHub Desktop.
Solved Source for : LandTract.java
/*
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.)
Write a separate program that asks the user to enter the dimensions for the two tracts of
land (in the order length of the first, width of the first, length of the second, width of the second).
The program should print the output of two tracts' toString methods followed by a sentence stating whether
or not the tracts have equal dimensions. (If the tracts have the same dimensions, print,
"The two tracts have the same size." Otherwise, print, "The two tracts do not have the same size.")
Print all three statements on separate lines.
*/
import java.util.Scanner;
public class LandTract {
public int length, width, area;
public LandTract(){
length = 0;
width = 0;
area= 0;
}
public LandTract(int length, int width) {
this.length = length;
this.width = width;
}
public void setArea (int area){
this.area = area;
}
// Area
public static int area(int length, int width) {
return length * width;
}
// Equals
public boolean equals(LandTract lt) {
boolean equals;
if (length == (lt.length) && width == (lt.width) || length == (lt.width) && width == (lt.length))
equals = true;
else
equals = false;
return equals;
}
// ToString
public String toString() {
String str = ("LandTract" + " with length " + length + ", width "
+ width + ", and area " + area);
return str;
}
public static void main(String[] args) {
int length1 = 0;
int width1 = 0;
int area1 = 0;
int length2 = 0;
int width2 = 0;
int area2 = 0;
Scanner keyboard = new Scanner(System.in);
// first tract
System.out.print("Enter length of first land tract:");
length1 = keyboard.nextInt();
System.out.print("Enter width of first land tract:");
width1 = keyboard.nextInt();
LandTract land1 = new LandTract(length1,width1);
area1 = LandTract.area(length1,width1);
land1.setArea(area1);
// second tract
System.out.print("Enter length of second land tract:");
length2 = keyboard.nextInt();
System.out.print("Enter width of second land tract:");
width2 = keyboard.nextInt();
LandTract land2 = new LandTract(length2,width2);
area2 = LandTract.area(length2,width2);
land2.setArea(area2);
// display
System.out.println(land1.toString());
System.out.println(land2.toString());
if (land1.equals(land2))
System.out.println("The two tracts have the same size.");
else
System.out.println("The two tracts do not have the same size.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment