Question

Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Rectangl2{
private int width,length;

public Rectangl2(int aWidth, int aLength) {
super();
width = aWidth;
length = aLength;
System.out.println("Inside parameterized!!!");
}

public int getWidth() {
return width;
}

public int getLength() {
return length;
}

public void setWidth(int aWidth) {
width = aWidth;
}

public void setLength(int aLength) {
length = aLength;
}

public void printInfo() {
System.out.print("This is a "+width);
System.out.println("x"+length+" rectangle.");
}

public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
Rectangl2 other = (Rectangl2) obj;
if (length != other.length)
return false;
if (width != other.width)
return false;
return true;
}
  
}

public class TestRectangle {
public static void main(String[] args) {
Rectangl2 r1 = new Rectangl2(10, 20);
Rectangl2 r2 = new Rectangl2(10, 20);
r1.printInfo();
r2.printInfo();
System.out.println("equal? "+r1.equals(r2));
}

Add a comment
Know the answer?
Add Answer to:
Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...
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
  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • 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...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two...

    My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two private instance variables, length l and width w with double data type. •There is a constructor with two parameter •There are 4 public methods which are circumference, area, getWidth, and getLength. •circumference method will return circumference to the caller.(the formula for circumference is 2 *l + 2*w ) •Area method Return the area to the caller(:l*w) •getLength method will return length to the caller....

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int...

    // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int main() {    Rectangle rectangle1;    Rectangle rectangle2;    rectangle1.setLength(10.0);    rectangle1.setWidth(5.0);    rectangle2.setLength(7.0);    rectangle2.setWidth(3.0);    cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;    cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;    cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;    cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;...

  • I need help with the C++ for the following problem: Write a program uses objected oriented...

    I need help with the C++ for the following problem: Write a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions: Attributes: width and length Member functions: setWidth(), setLength(), getWidth() , getLength(), getArea() Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(),...

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