Question

C++ Your assignment this week is to make your fractionType class safe by using exception handling....

C++ Your assignment this week is to make your fractionType class safe by using exception handling. Use exceptions so that your program handles the exceptions division by zero and invalid input. · An example of invalid input would be entering a fraction such as 7 / 0 or 6 / a · An example of division by zero would be: 1 / 2 / 0 / 3 Use a custom Exception class called fractionException. The class must inherit from exception (see example in lecture and note the entire class can be implemented in the .h file as in the lecture). Test your safe fractionType class with a main method that forces an invalid fraction and a divide by zero. The catch block in the main method must report which kind of error was encountered – i.e. invalid fraction or divide by zero. The following can be used to test an exception: try { fractionType num1(1,0); fractionType num2(0,3); fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); num3 = num1 / num2; cout << num1 << " / " << num2 << " = " << num3 << endl; } catch (fractionException e) { cout << "Exception caight in main: " << e.what() << endl; } Submission Guidelines: Submit modified fractionType class (.h and .cpp file if exists) Submit fractionException class (.h file and .cpp file if exists) Submit main test .cpp program to test the exception handling

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*fraction.h*/

#ifndef FRACTION_H
#define FRACTION_H

#include<iostream>
#include<exception>

using namespace std;

/*Exception class derived from std::exception*/
class fractionException : public exception
{
   private:
       const char *msg;
   public:
       fractionException(const char *m) : msg(m){}
       virtual const char *what() const throw()
       {
           return msg;
       }
};

//fractionType class definition
class fractionType
{
   private:
       int n, d;
      
   public:
       fractionType() : n(0), d(1){}
       fractionType(int num, int den) : n(num), d(den)
       {
           if(den == 0)
               throw fractionException("Division by zero");//Throw division by zero exception
       }
      
       fractionType operator/(const fractionType &x);
       friend ostream& operator<<(ostream &out, const fractionType &f);
       friend istream& operator>>(istream &in, fractionType &f);
};

#endif

/*End of franction.h*/

/*fraction.cpp*/

#include"fraction.h"
#include<iostream>

using namespace std;

//Function to perform division
fractionType fractionType::operator/(const fractionType &x)
{
   if(x.n == 0)
       throw fractionException("Division by zero");//Throw division by zero exception
   fractionType f;
   f.n = this->n*x.d;
   f.d = this->d*x.n;
   return f;
}

//Overloaded << operator
ostream& operator<<(ostream &out, const fractionType &f)
{
   out<<f.n<<"/"<<f.d;
   return out;
}

//Overloaded >> operator
istream& operator>>(istream &in, fractionType &f)
{
   in>>f.n;
   if(in.fail())
       throw(fractionException("Invalid Input"));//Throw invalid input exception
   in.ignore(1);
   in>>f.d;
   if(in.fail())
       throw(fractionException("Invalid Input"));//Throw invalid input exception
   if(f.d == 0)
       throw fractionException("Division by zero");//Throw division by zero exception  
   return in;
}

/*end of fraction.cpp*/

/*main.cpp*/

#include<iostream>
#include<iomanip>
#include "fraction.h"

using namespace std;

int main()
{
   try
   {
       fractionType num1(1,2);
       fractionType num2(0,3);
       fractionType num3;
       cout << fixed;
       cout << showpoint;
       cout << setprecision(2);
       num3 = num1 / num2;
       cout << num1 << " / " << num2 << " = " << num3 << endl;
   }
   catch (fractionException e)
   {
       cout << "Exception caught in main: " << e.what() << endl;
   }
   return 0;
}

/*End of main.cpp*/

SAMPLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
C++ Your assignment this week is to make your fractionType class safe by using exception handling....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

  • 12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user...

    12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user will enter 2 numbers and attempt to divide (in a try block) the 1st number by the 2nd number. The program will provide for 2 different catch statements: Division by zero - denominator is zero Division results in a negative number The user should be prompted with Enter 2 numbers: For input of 7 and 0 the output should be Exception: Division by zero...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates...

    Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates the error handling routine from the rest of the code. In this application, you practice handling exception errors. You use sample code that was purposefully designed to generate an exception error, and then you modify the code so that it handles the errors more gracefully. For this Assignment, submit the following program: The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT