Question

In: Computer Science

]write two main programs: one will add exception handling to a geometric object, and the other...

]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;
}

Solutions

Expert Solution

// 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:


Related Solutions

EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes (Session and Problems) and one driver class and ensure the user inputs cannot cause the system to fail by using exception handling. Overview from Project . Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+,-, *, /), the range of the factors to be used in the problems, and the number of problems...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are...
What advantages does object-oriented exception handling provide? When a program contains multiple catch blocks, how are they handled? Write the statement to declare a three-by-four array of integers with the elements initialized to zero. Name the array myArray. What is the difference between volatile and nonvolatile storage in Java programming? Give examples of different storage situations. What are some of the advantages of the ArrayList class over the Arrays class? How can you use the enhanced for loop? What does...
Function Template and Exception Handling In part one, you are going to implement the selection sort...
Function Template and Exception Handling In part one, you are going to implement the selection sort function that is capable of sorting vectors of int, double or string. In part two you will be writing a try catch block to catch the out-of-range exception. You are to write three functions and manipulate main() function for this lab all of which should be written in one main.cpp file: Part one: unsigned min_index(const vector<T> &vals, unsigned index): Passes in an index of...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT