Question

In: Computer Science

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 the length of the first is the same as the width of the other and vice versa, that counts as having equal dimensions.)
• toString - returns a String with details about the LandTract object in the format:
LandTract object with length 30 and width 40
(If, for example, the LandTract object had a length of 30 and a width of 40.)

Write a separate program that asks the user to enter the dimensions for the two tracts of land (in the order length of the first, width of the first, length of the second, width of the second). The program should print the output of two tracts' toString methods followed by a sentence stating whether or not the tracts have equal dimensions. (If the tracts have the same dimensions, print, "The two tracts have the same size." Otherwise, print, "The two tracts do not have the same size.") Print all three statements on separate lines.

Sample Run
java LandTract

Enter·length·of·first·land·tract:10↵
Enter·width·of·first·land·tract:55↵
Enter·length·of·second·land·tract:36↵
Enter·width·of·second·land·tract:75↵
LandTract·with·length·10,·width·55,·and·area·550↵
LandTract·with·length·36,·width·75,·and·area·2700↵
The·two·tracts·do·not·have·the·same·size.↵

Solutions

Expert Solution

//LandTract.java
public class LandTract {
    int length, width;

    public LandTract(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public int area(){
        return length*width;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LandTract landTract = (LandTract) o;
        return (length == landTract.length && width == landTract.width) ||
                (width == landTract.length && length == landTract.width);
    }

    public String toString() {
        return "LandTract object with length "+length+" and width "+width;
    }
}

///////////////////////////////////////////////////////////////////////////

import java.util.Scanner;

//LandTractTest.java
public class LandTractTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter·length·of·first·land·tract:");
        int length1 = scanner.nextInt();
        System.out.print("Enter·width·of·first·land·tract:");
        int width1 = scanner.nextInt();

        System.out.print("Enter·length·of·second·land·tract:");
        int length2 = scanner.nextInt();
        System.out.print("Enter·width·of·second·land·tract:");
        int width2 = scanner.nextInt();

        LandTract landTract1 = new LandTract(length1, width1);
        LandTract landTract2 = new LandTract(length2, width2);

        System.out.println("LandTract·with·length·"+landTract1.length+",·width·"+landTract1.width+",·and·area·"+landTract1.area());
        System.out.println("LandTract·with·length·"+landTract2.length+",·width·"+landTract2.width+",·and·area·"+landTract2.area());

        if(landTract1.equals(landTract2)){
            System.out.println("The·two·tracts·have·the·same·size.");
        }
        else{
            System.out.println("The·two·tracts·do·not·have·the·same·size.");
        }
    }
}


Related Solutions

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...
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...
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...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the...
Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int...
Create a TeeShirt class for Toby’s Tee Shirt Company. Fields include: orderNumber - of type int size - of type String color - of type String price - of type double Create set methods for the order number, size, and color and get methods for all four fields. The price is determined by the size: $22.99 for XXL or XXXL, and $19.99 for all other sizes. Create a subclass named CustomTee that descends from TeeShirt and includes a field named...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT