Question

In: Computer Science

afe Rational Fractions In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make...

afe Rational Fractions

In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make sure you have a working fractionType class before starting this assignment. The template requirement from week 4 is not required for this assignment.

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<int> num1(1,0);

fractionType<int> num2(0,3);

fractionType<int> 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

Solutions

Expert Solution

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





Feel free to reach out if you have any doubts or require any modifications..
Please do rate the answer thanks...


Related Solutions

C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
4) Chapter 13, problem 33 (page 598) Given the following list of items a) classify the...
4) Chapter 13, problem 33 (page 598) Given the following list of items a) classify the items as A, B, or C b) Determine the economic order quantity for each item (round to the nerest whole unit) item est annual demand ordering cost holding cost % unit price h4 20000 50 20 2.5 h5 60200 60 20 4 p6 9800 80 30 28.5 p7 14500 50 30 12 p8 6250 50 30 9 p9 7500 50 40 22 ts 21000...
CHAPTER 2-4 Exercise 2.24 In this exercise, we examine the effect of combining investments with positively...
CHAPTER 2-4 Exercise 2.24 In this exercise, we examine the effect of combining investments with positively correlated risks, negatively correlated risks, and uncorrelated risks. A firm is considering a portfolio of assets. The portfolio is comprised of two assets, which we will call ''A" and "B." Let X denote the annual rate of return from asset A in the following year, and let Y denote the annual rate of return from asset B in the following year. Suppose that E(X)...
CS3323 Homework 4 (Week 4: Chapter 12, 13) 1. List three characteristics that can serve as...
CS3323 Homework 4 (Week 4: Chapter 12, 13) 1. List three characteristics that can serve as a guide to evaluate design quality. (Section 12.2.1) 2. Explain how effective modular design is achieved through functional independence of the individual modules? (Section 12.3.5) 3. Describe the principle of information hiding as it applies to software design. (Section 12.3.6) 4. What are the elements that make up a software architectural style? (Section 13.3) 5. What is an archetype? (Section 13.6.2) 6. Explain the...
Business ethics This week we are covering the textbook topics found in Chapter 4, The Nature...
Business ethics This week we are covering the textbook topics found in Chapter 4, The Nature of Capitalism (beginning on page 117) and Chapter 5 Corporation (beginning on page 156). For this week’s discussion you need to post your thoughts on the following questions. Per our textbook, there are four key features of capitalism which include the existence of companies, the goal of making a profit, a competitive market, and private property ownership. How do these four features of capitalism...
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which...
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which creates a Point class with 2 instance variables; the xCoordinate and yCoordinate. It should have a default constructor and a values constructor. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes as follows. point: (x, y) Part 2 Do number 4 on page 719 which creates a Circle...
1. Chapter 14: Exercise 4 on page 462 (8 marks) Groceries: A grocer store’s receipts show...
1. Chapter 14: Exercise 4 on page 462 Groceries: A grocer store’s receipts show that Sunday customer purchases have a skewed distribution with a mean of $ standard deviation of $20 A) Explain why you cannot determine the probability that the next Sunday customer will spend at least $40.00. B) Can you estimate the probability that the next 10 Sunday customers will spend an average of at least $40.00 C) Is it likely that the next 50 Sunday customers will...
Critical Thinking Exercise: Hostility, Depression, and Heart Disease Now that you have completed Chapter 10, take...
Critical Thinking Exercise: Hostility, Depression, and Heart Disease Now that you have completed Chapter 10, take your learning a step further by testing your critical thinking skills on the following practical problem-solving exercise. Although everyone feels angry from time to time, when everyday anger turns destructive or occurs with increasing frequency, it can have a variety of unhealthy effects. In addition to reducing the overall quality of a person’s life, uncontrolled anger can damage relationships and make people feel as...
Exercise 4. Let ≥ be a rational, monotone and continuous preference on R2 +. Suppose that...
Exercise 4. Let ≥ be a rational, monotone and continuous preference on R2 +. Suppose that ≥ is such that there is at least one strict preference statement, i.e. there exists two bundles x and y in R2 + such that x>y. (a) Is there a discontinuous utility representation of ≥ ? Justify your answer. (b) Would your answer change if if we didn’t have at least one strict preference statement? Justify your answer. I don't know where my professor...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT