Question

In: Computer Science

Below is some code for a rectangle class that needs to be completed. Add member function...

Below is some code for a rectangle class that needs to be completed. Add member function declarations to the class declaration and member function definitions below the declaration. For the accessor functions, you can add the definitions directly into the class declaration. The goal is to code the class so that it works wthout changing the main program

#include <iostream>
using namespace std;

// rectangle has a vertical height and horizontal width
// The class below is a rectangle. It has two private
// data members: height and width.
// TODO: Complete the class declaration and definition.
class rectangle {
public:
// TODO: declare a default constructor
// Make the height and width = 1.
  
// TODO: declare member function void add
// @param int height, int width

// TODO: delcare member function void set
// @param int height, int width
  
// TODO: declare member function void draw
  
// TODO: define accessor for width
  
// TODO: define accessor for height
  
private:
int width;
int height;
};

// TODO: define the default constructor that
// sets height and width to 1.
rectangle::rectangle()
{

}

// TODO: Implement add to increment the length
void rectangle::add(int h, int w)
{

}

// TODO: Implement set to overwrite the data members
void rectangle::set(int h, int w)
{

}

// TODO: Implement draw to draw a rectangle using '*' characters
void rectangle::draw()
{
  
  
}

// TODO: Don't forget to define getWidth and getHeight


int main()
{
// Declare 2 rectangles
rectangle r1, r2;
  
// Print the unit rectangle
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
  
// Set, print dimensions and draw
r1.set(4, 3);
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
r1.draw();
  
// Assign, increment, print dimensions and draw
r2 = r1;
r2.add(3, 4);
cout << "r2 is " << r2.getHeight() << " x " << r2.getWidth() << endl;
r2.draw();
  
return 0;
}

Solutions

Expert Solution

#include <iostream>
using namespace std;

// rectangle has a vertical height and horizontal width
// The class below is a rectangle. It has two private
// data members: height and width.
// TODO: Complete the class declaration and definition.
class rectangle {
public:
// TODO: declare a default constructor
// Make the height and width = 1.
rectangle();
rectangle(int,int);
// TODO: declare member function void add
// @param int height, int width
void add(int,int);

// TODO: delcare member function void set
// @param int height, int width
void set(int,int);
// TODO: declare member function void draw
void draw();
// TODO: define accessor for width
int getWidth(){
return width;
}
// TODO: define accessor for height
int getHeight(){
return height;
}
private:
int width;
int height;
};

// TODO: define the default constructor that
// sets height and width to 1.
rectangle::rectangle()
{
   width=1;
   height=1;
}

// TODO: Implement add to increment the length
void rectangle::add(int h, int w)
{  
   height+=h;
   width+=w;
}

// TODO: Implement set to overwrite the data members
void rectangle::set(int h, int w)
{
   height=h;
   width=w;
}

// TODO: Implement draw to draw a rectangle using '*' characters
void rectangle::draw()
{
for(int i=0;i<height;i++){
   for(int j=0;j<width;j++)
       cout<<"*";
   cout<<endl;
}
  
}

// TODO: Don't forget to define getWidth and getHeight


int main()
{
// Declare 2 rectangles
rectangle r1, r2;
  
// Print the unit rectangle
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
  
// Set, print dimensions and draw
r1.set(4, 3);
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
r1.draw();
  
// Assign, increment, print dimensions and draw
r2 = r1;
r2.add(3, 4);
cout << "r2 is " << r2.getHeight() << " x " << r2.getWidth() << endl;
r2.draw();
  
return 0;
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

This is the code that needs to be completed... Thank you! public class MultiplicationTable { private...
This is the code that needs to be completed... Thank you! public class MultiplicationTable { private int[][] table; private int rows; private int cols;    /* Instantiate the two dimensional array named table. * Assign the numRows parameter to the rows field. * Assign the numCols parameter in the cols field. */ public MultiplicationTable(int numRows, int numCols) {    } /* Using nested for loops, fill the table array with * the values shown in the Multiplication Table document. */...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength *...
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength * * If the argument passed to the setLength function is zero or * * greater, it is copied into the member variable length, and true * * is returned. If the argument is negative, the value of length * * remains unchanged and false is returned. * *******************************************************************/ bool Rectangle::setLength(double len) { bool validData = true; if (len >= 0) // If the len...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
Below is the code for the isSmaller() member function: bool Location::isSmaller(const Location & r) const {...
Below is the code for the isSmaller() member function: bool Location::isSmaller(const Location & r) const { // ORDER OF COMPARISONS ==> 1st country; 2nd state; 3rd city int a = strcmp(country,r.country); // need strcmp // country and r.country are arrays and in C++ the expression country < r.country will compare address, not data // the data must be compared cell by corresponding cell in the 2 arrays, which takes place in the strcmp() function // There are three possible return...
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
This needs to be in Python and each code needs to be separated. Design a class...
This needs to be in Python and each code needs to be separated. Design a class for a fast food restaurant meals that should have 3 attributes; Meal Type (e.g., Burger, Salad, Taco, Pizza); Meal size (e.g., small, medium, large); Meal Drink (e.g., Coke, Dr. Pepper, Fanta); and 2 methods to set and get the attributes. Write a program that ask the user to input the meal type, meal size, and meal drinks. This data should be stored as the...
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT