Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

0000 T-Mobile 88% daveteaches.com Write a fraction class whose objects will repr

ID: 3679816 • Letter: 0

Question

0000 T-Mobile 88% daveteaches.com Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You 1. A set) operation that takes two integer arguments, a numerator and a denominator, and sets the calling object 2. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy. In these functions you will need to declare a local "fraction" variable, assign to it the result of the mathematical operation, and then return it 3. A boolean operation named isEqualTo that compares two fraction objects for equality. Since you aren't reducing your fractions, you'll need to do this by cross-multiplying. A little review: if numerator1denominator2 equals denominatorl numerator2, then the fractions are equal. 4. An output operation named print that displays the value of a Your class should have exactly two data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented. Here's a hint for how you will set up your arithmetic operation functions: You need two fractions. One is the parameter, one is the calling object. The function multiplies the calling object times the parameter and returns the result. In some ways it is similar to the comesBefore) function from the lesson. That function needs two fractions, and one is the calling object and one is the parameter When adding or subtracting fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator I am providing a client program for you below. You should copy and paste this and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it. I strongly suggest that you design your class incrementally. For example, you should first implement only the set function and the output function, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to member

Explanation / Answer

#include <iostream>

using namespace std ;

int main ( )
{
fraction f1 ( 9 , 8 ) ;
fraction f2 ( 2 , 3 ) ;
fraction result ;
fraction f3 ;
cout<< " The result starts off at " ;
result.print ( ) ;
cout <<endl ;

cout <<" The product of " ;
f1.print ( ) ;
cout <<" and " ;
f2.print ( ) ;
cout <<" is " ;
result =f1.MultipliedBy ( f2 ) ;
result.print ( ) ;
cout <<endl ;
  
f3 =result ;
cout <<" The sum of " ;
f1.print ( ) ;
cout <<" and " ;
f2.print ( ) ;
cout <<" is " ;
result =f1.AddedTo ( f2 ) ;
result.print ( ) ;
cout <<endl ;

cout <<" The difference of " ;
f1.print ( ) ;
cout <<" and " ;
f2.print ( ) ;
cout <<" is " ;
result =f1.Subtract ( f2 ) ;
result.print ( ) ;
cout <<endl ;

if( f1.isEqualTo ( f2 ) )
{
cout <<" The two fractions are equal. " <<endl ;
} else
{
cout <<" The two fractions are not equal. " <<endl ;
}
  
const fraction f4 ( 12 , 8 ) ;
const fraction f5 ( 202 , 303 ) ;

result =f4.DividedBy ( f5 ) ;
cout <<" The quotient of " ;
f4.print ( ) ;
cout <<" and " ;
f5.print ( ) ;
cout <<" is " ;
result.print ( ) ;
cout <<endl ;

system( " PAUSE " ) ;
return 0 ;
}


fraction::fraction ( )
{
numerator =0 ;
denominator =1 ;   
}
fraction fraction::print ( )const
{
int num = numerator ;
int den = denominator ;
if ( num > den )
{
for ( int counter = 2 ; counter < den ; counter++ )
{
while ( num % counter == 0 &den % counter == 0 )
{
num = ( num / counter ) ;
den = ( den / counter ) ;
}
}
}
else if ( den > num )
{
for ( int counter = 2 ; counter < num ; counter++ )
{
while ( num % counter == 0 &den % counter == 0 )
{
num = ( num / counter ) ;
den = ( den / counter ) ;
}
}
}

cout << num << " / " << den ;
}

fraction::fraction ( int newNumerator , int newDenominator )
{
numerator = newNumerator ;
denominator = newDenominator ;
}
bool fraction::isEqualTo ( fraction value )
{
( numerator / denominator ) == ( value.numerator / value.denominator ) ;
}
bool fraction::isGreaterThan( fraction value )
{
int fraction1 ;
int fraction2 ;
fraction1 = ( numerator / denominator ) ;
fraction2 = ( value.numerator / value.denominator ) ;
fraction1 > fraction2 ;
}
fraction fraction::DividedBy( fraction value )const
{
fraction result ;
result.numerator =numerator *value.denominator ;   
result.denominator =denominator *value.numerator ;
return result ;   
}
fraction fraction::MultipliedBy( fraction value )const
{
fraction result ;
result.numerator =numerator *value.numerator ;   
result.denominator =denominator *value.denominator ;
return result ;
}
fraction fraction::Subtract( fraction value )const
{
fraction result ;
result.numerator =( numerator *value.denominator ) - ( value.numerator * denominator ) ;   
result.denominator =denominator *value.denominator ;
return result ;
}
fraction fraction::AddedTo( fraction value )const
{
fraction result ;
result.numerator =( numerator *value.denominator ) + ( value.numerator * denominator ) ;   
result.denominator =denominator*value.denominator ;
return result ;

}

#include <iostream>

using namespace std ;

class fraction
{
private:
int numerator ;
int denominator ;

  
  
public:
fraction() ;
fraction( int , int ) ;
fraction AddedTo( fraction value )const ;
fraction MultipliedBy( fraction value )const ;
fraction Subtract( fraction value )const ;
fraction DividedBy( fraction value )const ;
fraction isGreaterThan( ) ;
fraction isEqualTo( ) ;
fraction print ( )const ;
} ;