Question

In: Computer Science

Declare and define a class for a fraction number. A fraction in mathematics is defined as...

Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator.

Requirements

Task1   

  • Define a fraction class that has the following member functions:
    • constructor that initializes the fraction by default arguments.
    • set function that sets the numerator of the fraction.
    • set function that sets the denominator of the fraction.
    • get function that returns the numerator of the fraction.
    • get function that returns the denominator of the fraction.
    • a function that displays the fraction.
  • Write the class in header and implementation files, and compile it separately from the client program. Name the files as fraction.h and fraction.cpp.
  • Document the class following the example of the point class posted on D2L.
  • Write a test program to show all your member functions work before you move to Task2. Name your program as project1_task1.cpp.

Task2

  • Add the following nonmember functions in your fraction header file and implementation file following the example of modified point class in the file “newpoint.h” and “newpoint.cpp”:
    • A function that returns the sum of two fractions.
    • A function that returns the difference of two fractions.
    • A function that returns the product of two fractions.
    • A function that returns the quotient of two fractions.

Name the modified files as newfraction1.h and newfraction1.cpp.

  • Write another test program to show all the operations work correctly before you move to Task 3. Name the program as project1_task2.cpp.

A run of this test program might look like this:

>a.out

Enter the first fraction: numerator denominator

3 5

Enter the second fraction: numerator denominator

2 3

The two fractions entered are

f1 = 3/5

f2 = 2/3

The arithmetic operations on these two fractions:

f1 + f2 = 19/15

f1 - f2 = -1/15

f1 * f2 = 2/5

f1 / f2 = 9/10

Task 3

Redo the Task 2 using operators.

  • Use operator overloading to define the following operations for the fraction class:
    • Sum: + as a member function
    • Difference: - as a member function
    • Product: * as a non member function
    • Quotient: / as a non member function
    • Output: << as a non member function
    • Input : >>   as a friend function of the class fraction
  • Following the example of the point class for all the documentation.
  • Write and document the class in header and implementation files, and compile it separately from the client program. Name the files as newfraction2.h and newfraction2.cpp.
  • Write a program that performs all the operations defined above. Name the program as project1_task3.cpp.

    Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator.

    Requirements

    Task1   

  • Define a fraction class that has the following member functions:
    • constructor that initializes the fraction by default arguments.
    • set function that sets the numerator of the fraction.
    • set function that sets the denominator of the fraction.
    • get function that returns the numerator of the fraction.
    • get function that returns the denominator of the fraction.
    • a function that displays the fraction.
  • Write the class in header and implementation files, and compile it separately from the client program. Name the files as fraction.h and fraction.cpp.
  • Document the class following the example of the point class posted on D2L.
  • Write a test program to show all your member functions work before you move to Task2. Name your program as project1_task1.cpp.
  • Task2

  • Add the following nonmember functions in your fraction header file and implementation file following the example of modified point class in the file “newpoint.h” and “newpoint.cpp”:
    • A function that returns the sum of two fractions.
    • A function that returns the difference of two fractions.
    • A function that returns the product of two fractions.
    • A function that returns the quotient of two fractions.
  • Name the modified files as newfraction1.h and newfraction1.cpp.

  • Write another test program to show all the operations work correctly before you move to Task 3. Name the program as project1_task2.cpp.
  • A run of this test program might look like this:

    >a.out

    Enter the first fraction: numerator denominator

    3 5

    Enter the second fraction: numerator denominator

    2 3

    The two fractions entered are

    f1 = 3/5

    f2 = 2/3

    The arithmetic operations on these two fractions:

    f1 + f2 = 19/15

    f1 - f2 = -1/15

    f1 * f2 = 2/5

    f1 / f2 = 9/10

    Task 3

    Redo the Task 2 using operators.

  • Use operator overloading to define the following operations for the fraction class:
    • Sum: + as a member function
    • Difference: - as a member function
    • Product: * as a non member function
    • Quotient: / as a non member function
    • Output: << as a non member function
    • Input : >>   as a friend function of the class fraction
  • Following the example of the point class for all the documentation.
  • Write and document the class in header and implementation files, and compile it separately from the client program. Name the files as newfraction2.h and newfraction2.cpp.
  • Write a program that performs all the operations defined above. Name the program as project1_task3.cpp.

Solutions

Expert Solution

Code

newfraction2.h

#ifndef NEWFRACTION2_H
#define NEWFRACTION2_H
#include <iostream>
using namespace std;
class fraction
{
   friend istream &operator>>(istream&, fraction&);
   friend ostream &operator<<(ostream&, fraction&);
public:
   fraction(int,int);
   fraction();
   void printfraction();
   fraction &operator=(fraction&);
   fraction operator+(const fraction& );
   fraction operator-(const fraction& );
   fraction operator *(const fraction& );
   fraction operator /(const fraction& );
  
   void reduction();
private:
   int numerator;
   int denominator;
};
#endif

newfraction2.cpp

#include"newfraction2.h";
fraction::fraction(int numerator, int denominator)
{
   this->numerator = numerator;
   //this->denominator = denominator;
   if(denominator ==0)
   {
       this->denominator = 1;
   }// end if
   reduction();
}
fraction::fraction()
{
   this->numerator=0;
   this->denominator=1;
}
fraction &fraction::operator=(fraction &number)
{
   numerator = number.numerator;
   denominator = number.denominator;
   return *this;
}
istream &operator>>(istream &in, fraction &number)
   {
       char slash;
       in >> number.numerator >> number.denominator;
       if(number.denominator == 0)
       {
           cout<<"invalid denominator/n";
           number.denominator = 1;
       }
       return in;
   }
ostream &operator<<(ostream &out, fraction &number)
   {
       out << number.numerator << '/' << number.denominator;
      
       return out;
   }
void fraction::reduction()
{
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
} // end function reduction

fraction fraction::operator+(const fraction& c1)
{
   fraction temp;
   temp.numerator=(numerator*c1.denominator)+(c1.numerator*denominator);
   temp.denominator=denominator*c1.denominator;
   temp.reduction();
   return temp;
}
fraction fraction::operator-(const fraction& c1)
{
   fraction temp;
   temp.numerator=(numerator*c1.denominator)-(c1.numerator*denominator);
   temp.denominator=denominator*c1.denominator;
   temp.reduction();
   return temp;
}
fraction fraction::operator*(const fraction& c1)
{
   fraction temp;
   temp.numerator=(numerator*c1.numerator);
   temp.denominator=denominator*c1.denominator;
   temp.reduction();
   return temp;
}
fraction fraction::operator/(const fraction& c1)
{
   fraction temp;
   temp.numerator=(numerator*c1.denominator);
   temp.denominator=denominator*c1.numerator;
   temp.reduction();
   return temp;
}

project1_task3.cpp

#include"newfraction2.h"

int main()
{
   fraction f1,f2,f3;
   cout<<"Enter firtst fraction: numerator denominator"<<endl;
   cin>>f1;
   cout<<"Enter second fraction: numerator denominator"<<endl;
   cin>>f2;
   cout<<"Ther two fraction enterd are "<<endl;
   cout<<"f1 = "<<f1<<endl;
   cout<<"f2 = "<<f2<<endl;

   cout<<"The arithmetic operations on these two fractions:"<<endl;
   f3=f1+f2;
   cout<<"f1 + f2 = "<<f3<<endl;
   f3=f1-f2;
   cout<<"f1 - f2 = "<<f3<<endl;
   f3=f1*f2;
   cout<<"f1 * f2 = "<<f3<<endl;
   f3=f1/f2;
   cout<<"f1 / f2 = "<<f3<<endl;

   return 1;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
Define packing fraction of a crystal . Obtain an expression for the packing fraction for the...
Define packing fraction of a crystal . Obtain an expression for the packing fraction for the BCC structure
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in...
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList. Deliverables: EmptyQueueException.java (given in Lab_7_StartUp folder) QueueInterface.java (given in Lab_7_StartUp folder) ArrayListQueue.java (need to create) Lab7.java (need to create) QueInterface: @author Frank M. Carrano @author Timothy M....
In Java Define the EvenNumber class for representing an even number. The class contains: A data...
In Java Define the EvenNumber class for representing an even number. The class contains: A data field value of the int type that represents the integer value stored in the object. A no-arg constructor that creates an EvenNumber object for the value 0. A constructor that constructs an EvenNumber object with the specified value. A function named getValue() to return an int value for this object. A function named getNext() to return an EvenNumber object that represents the next even...
Define a class called Rational for rational numbers. Arational number is a number that can...
Define a class called Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, /2, 3/4, 64/2, and so forth are all rational numbers.Represent rational numbers as two private values of type int, numfor the numerator and den for the denominator. The class has a default constructor with default values (num = 0,den = 1), a copy constructor, an assignment operator and three friend functions for operator overloading...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
How does one declare a class variable in C#?
How does one declare a class variable in C#?
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT