Question: Requested files: GeometricArea.cpp, SpecificationException.h, SpecificationException_test.cpp Please read the whole thing and write it in C++. I asked this question and someone just gave me back

Requested files: GeometricArea.cpp, SpecificationException.h, SpecificationException_test.cpp

Please read the whole thing and write it in C++. I asked this question and someone just gave me back my code that I put below.

Maximum number of files: 4

Extend the GeometricArea.cpp program you wrote to use an exception to handle user input where the parameters entered do not meet specifications, or that simply don't make sense. In the case of GeometricArea this occurs when the number of sides is two (2).

You should create a class SpecificationException that is thrown whenever your program encounters a specification that does not make sense. For example, if your program is asked to compute the area of a 2 sided polygon, You should throw a SpecificationException, because the fewest number of sides a polygon can have is 3..

You can create this class using only an interface (or header) file called SpecificationException.h. Simply include this header file in your GeometricArea.cpp source code and you can use that exception object..

The test driver for this program is named SpecificationException_test.cpp because it is supposed to call GeometricArea functions with values that trigger an exception.

Below is my code for GeometricArea.cpp

GeometricArea.h

#ifndef GEOMETRICAREA_H #define GEOMETRICAREA_H #include

class Circle { private: double side; public: //Exception for negative class NegativeSize { }; //Default constructor Circle() {side = 0.0;} //Mutator functions void setSide (double); //Accessor functions double getSide() const {return side;} double getArea() const {return (3.14)*(pow(side, 2.0));} }; class Triangle { private: float side1; float side2; float side3; public: //Exception for negative class NegativeSide1 { }; class NegativeSide2 { }; class NegativeSide3 { }; //Default Constructor Triangle() {side1 = 0.0; side2 = 0.0; side3 = 0.0;} //Mutator functions void setSide1(float); void setSide2(float); void setSide3(float); //Accessor functions float setSide1() const {return side1;} float setSide2() const {return side2;} float setSide3() const {return side3;} float getTarea() const {return (sqrt(((side1+side2+side3)/2)*(((side1+side2+side3)/2)-side1)*(((side1+side2+side3)/2)-side2)*(((side1+side2+side3)/2)-side3)));} }; class Polygon { private: double width; double length; public: //Exception for negative class NegativeWidth { }; class NegativeLength { }; //Default Constructor Polygon() {width = 0.0; length = 0.0;} //Mutator functions void setWidth(double); void setLength(double); //Accessor functions double getWidth() const {return width;} double getLength() const {return length;} double getParea() const {return length * width;} }; #endif

GeometricArea.cpp

#include "GeometricArea.h"

//Exception for Circle void Circle :: setSide (double s) { if (s >= 0) side = s; else throw NegativeSize(); } //Exception for Triangle void Triangle :: setSide1 (float o) { if (o >= 0) side1 = o; else throw NegativeSide1(); }

void Triangle :: setSide2 (float t) { if (t >= 0) side2 = t; else throw NegativeSide2(); } void Triangle :: setSide3 (float th) { if (th >= 0) side3 = th; else throw NegativeSide3(); } //Exception for Polygon void Polygon :: setWidth (double w) { if (w >= 0) width = w; else throw NegativeWidth(); }

void Polygon :: setLength (double len) { if (len >= 0) length = len; else throw NegativeLength(); }

GeometricArea_test.cpp

#include #include "GeometricArea.h" using namespace std;

int main() { //assigning variables double side, sides, side1, side2, side3, width, length; char again, c; Circle myCircle; Triangle myTriangle; Polygon myPolygon; //to loop the whole thing do{ cout << "Enter number of sides: "; cin >> sides; //Circle if (sides == 1 || sides == 0) { cout << "Enter diameter: "; cin >> side; } //Triangle else if (sides == 3) { cout << "Enter side 1: "; cin >> side1; cout << "Enter side 2: "; cin >> side2; cout << "Enter side 3: "; cin >> side3; } //Polygon else if (sides == 4) { cout << "Enter width: "; cin >> width; cout << "Enter length: "; cin >> length; } //Getting area of Circle if (sides == 1 || sides == 0) { try { myCircle.setSide(side); cout << "The area of the circle is: " << myCircle.getArea() << endl; cout << "Go again? (y/n): "; cin >> again; } //For negative invalid values catch (Circle :: NegativeSize) { cout << "ERROR: A negative value was given for the circle's side"; } } //Getting area of Triangle if (sides == 3) { try { myTriangle.setSide1(side1); myTriangle.setSide2(side2); myTriangle.setSide3(side3); cout << "The area of the triangle is: " << myTriangle.getTarea() << endl; cout << "Go again? (y/n): "; cin >> again; } //For invalid negative values catch (Triangle :: NegativeSide1) { cout << "ERROR: A negative value was given for the triangle's side 1"; } catch (Triangle :: NegativeSide2) { cout << "ERROR: A negative value was given for the triangle's side 2"; } catch (Triangle :: NegativeSide3) { cout << "ERROR: A negative value was given for the triangle's side 3"; } } //Getting area of Polygon if (sides == 4) { try { myPolygon.setWidth(width); myPolygon.setLength(length); cout << "The area of the polygon is: " << myPolygon.getParea() << endl; cout << "Go again? (y/n): "; cin >> again; } //For invalid negative values catch (Polygon :: NegativeWidth) { cout << "ERROR: A negative value was given for the polygon's width"; } catch (Polygon :: NegativeLength) { cout << "ERROR: A negative value was given for the polygon's length"; } } } while (again == 'y'); //to end program if (again == 'n') cout << "End of Program" << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!