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.