Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this simple coding activity. I will award full points to the fi

ID: 647253 • Letter: I

Question

I need help with this simple coding activity. I will award full points to the first person whos code works.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Shape
{
public:
   Shape(double w, double h);
   string toString();

private:
   double width;
   double height;

};

Shape::Shape(double w, double h)
{
   width = w;
   height = h;
}

string Shape::toString()
{
   stringstream ss;
   ss << "Width: " << width << endl;
   ss << "Height: " << height << endl;

   return ss.str();
}

class Rectangle : public Shape
{

public:
   Rectangle(double w, double h, int s);
   string toString();

private:
   int sides;

};


string Rectangle::toString()
{
   //
   // Implement the Rectangle toString function
   // using the Shape toString function
}

// Use the constructor you created
// for the previous problem here

//

int main()
{
   double width;
   double height;
   const int sides = 4;

   cin >> width;
   cin >> height;

   Rectangle bob = Rectangle(width, height, sides);
   cout << bob.toString();

   return 0;
}

In this problem, you?ll notice that the data members of the Shape class are now private instead of protected. This means that the child class no longer has direct access to them. We have added a toString method to the Shape class that can print these private variables, but it doesn?t know anything about the child class sides variable. Use the parent Shape class? toString Function to implement the Rectangle toString function. It should print out the same information, and in the same format, as the previous problem.

Explanation / Answer

class Polymorph {
   public static void main(String ar[]) {

   
      Shape scrib[] = new Shape[2];
      scrib[0] = new Rec(10, 20, 5, 6);
      scrib[1] = new Circ(15, 25, 8);

   
      for (int i = 0; i < scrib.length; i++) {
         scrib[i].draw();
         scrib[i].MoveTo(100, 100);
         scrib[i].draw();
      }

   
      Rec arect = new Rec(0, 0, 15, 15);
      arect.setwid(30);
      arect.draw();
   }
}

Shape Class (Shape.java)

abstract class Shape {
   private int ex;
   private int y;


   Shape(int ex1, int y1) {
      moveTo(ex1, y1);
   }


   int getre() { return ex; }
   int getde() { return y; }
   void setex(int ex1) { ex = ex1; }
   void setY(int y1) { y = y1; }


   void moveTo(int ex1, int y1) {
      setex(ex1);
      setY(y1);
   }
   void MoveTo(int deltaex, int deltay) {
      moveTo(getre() + deltaex, getde() + deltay);
   }


   abstract void draw();
}

rec Class (rec.java)

class rec eextends Shape {
   private int wid;
   private int hie;


   rec(int ex1, int y1, int wid1, int hei1) {
      super(ex1, y1);
      setwid(wid1);
      sethie(hei1);
   }

   // accessors for the wid & hie
   int getwid() { return wid; }
   int gethie() { return hie; }
   void setwid(int wid1) { wid = wid1; }
   void sethie(int hei1) { hie = hei1; }

   // draw the rec
   void draw() {
      System.out.println("Drawing a rec at:(" + getre() + ", " + getde() +
         "), wid " + getwid() + ", hie " + gethie());
   }
}

circ Class (circ.java)

class circ eextends Shape {
   private int radius;

   // constructor
   circ(int ex1, int y1, int newradius) {
      super(ex1, y1);
      setRadius(newradius);
   }

   // accessors for the radius
   int getRadius() { return radius; }
   void setRadius(int newradius) { radius = newradius; }

   // draw the circ
   void draw() {
      System.out.println("Drawing a circ at:(" + getre() + ", " + getde() +
         "), radius " + getRadius());
   }
}