Question

In: Computer Science

Write the definition for a generic class called Rectangle that has data members length and width....

  1. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions:
    setlength to set the length data member
    setwidth to set the width data member
    perimeter to calculate and return the perimeter of the rectangle
    area to calculate and return the area of the rectangle
    show to return a text to display the length and width of the rectangle
    sameArea that has one parameter of type Rectangle. sameArea returns 1 if the two Rectangles have the same area, and returns 0 if they don't.

Overload the following operators: -

Operators

Description

- (unary)

Length and width will change sign

+

Add bother areas and then find the ratio of the first Rectangle to find the new values of the length and the width

-

Subtract second area from first area and then find the ratio of the first Rectangle to find the new values of the length and the width

*

Multiply first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width

/

Divide first area by second area and then find the ratio of the first Rectangle to find the new values of the length and the width

<

Compare the areas

>

Compare the areas

<=

Compare the areas

>=

Compare the areas

==

Compare the areas

!=

Compare the areas

=

Assign the length and width to the first Rectangle

+=

Same as + operator and assign the result to the first Rectangle

-=

Same as - operator and assign the result to the first Rectangle

*=

Same as * operator and assign the result to the first Rectangle

/=

Same as / operator and assign the result to the first Rectangle

Solutions

Expert Solution

// C++ program to create the class Rectangle

#include <iostream>

#include <cmath>

using namespace std;

const double EPSILON = 1e-5; // used for comparison

template <class T>

class Rectangle

{

private :

       T length, width;

public:

       void setLength(T length);

       void setWidth(T width);

       T perimeter() const;

       T area() const;

       void show() const;

       bool sameArea(const Rectangle<T> &other) const;

       void operator-();

       Rectangle<T>& operator+(const Rectangle<T> &other);

       Rectangle<T>& operator-(const Rectangle<T> &other);

       Rectangle<T>& operator*(const Rectangle<T> &other);

       Rectangle<T>& operator/(const Rectangle<T> &other);

       bool operator<(const Rectangle<T> &other) const;

       bool operator>(const Rectangle<T> &other) const;

       bool operator<=(const Rectangle<T> &other) const;

       bool operator>=(const Rectangle<T> &other) const;

       bool operator==(const Rectangle<T> &other) const;

       bool operator!=(const Rectangle<T> &other) const;

       void operator=(const Rectangle<T> &other);

       void operator+=(const Rectangle<T> &other);

       void operator-=(const Rectangle<T> &other);

       void operator*=(const Rectangle<T> &other);

       void operator/=(const Rectangle<T> &other);

};

// function to set the length of rectangle

template <class T>

void Rectangle<T>:: setLength(T length)

{

       this->length = length;

}

// function to set the width of rectangle

template <class T>

void Rectangle<T>::setWidth(T width)

{

       this->width = width;

}

// function to return the perimeter of rectangle

template <class T>

T Rectangle<T>::perimeter() const

{

       return(length+width);

}

// function to return the area of rectangle

template <class T>

T Rectangle<T>::area() const

{

       return(length*width);

}

// function to display the length and width of rectangle

template <class T>

void Rectangle<T>:: show() const

{

       cout<<"Rectangle of length : "<<length<<" and width : "<<width<<endl;

}

// function to return if the two rectangles have same area

template <class T>

bool Rectangle<T>::sameArea(const Rectangle<T> &other) const

{

       return(fabs(area()-other.area()) < EPSILON);

}

// function to decrement the length and width of the rectangle by 1

template <class T>

void Rectangle<T>::operator-()

{

       length = -1*length;

       width = -1*width;

}

// function to add both areas and then find the ratio of the first Rectangle to find the new values of the length and the width

template <class T>

Rectangle<T>& Rectangle<T>::operator+(const Rectangle<T> &other)

{

       Rectangle<T> *result = new Rectangle<T>();

       T req_area = area()+other.area();

       T ratio = area()/req_area;

       result->length = result->length*ratio;

       result->width = result->width*ratio;

       return result;

}

// function to subtract both the second area from the first and then find the ratio of the first Rectangle to find the new values of the length and the width

template <class T>

Rectangle<T>& Rectangle<T>::operator-(const Rectangle<T> &other)

{

       Rectangle<T> *result = new Rectangle<T>();

       T req_area = fabs(area()-other.area());

       T ratio = area()/req_area;

       result->length = result->length*ratio;

       result->width = result->width*ratio;

       return result;

}

// function to multiply both areas and then find the ratio of the first Rectangle to find the new values of the length and the width

template <class T>

Rectangle<T>& Rectangle<T>::operator*(const Rectangle<T> &other)

{

       Rectangle<T> *result = new Rectangle<T>();

       T req_area = area()*other.area();

       T ratio = area()/req_area;

       result->length = result->length*ratio;

       result->width = result->width*ratio;

       return result;

}

// function to divide the first area by the second and then find the ratio of the first Rectangle to find the new values of the length and the width

template <class T>

Rectangle<T>& Rectangle<T>::operator/(const Rectangle<T> &other)

{

       Rectangle<T> *result = new Rectangle<T>();

       T req_area = area()/other.area();

       T ratio = area()/req_area;

       result->length = result->length*ratio;

       result->width = result->width*ratio;

       return result;

}

// function to return if first rectangle is less in area than second rectangle

template <class T>

bool Rectangle<T>::operator<(const Rectangle<T> &other) const

{

       return((fabs(area()-other.area()) >= EPSILON) && (area() < other.area()));

}

// function to return if first rectangle is greater in area than second rectangle

template <class T>

bool Rectangle<T>:: operator>(const Rectangle<T> &other) const

{

       return((fabs(area()-other.area()) >= EPSILON) && (area() > other.area()));

}

// function to return if first rectangle is less than or equal in area than second rectangle

template <class T>

bool Rectangle<T>::operator<=(const Rectangle<T> &other) const

{

       return((fabs(area()-other.area()) >= EPSILON) && (area() <= other.area()));

}

// function to return if first rectangle is greater than or equal in area than second rectangle

template <class T>

bool Rectangle<T>:: operator>=(const Rectangle<T> &other) const

{

       return((fabs(area()-other.area()) >= EPSILON) && (area() >= other.area()));

}

// function to return if first rectangle is equal in area to second rectangle

template <class T>

bool Rectangle<T>::operator==(const Rectangle<T> &other) const

{

       return(sameArea(other));

}

// function to return if first rectangle is not equal in area to second rectangle

template <class T>

bool Rectangle<T>::operator!=(const Rectangle<T> &other) const

{

       return(!sameArea(other));

}

// function to assign the length and width of second area to first

template <class T>

void Rectangle<T>::operator=(const Rectangle<T> &other)

{

       if(this != &other) // avoid self assignment

       {

             this->length = other.length;

             this->width = other.width;

       }

}

// function same as + operator and assign the result to the first Rectangle

template <class T>

void Rectangle<T>::operator+=(const Rectangle<T> &other)

{

       T req_area = area()+other.area();

       T ratio = area()/req_area;

       length = length*ratio;

       width = width*ratio;

}

// function same as - operator and assign the result to the first Rectangle

template <class T>

void Rectangle<T>::operator-=(const Rectangle<T> &other)

{

       T req_area = fabs(area()-other.area());

       T ratio = area()/req_area;

       length = length*ratio;

       width = width*ratio;

}

// function same as * operator and assign the result to the first Rectangle

template <class T>

void Rectangle<T>::operator*=(const Rectangle<T> &other)

{

       T req_area = area()*other.area();

       T ratio = area()/req_area;

       length = length*ratio;

       width = width*ratio;

}

// function same as / operator and assign the result to the first Rectangle

template <class T>

void Rectangle<T>::operator/=(const Rectangle<T> &other)

{

       T req_area = area()/other.area();

       T ratio = area()/req_area;

       length = length*ratio;

       width = width*ratio;

}

//end of rectangle class


Related Solutions

Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased
For the following exercises, use the written statements to construct a polynomial function that represents the required information.A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased by twice that amount, express the area of the rectangle as a function of x.  
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT