Question

Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables, a constructor with two param

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

Class Rectangle and a main function to test the code given below. Explanation given as comments within code. UML class diagram given below code. Hope this helps! (C++ language used)

#include <iostream>
#include <string>
#include <array>
using namespace std;

// this  function returns gcd (greatest common denominator) of 2 numbers. Used in Rectangle::getRatio method.
int getGcd(int x, int y) {
    // since all numbers divide 0
    if (x == 0 && y == 0)
        return 0;
    if (x == 0)
        return y;
    if (y == 0)
        return x;
    
    // if same
    if (x == y)
        return x;
    
    // if first number is bigger
    if (x > y)
        return getGcd(x - y, y);
    return getGcd(x, y - x);
}

// Rectangle class
class Rectangle {
    private:
        int length;
        int width;
        
    public:
        // parameterized constructor
        Rectangle(int length, int width) {
            this->length = length;
            this->width = width;
        }
        
        //getters and setters
        int getLength() {
            return this->length;
        }
        int getWidth() {
            return this->width;
        }
        void setLength(int length) {
            this->length = length;
        }
        void setWidth(int width) {
            this->width = width;
        }
        
        // for question a
        // method to calculate and return ratio between length and width in an int array of size 2, with numerator in [0] and denominator in [1].
        array<int,2> getRatio() {
            array<int,2> ratio;
            int gcd = getGcd(this->length, this->width);
            // to get ratio, divide length and width by gcd.
            ratio[0] = this->length / gcd; // stores numerator of ratio
            ratio[1] = this->width / gcd; // stores denominator of ratio
            // returns an int array with length in [0] and width in [1].
            return ratio;
        }
        
        // for question b
        // method that returns true if object is a square, and false if its not, making use of Rectangle::getRatio method.
        bool isSquare() {
            array<int,2> ratio = this->getRatio();
            if (ratio[0] == ratio[1]) { // this happens when 1:1 is the ratio, implying it is indeed a square.
                return true;
            } else {
                return false;
            }
        }
};

int main()
{
    // test 1 - expected output: "Ratio is 2:1" and "It is not a square"
    Rectangle r1(10, 5);
    array<int,2> ratio1 = r1.getRatio();
    cout<<"\nLength: "<<r1.getLength()<<"\tWidth: "<<r1.getWidth()<<"\n";
    cout<<"Ratio is: "<<ratio1[0]<<":"<<ratio1[1];
    if (r1.isSquare()) {
        cout<<"\nIt is a square.\n";
    } else {
        cout<<"\nIt is not a square.\n";
    }
    
    // test 2 - expected output: "Ratio is 1:1" and "It is a square"
    Rectangle r2(4,4);
    array<int,2> ratio2 = r2.getRatio();
    cout<<"\nLength: "<<r2.getLength()<<"\tWidth: "<<r2.getWidth()<<"\n";
    cout<<"Ratio is: "<<ratio2[0]<<":"<<ratio2[1];
    if (r2.isSquare()) {
        cout<<"\nIt is a square.\n";
    } else {
        cout<<"\nIt is not a square.\n";
    }
    return 0;
}

Output of the above code:
Length: 10 Width: 5 Ratio is: 2:1 It is not a square. Width: 4 Length: 4 Ratio is: 1:1 It is a square.

UML class diagram below.

Rectangle length: int width int i + + getLength C) ; int getWidth()int setLength (length: int) : void + set width (evidth: in

Please refer to the screenshot of the code given below to understand the indentation of the code.
8 9 1 #include <iostream> 2 #include <string> 3 #include <array> 4 5 using namespace std; 6 7 // this function returns gcd (g
39 40 41 42 43 44 45 46 47 48 //getters and setters int getLength() { return this->length; } int getWidth() { return this->wi
} } }; 74 75 76 77 78 79 80 81 82 83 84 85 86 87- 88 89 90 91 92 93 94 95 int main() { // test 1 - expected output: Ratio is

Add a comment
Know the answer?
Add Answer to:
Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables,...
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
  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

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

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

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

  • Write a class definition for a Rectangle. The data fields should be: • An integer for...

    Write a class definition for a Rectangle. The data fields should be: • An integer for the value of the width of the Rectangle • An integer for the value of the height of the Rectangle An integer for the value of the area of the Rectangle It must have: . A default constructor A constructor that has a parameters for width and height and assigns them to the member variables. • The class should have mutators for all of...

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

  • Java Programming assignment. 1. Create a class called Square that takes a width parameter in the...

    Java Programming assignment. 1. Create a class called Square that takes a width parameter in the constructor. The Square class should have a draw() method that will draw the square on the screen. Create a class called TestSquare that will take width from the user, create an object of Square, and invoke the draw() method on the square object. Below is a UML diagram for the Square and Rectangle class: 3. Create a zip file that contains your Java programs....

  • Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...

    Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

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

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