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

Problem a (PA9a.java) JAVA code Write a Fraction class – that is, write your own

ID: 3767252 • Letter: P

Question

Problem a (PA9a.java) JAVA code

Write a Fraction class – that is, write your own class that will represent a fraction in your program. Each variable of type Fraction represents a single fraction. That means that the class should have at least two member variables that should be private: the numerator and the denominator of the fraction. The class also must have the following public member functions:

• Fraction(int n, int d); // constructor that defines a fraction n/d

• Fraction(); // constructor that defines a default fraction of 0/1

• Fraction(Scanner s); // constructor that defines a fraction via Scanner input

• double toDecimal(); // returns the decimal value of the fraction

• String toString(); // returns the string form of the fraction o "numerator" if denominator is 1 o "numerator/denominator (decimal, with three decimal places)" otherwise

• int getNumerator(); // returns the numerator of the fraction • int getDenominator(); // returns the denominator of the fraction • Fraction plus(Fraction f); // returns fraction + parameter

• Fraction minus(Fraction f); // returns fraction - parameter • Fraction times(Fraction f); // returns fraction * parameter

• Fraction divides(Fraction f); // returns fraction / parameter

You have been provided a private method:

• int[] simplifyFraction(int[] f); // returns simplified version of parameter

Using this method, all constructors should make sure that the fraction is in reduced form. You may not change the public API – that is, any methods not listed above must be made private.

Explanation / Answer

#include <iostream>

using namespace std;

class fraction

{

public:

void plus_equals()

{

}

void minus_equals()

{

}

void times_equals()

{

}

void divide_equals()

{

}

double to_decimal()

{

}

void input()

{

cin >> numerator >> denominator;

}

void output()

{

cout << numerator << "/" << denominator << endl;

}

private:

void reduce()

{

}

int numerator;

int denominator;

};

int main()

{

fraction a, b;

cout << "Enter a fraction: ";

a.input();

a.output();

cout << "Enter a fraction: ";

b.input();

b.output();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote