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

Help on an assigment! Hey guys I\'ve done a little bit of the work but I need so

ID: 3783935 • Letter: H

Question

Help on an assigment! Hey guys I've done a little bit of the work but I need some assistance on this c++ assignment

The project is a C++   program   that   utilizes   classes   and   inheritance  
to   model   geometric   shapes

I've figured out the main.cpp format and have Specification file of the triangle, rectangle and color completed. I also have the shape implementation file completed but not the specification file. apparently I am not allowed to change any #include stuff, any help is appreciated! Thank you so much ((so in short triangle.h, rectangle.h, color.h, shape.cpp, and main.cpp are COMPLETED// triangle.cpp, rectangle.cpp, color.cpp, and shape.h are NOT COMPLETED))

Here is the main.cpp file

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// main.cpp
//
// Driver program which is used to test each
// class member function.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <typeinfo>
#include "color.h"
#include "shape.h"
#include "triangle.h"
#include "rectangle.h"

using namespace std;                     // Global using declaration

// Prototypes for support functions -- definitions appear after main()
void Bar(int n);                         // Prototype for Bar function
void Print(Shape* someShape);           // Prototype for Print function
//void PrintColor(Color someColor);        // Prototype for PrintColor function

// Start of main() function

int main (int argc, char* const argv[]) // Command-line arguments (more on this later)
{
ifstream inputs;                         // Input file stream variable for test file
char op, ch;                           // Hold operation and optional char input from test file
Shape* ptr = NULL;                     // Pointer to abstract shape object
string operand1,operand2,operand3,operand4;   // Holds operand values input from test file
string comment;                        // Holds comment input from test file
Color someColor;                       // Holds color input from file
char t;                                // Holds s for Shape, t/T for Triangle, r/R for Rectangle
double a, b, c;                        // Holds lengths a, b, c input from file
int red, green, blue;                  // Holds red, green, blue input from file
Color* color;


// Output usage message if test input file name is not provided
if (argc != 2)
{
    cout << "Usage: project02 <inputfile> ";
   return 1;
}

// Attempt to open test input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
    cout << "Error - unable to open input file" << endl;
   return 1;
}

Bar(60);                                           // Output long bar

// Process comment line from input file
getline(inputs, comment);                          // Input file header comment
cout << endl << comment << endl << endl;           // Output file header comment

// Configure floating point decimal output
cout << fixed << showpoint << setprecision(2);
  
  
// Below is the primary loop that processes each operation appearing within the test file.
// Starts with an initial priming read of first operation

inputs >> op;                                         // Attempt to input first test operation from file
  
while (inputs)                                     // While Not-EOF
{
    switch (op)                                      // Process operation input from test file
    {
     case '#':   // Test file comment
            getline(inputs, comment);      // Input and echo the comment appearing in the test file
            cout << '#' << comment << endl;
            break;
  
     case '?':   // Print Operator
            cout << "TypeID -- ";
            Print(ptr);
            break;  

     case '~':   // Print Bar
            Bar(40);                         // Output short bar
            break;              

     case '+':   // Use symbol input from file to invoke appropriate constructor
                 inputs >> t;               // Input object type symbol

                 try
                 {
                   cout << "Constructor -- ";
                   switch (t)
                   {
                     case 's': // Shape default constructor
                                cout << "Shape() ";
                                ptr = new Shape();
                                break;

                     case 'S': // Shape parameterized constructor
                                inputs >> red >> green >> blue;
                                cout << "Shape(" << red << "," << green << "," << blue << ") ";
                                ptr = new Shape(red,green,blue);
                                break;

                     case 't': // Triangle default constructor
                                cout << "Triangle() ";
                                ptr = new Triangle;
                                break;

                     case 'T': // Triangle parameterized constructor
                                inputs >> a >> b >> c >> red >> green >> blue;
                                cout << "Triangle(" << a << ", " << b << ", "
                                     << c << ", " << red << ", " << green << ", " << blue << ") ";
                                ptr = new Triangle(a, b, c, red, green, blue);
                                break;

                     case 'r': // Rectangle default constructor
                                cout << "Rectangle() ";
                                ptr = new Rectangle;
                                break;

                     case 'R': // Rectangle parameterized constructor
                                inputs >> a >> b >> red >> green >> blue;
                                cout << "Rectangle(" << a << ", " << b << ", "
                                    << red << ", " << green << ", " << blue << ") ";
                                ptr = new Rectangle(a, b, red, green, blue);
                                break;

                     default:   cout << "Error: unrecognized object" << endl;
                    } // End switch (op)


                    cout << "Completed";
                  } // End try
                  catch ( ... )                        // Catch any exception thrown above
                  {
                    cout << "Failed";
                  }
          
                  cout << endl;                
                  break;

      case 'c':   // getColor()
                  cout << "getColor() -- ";
                  try
                  {
                    ptr->getColor().print();
                    cout << " Completed";
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;

      case 'a':   // area(...)
                  //inputs >> operand1;
                  cout << "area() -- ";
                  try
                  {
                    cout << ptr->area();
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;  

      case 'p':   // perimeter(...)
                  //inputs >> operand1;
                  cout << "perimeter() -- ";
                  try
                  {
                    cout << ptr->perimeter();
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl;
                  break;
                      
      case '-':   // Destructor
                  try
                  {
                    cout << "Destructor -- ";
                    delete ptr;                        // Deallocate currency
                    cout << "Completed";
                       ptr = NULL;                     // Make sure that ptr is not a dangling pointer
                  }
                  catch ( ... )
                  {
                    cout << "Failed";
                  }
                  cout << endl << endl;
                  break;      

      default:    // Error
                  cout << "Error - unrecognized operation '" << op << "'" << endl;
                  cout << "Terminating now..." << endl;
                  return 1;
                  break;
    }
  
    inputs >> op;                                     // Attempt to input next command
}

cout << endl;
Bar(60);                                           // Output long bar

return 0;
}


void Bar(int n)
// Bar() -- prints horizontal bar
{
   for(int k = 0; k < n; k++)
        cout << '#';
    cout << endl;
} // End Bar()


void Print(Shape* someShape)
// Writes shape object description to stdout
{
   string s = typeid(*someShape).name();
   cout << s << endl;         
}

/************** End of main.cpp ***************/


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Okay now here are the specification files (besides the shape.h while I need to complete)

///////////////////////////////

// color.h
//
// Color class models the color of an item <red,green,blue>
//
// Use the contents of this file to implement color.cpp


#include <iostream>
using namespace std;

#ifndef COLOR_H
#define COLOR_H

class Color
{
private:
    int     red;            // Amount of red 0-255
    int     green;          // Amount of green 0-255
    int     blue;           // Amount of blue 0-255

public:
    Color();                // Initializes red, green, and blue to zero
    void setRed(int rr);    // Sets red to rr
    void setGreen(int gg); // Sets green to gg
    void setBlue(int bb);   // Sets blue to bb

    int getRed() const;     // Returns red
    int getGreen() const;   // Returns green
    int getBlue() const;    // Returns blue

    void print() const      // Writes <red,green,blue> to std output
    {
   cout << "<" << getRed() << "," << getGreen() << "," << getBlue() << ">";
    }
};

#endif

////////////////////////////////////////////////////////////////

// rectangle.h
//
// Rectangle class models Rectangle with side lengths l, w
//
// Use the contents of this file to implement rectangle.cpp
//

#include "shape.h"

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle : public Shape
{
private:
    double l, w;                                  // Side lengths

public:
    Rectangle();                                  // Initializes l, w to 0.0 and red, green, blue to 0

    Rectangle(double ll, double ww,               // Initializes l, w to ll, ww and
              int rr, int gg, int bb);            // red, green, blue to rr, gg, bb respectively
                                               

    double area() const;                          // Returns area value computed from l, w

    double perimeter() const;                     // Returns perimeter value computed from l, w
};

#endif

//////////////////////////////////////////////////////

//
// triangle.h
//
// Triangle class models triangle with side lengths a, b, c
//
// Use the contents of this file to implement triangle.cpp

#include "shape.h"

#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle : public Shape
{
private:
    double a, b, c;            // Side lengths

public:
    Triangle();                                   // Initializes a, b, c to 0.0 and red, green, blue to 0

    Triangle(double aaa, double bbb, double ccc, // Initializes a, b, c to aaa, bbb, ccc and
             int rr, int gg, int bb);             // red, green, blue to rr, gg, bb respectively
                                               

    double area() const;                          // Returns area value computed from a, b, c

    double perimeter() const;                     // Returns perimeter value computed from a, b, c
};

#endif

//////////////////////////////////////////////////

And here is the shape implementation file that I have (in which I need the .h specification file for)

////////////////////////////////////////////////////

// shape.cpp
//
// Shape class models a generic geometric shape with color
// defined by a private Color object attribute c
//
// Use the contents of this file to reverse engineer the contents of shape.h
//

#include "shape.h"

Shape::Shape()
// Initializes c to <0,0,0>
{
    c = Color();              
}

Shape::Shape(int r, int g, int b)
// Initializes c to <r,g,b>
{
    c.setRed(r);
    c.setGreen(g);
    c.setBlue(b);
}

Color Shape::getColor() const
// Returns color of object
{
    return c;
}

double Shape::area() const
// Returns default area of 0.0 -- polymorphic function
{
    return 0.0;
}

double Shape::perimeter() const
// Returns default perimeter of -1.0 -- polymorphic function
{
    return -1.0;
}

////////////////////////////////////////////////////////

Thank you so much for anytime or help that you provide!

Explanation / Answer

Shape.java

/* nhgrif says:

* Phrancis ready for inheritance/polymorphism?

* Given the following abstract class:

*

* public abstract class Shape {

*      public abstract double area();

*      public abstract double perimeter();

* }

*

* Implement a Circle, Triangle, and Rectangle class which extend the class Shape.

* Ex: public class Circle extends Shape ... etc

*/

public abstract class Shape {

    public abstract double area();

    public abstract double perimeter();

}

Rectangle.java

public class Rectangle extends Shape {

    private final double width, length; //sides

    public Rectangle() {

        this(1,1);

    }

    public Rectangle(double width, double length) {

        this.width = width;

        this.length = length;

    }

    @Override

    public double area() {

        // A = w * l

        return width * length;

    }

    @Override

    public double perimeter() {

        // P = 2(w + l)

        return 2 * (width + length);

    }

}

Circle.java

public class Circle extends Shape {

    private final double radius;

    final double pi = Math.PI;

    public Circle() {

        this(1);

    }  

    public Circle(double radius) {

        this.radius = radius;

    }

    @Override

    public double area() {

        // A = r^2

        return pi * Math.pow(radius, 2);

    }

    public double perimeter() {

        // P = 2r

        return 2 * pi * radius;

    }

}

Triangle.java

public class Triangle extends Shape {

    private final double a, b, c; // sides

    public Triangle() {

        this(1,1,1);

    }

    public Triangle(double a, double b, double c) {

        this.a = a;

        this.b = b;

        this.c = c;

    }

    @Override

    public double area() {

        // Heron's formula:

        // A = SquareRoot(s * (s - a) * (s - b) * (s - c))

        // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle

        double s = (a + b + c) / 2;

        return Math.sqrt(s * (s - a) * (s - b) * (s - c));

    }

    @Override

    public double perimeter() {

        // P = a + b + c

        return a + b + c;

    }

}

And this is what I used to test it all, which it all works as intended:

TestShape.java

public class TestShape {

    public static void main(String[] args) {

        // Rectangle test

        double width = 5, length = 7;

        Shape rectangle = new Rectangle(width, length);

        System.out.println("Rectangle width: " + width + " and length: " + length

                + " Resulting area: " + rectangle.area()

                + " Resulting perimeter: " + rectangle.perimeter() + " ");

        // Circle test

        double radius = 5;

        Shape circle = new Circle(radius);

        System.out.println("Circle radius: " + radius

            + " Resulting Area: " + circle.area()

            + " Resulting Perimeter: " + circle.perimeter() + " ");

        // Triangle test

        double a = 5, b = 3, c = 4;

        Shape triangle = new Triangle(a,b,c);

        System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c

                + " Resulting Area: " + triangle.area()

                + " Resulting Perimeter: " + triangle.perimeter() + " ");

    }

}