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...
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...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT