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

Two classes LandTrack and Test are provided in two separate files.

Test class is the driver class with the main method

Instructions on running the program from a terminal:

- javac LandTrack.java Test.java

- java Test

LandTract.java

public class LandTract{
        
        // Instance variables representing length and width of land tract
        private double length;
        private double width;

        // Parameterized constructor
        LandTract(double length, double width) {
                this.length = length;
                this.width = width;
        }

        // Method to get Area of land tract
        public double getArea() {
                return length * width;
        }

        // Getter method for private field length
        public double getLength() {
                return length;
        }  

        // Setter method for private field length
        public void setLength(double length) {
                this.length = length;
        }

        // Getter method for private field width
        public double getWidth() {
                return width;
        }

        // Setter method for private field width
        public void setWidth(double width) {
                this.width = width;
        }

        // Comparing this object with another object
        public boolean equals(LandTract obj) {
                if (obj.getLength() == this.length && obj.getWidth() == this.width) {
                        return true;
                }
                return false;
        }

        // String representation of object
        @Override
        public String toString() {
                return "Length = " + length + "\nWidth = " + width + "\nArea = " + getArea();
        }

}

Test.java (Driver class)

import java.util.*;

// Driver class
public class Test {
        public static void main(String[] args) {

                // Using scanner to read user input
                Scanner sc = new Scanner(System.in);
                
                // Size of the array
                int arrSize = 3;

                // Array to store LandTract objects
                LandTract[] landTractArr = new LandTract[arrSize];
                
                // Initializing the landTractArr by user input
                for(int i = 0; i < arrSize; i++) {
                        System.out.print("Enter the length of Tract " + (i+1) + ": ");
                        double length = sc.nextDouble();
                        System.out.print("Enter the width of Tract " + (i+1) + ": ");
                        double width = sc.nextDouble();
                        landTractArr[i] = new LandTract(length, width);
                }

                // Invoking toString method to print details of objects
                for(int i = 0; i < arrSize; i++) {
                        System.out.println("\nInfo of LandTract " + (i+1) + ":");
                        System.out.println(landTractArr[i]);
                }

                // Comparing first and second objects
                if(landTractArr[0].equals(landTractArr[1])) {
                        System.out.println("\nThe first and second tracts are the same size.");
                } else {

                        System.out.println("\nThe first and second tracts are NOT 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...
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...
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 class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
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...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT