Question

In: Computer Science

Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a...

Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:

  •  a constructor that accepts arguments for the two fields

  •  a method that returns the tract’s area(i.e. length * width)

  •  an equals method that accepts a LandTract object as an argument. If the argument object

    holds the same data (i.e. length and width) as the calling object, this method should return

    true. Otherwise, it should return false.

  •  a toString method (that returns a String representation of the tract’s length, width, and

    area).

    Demonstrate the class in a program by creating an array of three LandTract objects initialized with user input. Then, print the info of the LandTract objects stored in the array by invoking the toString method (implicitly or explicity). Also, use the equals method to compare at least two LandTract objects and print wether they are of equal size or not.

    Sample Output:

Enter the length of Tract 1: 2

Enter the length of Tract 1: 3

Enter the length of Tract 2: 2

Enter the length of Tract 2: 3

Enter the length of Tract 3: 4

Enter the length of Tract 3: 5

Info of LandTract 1:

Length = 2.0

Width = 3.0

Area = 6.0

Info of LandTract 2:

Length = 2.0

Width = 3.0

Area = 6.0

Info of LandTract 3:

Length = 4.0

Width = 5.0

Area = 20.0

The first and second tracts are the same size.

Solutions

Expert Solution

Find below the code for above case

// LandTract class that has two fields (i.e. instance variables)

LandTract.java

public class LandTract {

double length;

double width;

public LandTract(double length, double width) { // constructor that accepts arguments for the two fields

this.length = length;

this.width = width;

}

public double getArea() {

return (this.length * this.width); //  method that returns the tract’s area(i.e. length * width)

}

public boolean equals(LandTract obj) { //  equals method that accepts a LandTract object as an argument

if(this.length == obj.length && this.width == obj.width)

return true;

else

return false;

}

public String toString() { // toString method

return "Length = "+this.length+"\nWidth = "+this.width+"\nArea = "+getArea()+"\n";

}

}

Test82.java

import java.util.*;

class Test82 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

  

LandTract objs[] = new LandTract[3]; //  an array of three LandTract objects initialized with user input

  

double l, w;

  

for(int i = 0; i < 3; i++) {

System.out.print("Enter the length of Tract "+(i+1)+": ");

l = sc.nextDouble();

System.out.print("Enter the width of Tract "+(i+1)+": ");

w = sc.nextDouble();

objs[i] = new LandTract(l, w);

System.out.println();

}

  

System.out.println();

for(int i = 0; i < 3; i++) {

System.out.println("Info of LandTract "+(i+1)+":");

System.out.println(objs[i]);

}

  

System.out.println();

  

if(objs[0].equals(objs[1])) {

System.out.println("The first and second tracts are the same size.");

} else {

System.out.println("The first and second tracts are not of the same size.");

}

}

}


Related Solutions

Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a...
Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and width) as the calling object, this method should...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Make a LandTract class with the following fields: • length - an int containing the tract's...
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...
Make a LandTract class with the following fields: • length - an int containing the tract's...
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...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT