I am having trouble figuring out how to correctly put in an equal method that co
ID: 3621290 • Letter: I
Question
I am having trouble figuring out how to correctly put in an equal method that compares objects into my program that compares land tracts. Also when I compile the program it says java.lang.NoSuchMethodError: mainException in thread "main
My program is given below.
These are the given requirements:
Requirements:
? The application should be named LandArea.java
? An equals method which compares objects must be used to compare the tracts.
? A toString method must be used.
? A constructor must be coded in the LandTract class
? Submit LandArea.java and LandTract.java through the assignment tool in Moodle.
? Include comments defining the purpose of the code.
Here is my program(2 parts):
//This is a program that compares tracts of land. Michael Phillips 800682305
import java.io.*;
class LandArea
{
private double length;
private double width;
public LandArea(double l, double w)
{
length = l;
width = w;
}
//This returns the area back
public double getArea()
{
return width*length;
}
public String toString()
{
String str = "Tract length= " + length + "Tract width= "+ width + " Area = " + getArea();
return str;
}
}
//2ND PART::::(Has a couple errors)
public class LandTract
{
public static void main(String[] args)
{
LandTract myLandTract1 = new LandTract(10,10);
LandTract myLandTract2 = new LandTract(30,40);
//outputs data with toString
System.out.println("Tract 1 = " + myLandTract1);
System.out.println("Tract 2 = " + myLandTract2);
//compares class variables
if(myLandTract1.equals(myLandTract2))
System.out.println("Tracts 1 & 2 are equal!");
else
System.out.println("The two tracts are equal");
//Ends program
System.exit(0);
}
}
Explanation / Answer
// save following program as LandArea.java import java.io.*; public class LandArea { private double length; private double width; public LandArea(double l, double w) { length = l; width = w; } //This returns the area back public double getArea() { return width*length; } public String toString() { String str = "Tract length= " + length + "Tract width= "+ width + " Area = " + getArea(); return str; } public boolean equals(LandArea ld) { if(this.length == ld.length && this.width == ld.width) return true; else return false; } } // save following programm as LandTract.java public class LandTract { public static void main(String[] args) { LandArea myLandTract1 = new LandArea(10,10); LandArea myLandTract2 = new LandArea(30,40); //outputs data with toString System.out.println("Tract 1 = " + myLandTract1); System.out.println("Tract 2 = " + myLandTract2); //compares class variables if(myLandTract1.equals(myLandTract2)) System.out.println("Tracts 1 & 2 are equal!"); else System.out.println("The two tracts are not equal"); //Ends program System.exit(0); } } // now keep both programs in same folder and run LandTract.java (in netbeans or // elcispe) // else compile two files using javac and run LandTract using java intrepretor.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.