In: Computer Science
]write two main programs: one will add exception handling to a geometric object, and the other will create a Template function to find the max of three “entities” (either ints, floats, doubles, chars, and strings)
A trapezoid is a quadrilateral in which two opposite sides are parallel. (note: all squares, rectangles, and parallelograms are trapezoids)
Note 1: the length of Base 1 is always greater than or equal to Base 2
Note 2: To form a valid trapezoid, if must satisfy the side/length rule, namely, the sum of the lengths of Leg1,Leg2 , and Base 2 must be greater than the length of Base 1
Note 3:If a default trapezoid is created, set all the sides equal to 1 (i.e., a square, or rhombus)
The following is the UMs for a trapezoid:
Trapezoid |
-base1 : double -base2: double -leg1: double -leg2: double |
+Trapezoid() +Trapezoid (base1, base2, leg1, leg2) // setters and getters +print(void): void +toString(void) : string |
For the driver program:
Note 1: If the user creates a trapezoid that violates the side/length rule (including 0 or negative side lengths), throw an exception
Note 2: If the user sets one of the sides that violates the side/length rule, do not change the side, and throw an exception
A main drvier (main8a.cpp) :
#include <iostream> #include "trapezoid.h" using namespace std; int main() { cout << "Welcome to Lecture 16: Exceptions and Templates" << endl; cout << "The Trapezoid Program!\n"; cout << "\nTry creating t1 (no parameters, which is valid):\n"; try { Trapezoid t1; // this is valid, by design t1.print(); } catch(string errorMsg) { cout << errorMsg << endl; } cout << "\nTry creating t2(15,3,3,3) (which is invalid):\n"; try { Trapezoid t2(15,3,3,3); // this is invalid t2.print(); } catch(string errorMsg) { cout << errorMsg << endl; } cout << "\nTry creating t3(5,3,4,4) (which is valid):\n"; try { Trapezoid t3(5,3,4,4); // this is valid t3.print(); } catch(string errorMsg) { cout << errorMsg << endl; } cout << "\nTry changing base1 of default t4 to 10 (which is invalid):\n"; try { Trapezoid t4; t4.setBase1(10); // this is invalid t4.print(); } catch(string errorMsg) { cout << errorMsg << endl; } cout << "\nTry changing leg2 of t5(55,30,15,12) to 6 (which is invalid):\n"; try { Trapezoid t5(55,30,15,12); t5.setLeg2(6); // this is invalid t5.print(); } catch(string errorMsg) { cout << errorMsg << endl; } cout << "\nTry changing leg1 of default t6 to .5 (which is valid):\n"; try { Trapezoid t6; t6.setLeg1(.5); // this is valid t6.print(); } catch(string errorMsg) { cout << errorMsg << endl; } return EXIT_SUCCESS; }
// trapezoid.h
#ifndef TRAPEZOID_H_
#define TRAPEZOID_H_
#include <iostream>
#include <string>
using namespace std;
class Trapezoid
{
private:
double base1, base2, leg1, leg2;
public:
Trapezoid();
Trapezoid (double base1,double base2,double
leg1,double leg2);
void setBase1(double base1);
void setBase2(double base2);
void setLeg1(double leg1);
void setLeg2(double leg2);
double getBase1();
double getBase2();
double getLeg1();
double getLeg2();
void print();
string toString();
};
// default constructor
Trapezoid::Trapezoid()
{
base1 = 1;
base2 = 1;
leg1 = 1;
leg2 = 1;
}
// parameterized constructor
Trapezoid::Trapezoid(double base1, double base2, double leg1,
double leg2)
{
// check if values are valid
if((leg1 > 0 && leg2 > 0 &&
base1 > 0 && base2 > 0) && (base1 >=
base2) && (leg1+leg2+base2) > base1)
{
this->leg1 = leg1;
this->leg2 = leg2;
this->base1 = base1;
this->base2 = base2;
}else
throw string("It doesn't satisfy
side/length rule , sum of the lengths of Leg1,Leg2 , and Base 2
must be greater than the length of Base 1");
}
// set base1
void Trapezoid::setBase1(double base1)
{
// check if base1 is valid
if((base1 > 0 ) && (base1 >= base2)
&& (leg1+leg2+base2) > base1)
{
this->base1 = base1;
}else
throw string("Invalid value for
base1");
}
// set base2
void Trapezoid:: setBase2(double base2)
{
// check if base2 is valid
if((base2 > 0) && (base1 >= base2)
&& (leg1+leg2+base2) > base1)
{
this->base2 = base2;
}else
throw string("Invalid value for
base2");
}
// set leg1
void Trapezoid:: setLeg1(double leg1)
{
// check if leg1 is valid
if((leg1 > 0) && (leg1+leg2+base2) >
base1)
{
this->leg1 = leg1;
}else
throw string("Invalid value for
leg1");
}
// set leg2
void Trapezoid:: setLeg2(double leg2)
{
// check if leg2 is valid
if((leg2 > 0 ) && (leg1+leg2+base2) >
base1)
{
this->leg2 = leg2;
}else
throw string("Invalid value for
leg2");
}
// return base1
double Trapezoid:: getBase1()
{
return base1;
}
// return base2
double Trapezoid:: getBase2()
{
return base2;
}
// return leg1
double Trapezoid:: getLeg1()
{
return leg1;
}
// return leg2
double Trapezoid:: getLeg2()
{
return leg2;
}
// print the values of Trapezoid
void Trapezoid:: print()
{
cout<<"\nLeg 1 : "<<leg1;
cout<<"\nLeg 2 : "<<leg2;
cout<<"\nBase 1 : "<<base1;
cout<<"\nBase 2 : "<<base2;
}
// return the string representation of Trapezoid
string Trapezoid::toString()
{
return ("Leg 1 :"+to_string(leg1)+" Leg 2 :
"+to_string(leg2)+" Base 1 : "+to_string(base1)+" Base 2 :
"+to_string(base2));
}
#endif
//end of trapezoid.h
//main8a.cpp : C++ program to test the Trapezoid class
#include <iostream>
#include "trapezoid.h"
using namespace std;
int main() {
cout << "Welcome to Lecture 16: Exceptions and
Templates" << endl;
cout << "The Trapezoid Program!\n";
cout << "\nTry creating t1 (no parameters,
which is valid):\n";
try
{
Trapezoid t1; // this is valid, by design
t1.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
cout << "\nTry creating t2(15,3,3,3) (which
is invalid):\n";
try
{
Trapezoid t2(15,3,3,3); // this is invalid
t2.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
cout << "\nTry creating t3(5,3,4,4) (which is
valid):\n";
try
{
Trapezoid t3(5,3,4,4); // this is valid
t3.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
cout << "\nTry changing base1 of default t4
to 10 (which is invalid):\n";
try
{
Trapezoid t4;
t4.setBase1(10); // this is invalid
t4.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
cout << "\nTry changing leg2 of
t5(55,30,15,12) to 6 (which is invalid):\n";
try
{
Trapezoid t5(55,30,15,12);
t5.setLeg2(6); // this is invalid
t5.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
cout << "\nTry changing leg1 of default t6 to
.5 (which is valid):\n";
try
{
Trapezoid t6;
t6.setLeg1(.5); // this is valid
t6.print();
}
catch(string errorMsg)
{
cout << errorMsg << endl;
}
return 0;
}
//end of main8a.cpp
Output: