Question

In: Computer Science

I have figured out this assignment, but I am having a hard time figuring out the...

I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks!

Creating a Rectangle class

We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following:

  1. The private data shall consist of 4 members:
    1. length
    2. width
    3. area
    4. perimeter
  2. Have 1 constructor with the following parameters: length and width.
  3. Have 2 mutators:
    1. setLength to set/modify the length.
    2. setWidth to set/modify the width.
  4. Have 5 accessors:
    1. getLength to return the length.
    2. getWidth to return the width.
    3. getPerimeter to return the perimeter.
    4. getArea to return the area.
    5. printObjet to print the representation of the rectangle (using the same output criteria of the previous incarnations). NOTE: This routine must print out the following: "You created a rectangle with the following characteristics:" followed by a blank line before printing out the values of the class data.

You then need to create a 'main' program and instantiate 3 rectangle objects with different parameters. You also need to print each rectangle's characteristics.

For this assignment you need to turn in 1 file which contains the class header and declaration/implementation, and the main program. Don't forget to include your file header.

Expected Output:

Your output shall look like this given the inputs below:

Enter the length of the 1st rectangle: 3
Enter the width of the 1st rectangle: 5
You created a rectangle with the following characteristics:

    width = 5
    length = 3
    perimeter = 16
    area = 15

Enter the length of the 2nd rectangle: 4
Enter the width of the 2nd rectangle: 6
You created a rectangle with the following characteristics:

    width = 6
    length = 4
    perimeter = 20
    area = 24

Enter the length of the 3rd rectangle: 4.1
Enter the width of the 3rd rectangle: 6.2
You created a rectangle with the following characteristics:

    width = 6.2000
    length = 4.1000
    perimeter = 20.6000
    area = 25.4200

EXTRA CREDIT

For 7 points of extra credit:

Store 10 rectangle objects you created in a vector and sequence through the vector to print out the 10 rectangle objects.

Solutions

Expert Solution

Screenshot

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

Program

rectangle.h

//Create a class rectangle
class Rectangle {
   //Instance variables
private:
   double length;
   double width;
   double perimeter;
   double area;
   //Member functions
public:
   //Constructor
   Rectangle(double len, double wid);
   //Mutators
   void setLength(double len);
   void setWidth(double wid);
   //Accessors
   double getLength();
   double getWidth();
   double getArea();
   double getPerimeter();
   //Print object details
   void print();
};

rectangle.cpp

#include "rectangle.h"
#include<iostream>
#include<iomanip>
using namespace std;
   //Constructor
   Rectangle::Rectangle(double len, double wid) {
       length = len;
       width = wid;
   }
   //Mutators
   void Rectangle::setLength(double len) {
       length = len;
   }
   void Rectangle::setWidth(double wid) {
       width = wid;
   }
   //Accessors
   double Rectangle::getLength() {
       return length;
   }
   double Rectangle::getWidth() {
       return width;
   }
   double Rectangle::getArea() {
       area = (length*width);
       return area;
   }
   double Rectangle::getPerimeter() {
       perimeter = (2 * (length + width));
       return perimeter;
   }
   //Print object details
   void Rectangle::print() {
       cout <<setprecision(4);
       cout << "\n\nwidth=" << width << "\nlength=" << length << "\nperimeter=" << getPerimeter();
       cout << "\narea=" << getArea() << endl << endl;
   }

main.cpp

#include <iostream>
#include<vector>
#include"rectangle.h"
using namespace std;

int main()
{
   //Variables for input read
   double l, w;
    /*//Create 3 objects of rectangle
   for (int i = 0; i < 3; i++) {
       cout << "Enter the length of the " << (i + 1) << "th rectangle : ";
       cin >> l;
       cout << "Enter the width of the "<<(i+1)<<"th rectangle : ";
       cin >> w;
       cout << "You created a rectangle with the following characteristics : ";
       Rectangle rect(l, w);
       rect.print();
   }*/
   //Create vector to store object
   vector<Rectangle> rectangles;
   for (int i = 0; i < 10; i++) {
       cout << "Enter the length of the " << (i + 1) << "th rectangle : ";
       cin >> l;
       cout << "Enter the width of the "<<(i+1)<<"th rectangle : ";
       cin >> w;
       Rectangle rect(l, w);
       rectangles.push_back(rect);
   }
   //Print vector
   for (int i = 0; i < 10; i++) {
       cout << "You created a rectangle with the following characteristics : ";
       rectangles.at(i).print();
   }
}

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

Output

Enter the length of the 1th rectangle : 1
Enter the width of the 1th rectangle : 2
Enter the length of the 2th rectangle : 2
Enter the width of the 2th rectangle : 3
Enter the length of the 3th rectangle : 3
Enter the width of the 3th rectangle : 4
Enter the length of the 4th rectangle : 4
Enter the width of the 4th rectangle : 5
Enter the length of the 5th rectangle : 5
Enter the width of the 5th rectangle : 6
Enter the length of the 6th rectangle : 6
Enter the width of the 6th rectangle : 7
Enter the length of the 7th rectangle : 7
Enter the width of the 7th rectangle : 8
Enter the length of the 8th rectangle : 8
Enter the width of the 8th rectangle : 9
Enter the length of the 9th rectangle : 9
Enter the width of the 9th rectangle : 10
Enter the length of the 10th rectangle : 10
Enter the width of the 10th rectangle : 11
You created a rectangle with the following characteristics :

width=2
length=1
perimeter=6
area=2

You created a rectangle with the following characteristics :

width=3
length=2
perimeter=10
area=6

You created a rectangle with the following characteristics :

width=4
length=3
perimeter=14
area=12

You created a rectangle with the following characteristics :

width=5
length=4
perimeter=18
area=20

You created a rectangle with the following characteristics :

width=6
length=5
perimeter=22
area=30

You created a rectangle with the following characteristics :

width=7
length=6
perimeter=26
area=42

You created a rectangle with the following characteristics :

width=8
length=7
perimeter=30
area=56

You created a rectangle with the following characteristics :

width=9
length=8
perimeter=34
area=72

You created a rectangle with the following characteristics :

width=10
length=9
perimeter=38
area=90

You created a rectangle with the following characteristics :

width=11
length=10
perimeter=42
area=110


Related Solutions

I am having difficulties with figuring out the formulas for this this question. Is there a...
I am having difficulties with figuring out the formulas for this this question. Is there a walk-through for this? You are the manager of a monopoly that sells a product to two groups of consumers in different parts of the country. Group 1’s elasticity of demand is -2, while group 2’s is -3. Your marginal cost of producing the product is $30. a. You are a monopolist. b. You compete against one other firm in a Cournot oligopoly. c. You...
I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
I have had such a hard time figuring out this problem please show with detailed response!...
I have had such a hard time figuring out this problem please show with detailed response! thank you Consider the titration of a 23.0?mL sample of 0.175M CH3NH2 with 0.155M HBr. Determine each of the following. 1.the initial pH 2.the volume of added acid required to reach the equivalence point 3.the pH at 6.0mL of added acid 4.the pH at one-half of the equivalence point 5.the pH at the equivalence point 6.the pH after adding 6.0mL of acid beyond the...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing 55x + 45y, however the variables are throwing me off. I only need help on question #1 as it would be a great help to understanding the rest of what the questions are asking. The problem is as follows: NorCal Outfitters manufactures a variety of specialty gear for outdoor enthusiasts. NorCal has decided to begin production on two new models of crampons: the Denali...
These are the correct answers but I am having difficulty figuring out how these figures were...
These are the correct answers but I am having difficulty figuring out how these figures were reached. On March ​1, 2018​, Nailtique Nail Salons issued $450,000 of 15​-year, 5 percent bonds payable. The bonds were sold for $432,000. The bonds pay interest each August 31 and February 28​, and any discount or premium is amortized using​ straight-line amortization. Requirement 1. Fill in the blanks to complete each statement. a. Nailtique Nail Salons' bonds are priced at (express the price as...
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating...
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating APIC and Retained earnings for Stock Dividends. Also, I need assistance with the fourth problem, how to calculate the total value of shareholders' equity. Could you please help show me how to solve? Accounting for Share Transactions The shareholders' equity section of the consolidated balance sheet of Wilson Industries appeared as follows at the beginning of the year: Shareholders' Equity Class A common stock,...
Can you balance these redox equations? I am having trouble figuring out the process of adding...
Can you balance these redox equations? I am having trouble figuring out the process of adding extra products to make them work. ClO3- + Cl- -----> Cl2 + ClO2- Cr2O72- + C2O42- -----> Cr3+ + CO2
I understand the answer to this I am just having a hard time creating a graph...
I understand the answer to this I am just having a hard time creating a graph for it. Bill the butcher is upset because the government plans to tax beef $.10 a pound. "I hate paying taxes," he says. "Because of this, I'm raising all my beef prices by $.10 a pound. The consumers will bear this burden, not me." Do you see anything wrong with this way of thinking? Explain. Draw a graph describing your answer and attach it...
I am currently working on creating a dice game. I have not figured out how to...
I am currently working on creating a dice game. I have not figured out how to make it work? What should I do to make it work? Here is what I have so far: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Dice Game</title> <link rel="stylesheet" type="text/css" href="dice.css"> </head> <body> <div class="row" align="center"> <div class="col-4"> <h3>Your Dice</h3> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice1"/> <img src="dice images/m1.png" width="100" height="100" alt="roll: 1" id="mydice2"/> </div> <div class="col-4"> <h3>Opponent's Dice</h3> <img src="dice images/o1.png" width="100"...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me...
I am having trouble figuring out how to To-Do list using java. Can anyone guide me through the steps for achieving this?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT