Question: Assignment 4 (a) Re-code the program in Assignment 3 in Java. (b) Design the try-catch mechanism so that division by zero does not produce wrong
Assignment 4
(a) Re-code the program in Assignment 3 in Java.
(b) Design the try-catch mechanism so that division by zero does not produce wrong output.
(b) Run you program and include the output in your submission.
/*-----------BinaryOperators.h--------------*/ #ifndef BINARYOPERATIONS_H #define BINARYOPERATIONS_H #include #include #include #include using namespace std; class BinaryOperator{ public: BinaryOperator (double op1, double op2): fOp1(op1), fOp2(op2){} virtual double DoOp () const = 0; protected: const double fOp1; const double fOp2; }; //class BinaryOPerator class Adder: public BinaryOperator{ public: Adder(double op1, double op2) : BinaryOperator(op1, op2){} virtual double DoOp () const; };//class Adder class Subtractor: public BinaryOperator{ public: Subtractor(double op1, double op2) : BinaryOperator(op1, op2){} virtual double DoOp () const; };//class Subtractor class Multiplier: public BinaryOperator{ public: Multiplier(double op1, double op2) : BinaryOperator(op1, op2){} virtual double DoOp () const; };//class Multiplier class Divider: public BinaryOperator{ public: Divider(double op1, double op2) : BinaryOperator(op1, op2){} virtual double DoOp () const; };//class Divider void Test(); #endif /*-----------BinaryOperators.cpp--------------*/ #include #include #include "BinaryOperators.h" using namespace std; double Adder::DoOp() const{ return fOp1 + fOp2 ; } double Subtractor::DoOp() const{ return fOp1 - fOp2 ; } double Multiplier::DoOp() const{ return fOp1 * fOp2 ; } double Divider::DoOp() const{ if (fOp2 == 0){ throw string("Divide by zero"); } return fOp1 / fOp2 ; } void Test(){ BinaryOperator *bptr; //3+7 Adder a(3,7); bptr = &a; cout <<"Adder output is " << bptr->DoOp() <DoOp() <DoOp() <DoOp() <DoOp() <