Question
Can you please help me get started with this java homework? Write a class named Fraction that will store fractional values as rational numbers, maintaining
Can you please help me get started with this java homework?
Write a class named Fraction that will store fractional values as rational numbers, maintaining both a numerator and a denominator.
Use the following guidelines.
The class should internally maintain a numerator and a denominator in lowest terms, i.e. their GCD should always be 1.
The class should should support positive and negative rational numbers.
The class should have a default constructor initializing a Fraction to 0.
The class should have a two parameter constructor Fraction(int n, int d) to support initialization with arbitrary numerators and denominators
The class should implement the following methods
Fraction add(Fraction)
Fraction subtract(Fraction)
Fraction multiply(Fraction)
Fraction divide(Fraction) - throw new ArithmeticException("zerodivide"); on zero divide.
The class should have a public String toString() method to return a String representation of the rational number.
The class should implement the following accessor methods
int getNumerator()
int getDenominator()
double getDecimal()
The template for the class is below. You must fill in the methods.
public class Fraction { int numerator; int denominator; Fraction() { } Fraction(int n, int d) { } public int getNumerator() { return 0; } public int getDenominator() { return 0; } public double getDecimal() { return 0; } public String toString() { return ""; } public Fraction add(Fraction opr) { return null; } public Fraction subtract(Fraction opr) { return null; } public Fraction multiply(Fraction opr) { return new Fraction(numerator * opr.numerator, denominator * opr.denominator); } public Fraction divide(Fraction opr) { return null; } }
Copy and paste methods from the previous GCD assignment as needed, or use the GCD object, your choice.
Test the class with the following code, which you should put in a static public void main(String[]) method in a separate class named AssignFraction.
Fraction f0 = new Fraction(); Fraction f1 = new Fraction(2, 2); Fraction f2 = new Fraction(2, 7); Fraction f3 = new Fraction(1, -3); System.out.println(f1 + " + " + f0 + " = " + f1.add(f0)); System.out.println(f1 + " * " + f2 + " = " + f1.multiply(f2)); System.out.println(f1 + " / " + f3 + " = " + f1.divide(f3)); System.out.println(f2 + " + " + f3 + " = " + f2.add(f3)); System.out.println(f2 + " - " + f3 + " = " + f2.subtract(f3)); System.out.println(f2 + " * " + f3 + " = " + f2.multiply(f3)); System.out.println(f2 + " / " + f3 + " = " + f2.divide(f3)); System.out.println("the numerator of " + f3 + " is " + f3.getNumerator()); System.out.println("the denominator of " + f3 + " is " + f3.getDenominator()); System.out.println("the decimal value of " + f3 + " is " + f3.getDecimal());
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started