can someone help me with this assignment?

im really confused about this assignment, i know how to do the separate files part but im having trouble with rest of the assignment any help would be greatly appreciated. Its supposed to do this:

Write a class called Fraction and an application program which tests the class. Your job is to write 3 files, completely from scratch.

1. Fraction.h -- header file
2. Fraction.cpp -- implementation file
3. FractApp.cpp -- application file

1. Class Fraction (in "Fraction.h" and "Fraction.cpp")

Fractions are of the form a / b, where a is the numerator and b is the denominator, and a,b are integers (and b is not 0). In addition to the two int data members, provide the following methods (15 class methods, 1 friend function and 1 regular function):

* Class methods:
1. Fraction(int n = 0, int d = 1);
2. int getNum() const;
3. int getDenom() const;
4. Fraction operator+(const Fraction& f2) const;
5. Fraction operator-(const Fraction& f2) const; // binary subtraction
6. Fraction operator*(const Fraction& f2) const;
7. Fraction operator/(const Fraction& f2) const;
8. Fraction operator-() const; // unary minus
9. bool operator==(const Fraction& f2) const;
10. bool operator<(const Fraction& f2) const;
11. bool operator<=(const Fraction& f2) const;
12. bool operator>(const Fraction& f2) const;
13. bool operator>=(const Fraction& f2) const;
14. void reduce();
15. void standardize();
* Friend functions:
16. ostream& operator<<(ostream& out, const Fraction& f);
* Regular functions:
17. bool operator!=(const Fraction& f1, const Fraction& f1);

Notes on the methods and functions:

* The constructor and two get methods should be straight-forward.
As a reminder, the full definition of the constructor (in the implementation file) should not have default values in the parameter.
* Arithmetic operations on fractions are defined by the following rules:
o a / b + c / d = (ad + bc) / bd
o a / b - c / d = (ad - bc) / bd
o a / b * c / d = ac / bd
o (a / b) / (c / d) = ad / bc (where c/d is not 0)
o -(a / b) = (-a) / b
* Relational operators should be obvious, after understanding the arithmetic operations.
NOTE: Do NOT convert a Fraction to a double in order to compare two Fraction's. That's because a double can express only up to 15 decimal digits, while a Fraction can express numbers of higher precisions.
* The reduce() method modifies 'this' object to a reduced form. For example, 10/15 ==> 2/3.
To write this method, you will need to find the greatest common divisor (gcd) of the numerator and denominator. You are welcome to use the following. You can put it as a private method (since it will only be called by other member functions, not from outside).

class Fraction
{
public:
...
private:
...
int gcd(int a, int b) const;
};

int Fraction::gcd(int a, int b) const
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}

* The standardize() method modifies 'this' object to a standardized form (with a positive denominator). For example,

2/-3 ==> -2/3
-2/-3 ==> 2/3

* The output operator (<<) should print the fraction as "a/b" to the parameter out.

2. Application (in "FracApp.cpp")

Write an application which tests all the methods you wrote. The output of the program should be:

1/3 + 5/8 = 23/24
1/3 - 5/8 = -7/24
1/3 * 5/8 = 5/24
1/3 / 5/8 = 8/15
-(1/3) = -1/3

24/21, after reduce(), 8/7
5/8, after reduce(), 5/8

2/-3, after standardize(), -2/3
-2/-3, after standardize(), 2/3

11/15 < 17/23 is true
11/15 <= 17/23 is true
11/15 > 17/23 is false
11/15 >= 17/23 is false
11/15 == 17/23 is false
11/15 != 17/23 is true

11/15 < 22/30 is false
11/15 <= 22/30 is true
11/15 > 22/30 is false
11/15 >= 22/30 is true
11/15 == 22/30 is true
11/15 != 22/30 is false

Write necessary code to produce this output. Of course you must create appropriate Fraction objects and call methods to produce the results.
NOTE: There is no interaction with the user in the program.
Uh...without doing your whole HW - I'm not even sure where to start other than how to start creating your 3 files.

1. Fraction.h -- header file
2. Fraction.cpp -- implementation file
3. FractApp.cpp -- application file

Starting with 3. FractApp.cpp, this is basically where your main() will be to run your program. Anything written in the section of FractApp.cpp is basically supposed to test the class (I'm guessing you already know what these are, if you don't say so - so I can modify my answer here) you are supposed to write in Fraction.h.

1. Fraction.h
This is where you write the class header's - uh... a header is like the function without the body. IE:

full function
void DoSomething(){
blahblha;
}

The header of above:
void DoSomething();

2. Fraction.cpp
This is where the stuff that makes your program work would go.

I can't go into more detail without having more specific questions.
the part i have trouble with is the actual defining of all the functions where you need to make it do something. I dont really know what to put in each of the class functions. im not really good at calculating things basically so i dont know what goes into the functions.

like these:

* Class methods:
1. Fraction(int n = 0, int d = 1);
2. int getNum() const;
3. int getDenom() const;
4. Fraction operator+(const Fraction& f2) const;
5. Fraction operator-(const Fraction& f2) const; // binary subtraction
6. Fraction operator*(const Fraction& f2) const;
7. Fraction operator/(const Fraction& f2) const;
8. Fraction operator-() const; // unary minus
9. bool operator==(const Fraction& f2) const;
10. bool operator<(const Fraction& f2) const;
11. bool operator<=(const Fraction& f2) const;
12. bool operator>(const Fraction& f2) const;
13. bool operator>=(const Fraction& f2) const;
14. void reduce();
15. void standardize();
* Friend functions:
16. ostream& operator<<(ostream& out, const Fraction& f);
* Regular functions:
17. bool operator!=(const Fraction& f1, const Fraction& f1);
Ok so you know what classes are. Basically you're making a class (@ a quick glance through your specs)

Your class contains those functions you listed as public members.

Your class also contains private members in the form of:
int Numerator;
int Denominator;

Classes are written as (this would be in your Fraction.h file)
class <class name>
{
public:
private:
};

IE:
//---------
Fraction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//1*
#ifndef _FRACTION_   
#define _FRACTION_

class Fraction
{
public:
  Fraction(int n = 0, int d = 1); //your constructor
  int getNum() const;

  //...
  Fraction operator+(const Fraction& f2) const;

  //etc down your list until
  friend ostream& operator<<(ostream& out, const Fraction& f);
private:
  int Num,Denom;  //2*
};

#endif 


1* These are precompile headers meaning these are executed before the code is compiled. The reason we do this is because in most cases, a .h file is included many times throughout a program. If compiled without these headers the compiler will throw an error that is something like "This blahblha has already been defined in faction.obj" - basically it doesn't like it that it has already seen it before and seems to be forced into compiling it again.

#ifndef _FRACTION_
This means if not defined, it is paired with #endif and functions like a C++ 'if'. I am asking the compiler if it has NOT seen this before, if it is true meaning this is the first time it ran into this block of code, then compile the following code up to the point of #endif.

#define _FRACTION_
If #ifndef is true, then I tell the compiler to define _FRACTION_ thus, the compiler now knows that _FRACTION_ exists and will continue on to the rest of the code. The reason for the #define is to make #ifndef untrue. This is how we guarantee that our header file is compiled only once.

2* The reason I suspect you have these two variables as members of your class is because of
Fraction(int n = 0, int d = 1);

Which appears to be the constructor. It passes in two variables and since I see you have two other functions whose purpose is to return the numerator and denominator then those members are likely private.

2. int getNum() const;
3. int getDenom() const;

//------------------
To your Fraction.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "Fraction.h"
#include <iostream>
using namespace std; //don't worry about this, just put it in so cout works

//declaring our constructor : Fraction(int n = 0, int d = 1);
Fraction::Fraction(int n, int d)
{
  Num = n;
  Denom = d;

  //probably a good idea to do some error catching here like
  if( d == 0 )
    cout<<"Hey don't make blackholes by dividing by zero!";
}

int Fraction::getNum() const{ return Num; }

//same thing down to the end, here is another example
//here we're overloading
Fraction Fraction::operator+(const Fraction& f2) const
{
  Fraction ReturnObj;

  //fractions have to be the same denominator to be added together 
  if( Denom == f2.Denom )
  {
    ReturnObj.Num = Num + f2.Num;
    ReturnObj.Denom = Denom;
  }
  //uh - oh they're not the same denom
  else
  {
    //this would solve the problem but not the most efficient way
    //you would have to see if it is possible to reduce the fraction after
    //adding, ie 4/8 reduced is 1/2
    ReturnObj.Num = Num*f2.Denom + f2.Num*Denom;
    ReturnObj.Denom = Denom*f2.Denom;
  }  
  return ReturnObj;
}


//----------
Onto FractApp.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "Fraction.h"

int main()
{
  Fraction f1(1,2);  //initalizing your object
  Fraction f2(2,3);

  cout<< "This is my numorator!"<< f1.getNum()  << endl;
  cout<< "Adding f1 and f2, my numerator should be 7!" << (f1+f2).getNum() <<endl;
  return 0;
}


If you create 3 files and compile these, they should work fine and compile cleanly. Well for the most part...I coded this in the terribly small edit box so there may be compile errors. This should be more than enough to get you started. .
Last edited on
well while usin class, one sud metion private at the beginning followed by public!!!!!
Last edited on
man i suck!!! can someome give me a step by step procedure cause now i get errors regarding the friend functions. Basically, can i have a step by step walk through on how to solve this assignment. I suck, im not a real good programmer.
milee -
It doesn't matter which is first. Classes are private by default and inherited private. You can write a class like this:

1
2
3
4
5
6
7
8
class A
{
//don't need to write private because classes are already private
    int mA; //private already;

public:
    A();
};


burnout
Remove the friend for now. Keep trying. Write something...anything to show you're at least trying otherwise we're doing our HW for you. Which isn't a bad idea except I'm not getting paid for this :)
ok so here is what i got so far. I hope im on the right track:

in Fraction.h file:

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;

class Fraction
{
private:
int Num,Denom;
public:
Fraction(int n = 0, int d = 1);
int getNum() const;
int getDenom() const;
Fraction operator+(const Fraction& f2) const;
Fraction operator-(const Fraction& f2) const; // binary subtraction
Fraction operator*(const Fraction& f2) const;
Fraction operator/(const Fraction& f2) const;
Fraction operator-() const; // unary minus

};

#endif

in Fraction.cpp file:

#include "Fraction.h"


Fraction::Fraction(int n, int d)
{
Num = n;
Denom = d;
if(d == 0)
cout << "Enter Denominator that is greater than 0";
}

int Fraction::getNum() const
{
return Num;
}

int Fraction::getDenom() const
{
return Denom;
}

Fraction Fraction::operator +(const Fraction &f2) const
{
Fraction total;

total.Num = Num * f2.Denom + f2.Num * Denom;
total.Denom = Denom * f2.Denom;

return total;
}

Fraction Fraction::operator-(const Fraction& f2) const
{
Fraction totalDiff;

totalDiff.Num = Num * f2.Denom + f2.Num * Denom;
totalDiff.Denom = Denom * f2.Denom;

return totalDiff;
}

Fraction Fraction::operator*(const Fraction& f2) const
{
Fraction totalMult;

totalMult.Num = Num * f2.Num;
totalMult.Denom = Denom * f2.Denom;

return totalMult;
}

Fraction Fraction::operator/(const Fraction& f2) const
{
Fraction totalDiv;

totalDiv.Num = Num * f2.Denom;
totalDiv.Denom = Denom * f2.Num;

return totalDiv;
}

How would i do these?:

Fraction operator-() const; // unary minus
bool operator==(const Fraction& f2) const;
bool operator<(const Fraction& f2) const;
bool operator<=(const Fraction& f2) const;
bool operator>(const Fraction& f2) const;
bool operator>=(const Fraction& f2) const;
void reduce();
void standardize();
* Friend functions:
ostream& operator<<(ostream& out, const Fraction& f);
* Regular functions:
bool operator!=(const Fraction& f1, const Fraction& f1);



Topic archived. No new replies allowed.