Question

The program must be in C# syntax. Write the definition for a generic class called Rectangle...

  1. The program must be in C# syntax.
  2. 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

  1. Write the definitions for each of the above member functions.
  2. Write main function to create two rectangle pointers. Set the length and width of the first rectangle to 5 and 2. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle and its area and perimeter.

Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result

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

------------------ I Used Visual Studio 2013, C# Language, Console Application ------------------

------------------ OUTPUT ------------------

file:///C:/Users/krisfo/documents/visual studio 2013/Projects/CSharp_Consol Rectangle 1 :: Length : 5.00 Width : 2.00 Area :

file:///C:/users/krisfo/docum Multiplication :: Length : 48.61 Width : 183.73 Area : 8930.25 Perimeter : 464.67 Division :: L

------------------ CODE ------------------

------------------ Program.cs(Main Method Class) ------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_Console_RectangleProject
{
class Program
{
static void Comparison(Rectangle r1, Rectangle r2)
{
Console.WriteLine("\nComparison Operator :: ");
if (r1 == r2)
Console.WriteLine("Rectangle 1 area is equal to Rectangle 2 area");
else if (r1 < r2)
Console.WriteLine("Rectangle 1 area is less than Rectangle 2 area");
else if (r1 > r2)
Console.WriteLine("Rectangle 1 area is greater than Rectangle 2 area");
else if (r1 <= r2)
Console.WriteLine("Rectangle 1 area is less than or equal to Rectangle 2 area");
else if (r1 >= r2)
Console.WriteLine("Rectangle 1 area is greater than or equal to Rectangle 2 area");
else if (r1 != r2)
Console.WriteLine("Rectangle 1 area is not equal to Rectangle 2 area");
}
static void Main(string[] args)
{
Rectangle r1 = new Rectangle();
r1.setLength(5);
r1.setWidth(2);
Rectangle r2 = new Rectangle();
r2.setLength(5);
r2.setWidth(18.9);
Console.WriteLine("Rectangle 1 :: ");
r1.show();


Console.WriteLine("\nRectangle 2 :: ");
r2.show();
  
Program.Comparison(r1, r2);

Console.WriteLine("\nRectangle 1 Modified :: ");
r1.setLength(15);
r1.setWidth(6.3);
r1.show();
  
Program.Comparison(r1, r2);

Console.WriteLine("\nChange Rectangle 2 Sign :: ");
r1 = -r2;
r1.show();

Console.WriteLine("\nAddition :: ");
r1 += r2;
r1.show();

Console.WriteLine("\nSubtraction :: ");
r1 = r2 - r1;
r1.show();

Console.WriteLine("\nMultiplication :: ");
r1 *= r2;
r1.show();

Console.WriteLine("\nDivision :: ");
r1 /= r2;
r1.show();

Console.ReadLine();
}
}
}

------------------ Rectangle.cs ------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_Console_RectangleProject
{
class Rectangle
{
double length, width;
public Rectangle()
{
length = 0;
width = 0;
}
public void setLength(double l)
{
length = l;
}

public void setWidth(double w)
{
width = w;
}

public double perimeter()
{
return 2 * (length + width);
}

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

public void show()
{
Console.WriteLine("Length : " + length.ToString("0.00"));
Console.WriteLine("Width : " + width.ToString("0.00"));
Console.WriteLine("Area : " + area().ToString("0.00"));
Console.WriteLine("Perimeter : " + perimeter().ToString("0.00"));
}

public int sameArea(Rectangle r2)
{
if (area()==r2.area())
{
return 1;
}
return 0;
}

public static Rectangle operator -(Rectangle r)
{
r.length = -r.length;
r.width = -r.width;
return r;
}

public static Rectangle operator +(Rectangle r1, Rectangle r2)
{
Rectangle r3 = new Rectangle();
double newArea = r1.area() + r2.area();
double ratio = r1.length / r1.width;
//To find the width & length of new Rectangle r3
r3.width = Math.Sqrt(newArea / ratio);
r3.length = ratio * r3.width;
return r3;
}

public static Rectangle operator -(Rectangle r1, Rectangle r2)
{
Rectangle r3 = new Rectangle();
double newArea = r2.area() - r1.area();
double ratio = r1.length / r1.width;

r3.width = Math.Sqrt(newArea / ratio);
r3.length = ratio * r3.width;
return r3;
}

public static Rectangle operator *(Rectangle r1, Rectangle r2)
{
Rectangle r3 = new Rectangle();
double newArea = r1.area() * r2.area();
double ratio = r1.length / r1.width;

r3.width = Math.Sqrt(newArea / ratio);
r3.length = ratio * r3.width;
return r3;
}

public static Rectangle operator /(Rectangle r1, Rectangle r2)
{
Rectangle r3 = new Rectangle();
double newArea = r1.area() / r2.area();
double ratio = r1.length / r1.width;

r3.width = Math.Sqrt(newArea / ratio);
r3.length = ratio * r3.width;
return r3;
}

public static bool operator <(Rectangle r1, Rectangle r2)
{
if (r1.area()<r2.area())
{
return true;
}
return false;
}

public static bool operator >(Rectangle r1, Rectangle r2)
{
if (r1.area() > r2.area())
{
return true;
}
return false;
}

public static bool operator <=(Rectangle r1, Rectangle r2)
{
if (r1.area() <= r2.area())
{
return true;
}
return false;
}

public static bool operator >=(Rectangle r1, Rectangle r2)
{
if (r1.area() >= r2.area())
{
return true;
}
return false;
}

public static bool operator ==(Rectangle r1, Rectangle r2)
{
if (r1.area() == r2.area())
{
return true;
}
return false;
}

public static bool operator !=(Rectangle r1, Rectangle r2)
{
if (r1.area() != r2.area())
{
return true;
}
return false;
}

}
}

-------------------- -------------------- -------------------- -------------------- -------------------- --------------------
-------------------- -------------------- -------------------- -------------------- -------------------- --------------------

Add a comment
Know the answer?
Add Answer to:
The program must be in C# syntax. Write the definition for a generic class called Rectangle...
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
  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. 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...

  • C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *,...

    C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3. Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • U8ing separate files, write th definition for a clas called Poit and a class called Cirele...

    U8ing separate files, write th definition for a clas called Poit and a class called Cirele 1. The class point has the data members (double) and y (double) for the x and y coordinates The class has the following ember functions a) void set x (double) to set the x data member b) void set y(double) to set the y data member c) double get-x() to return th the data member d) double get yO to zeturn the the y...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

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