Question

Make a LandTract class with the following fields: LandTract is a rectangle. * length - an...

Make a LandTract class with the following fields: LandTract is a

rectangle.

* length - an int containing the tract's length

* width - an int containing the tract's width

The class should have a constructor with two arguments length and width and assign these to

the class variables.

Now define a Copy Constructor: The constructor that you will write will be a copy constructor

that uses the parameter LandTract object to make a duplicate LandTract object, by copying the

value of each instance variable (length and width) from the parameter object to the the new copy

object.

You also need to define the following methods:

• equals - takes another LandTract object as a parameter and returns a boolean (true or false)

saying whether or not the two tracts have the same dimensions (length and width).

Compare the length and width of the tract given as parameter to the equals method with the

length and width of the other LandTract object.

• toString - returns a String with details about the LandTract object in the format:

“LandTract object has a length of 30 and width of 40”

(If, for example, the LandTract object had a length of 30 and a width of 40.)

Write a separate demo class that asks the user to enter the dimensions for the two tracts of land

using Scanner object. Then, create another LandTract object as a copy of the first LandTract

object. It should print the output of two tracts' using the toString method, and should also print a

statement 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.")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN JAVA:

LandTract.java file:


public class LandTract {
   double width;
   double length;

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

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

   public boolean equals(LandTract obj) {
       if (this.width == obj.width && this.length == obj.length)
           return true;
       return false;
   }

   public String toString() {
       return "LandTract object has a length of " + length + ", and width of " + width;
   }

}

DemoTract.java file:

import java.util.Scanner;

public class DemoTract {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       double length, width;
       System.out.print("Enter dimensions of a LandTract : ");
       length = sc.nextDouble();
       width = sc.nextDouble();
       LandTract lt1 = new LandTract(length, width);
       System.out.print("Enter dimensions of another LandTract : ");
       length = sc.nextDouble();
       width = sc.nextDouble();
       LandTract lt2 = new LandTract(length, width);
       LandTract lt3 = new LandTract(lt1);
       System.out.println(lt1);
       System.out.println(lt2);
       if (lt1.equals(lt2))
           System.out.println("The two tracts have the same size");
       else
           System.out.println("The two tracts do not have the same size");
   }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Make a LandTract class with the following fields: LandTract is a rectangle. * length - an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Write a class named Room that has two fields one for the room’s length and one...

    Write a class named Room that has two fields one for the room’s length and one for the width. The class should have a constructor that sets the length and the width and a method named “getArea” that returns the room’s area (area = ½ length x width). It should also have a method named “isEqual” (with a Room argument) that determines if the two rooms are equal or not. (25 points). Do not write the demo to test this...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....

    Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp. Also uses Super in the TextBook Class section. Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attributes, title and price, and get/set methods for the two attributes. - price: double Вook) Book(String, double) getTitle(): String setTitle(String): void + getPrice() double setPrice(double): void toString() String + The first constructor doesn't...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables,...

    Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables, a constructor with two parameter to initialize length and width, set and get methods for each variables and the following methods: a. The first method calculates and returns the value of the ratio of the length to the width of the rectangle. b. The second method determines whether a rectangle is a Square depending on the value of the ratio, which should be calculated...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT