Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java, add features into the Fraction class as described below. Fraction exercise Start with this copy of the Fraction class (see below) and add

Using Java, add features into the Fraction class as described below.

Fraction exercise

Start with this copy of the Fraction class (see below) and add the following features:

  • Write a method called Simplify, which returns a simplified version of the calling object (no parameters are needed). The method should return a new Fraction (simplified), but not change the original one. The fraction in the form 0/N should have simplified form 0/1. Any other fraction has the usual mathematical definition of "simplified form". Keep within the rules already established in this Fraction class (e.g. denominator always positive, any negative values go in the numerator, etc).
  • Write methods add, subtract, multiply, and divide. Each one should take in a Fraction as a parameter and perform the given computation between the calling object and the parameter object. (The calling object is always the first operand). The result of each operation should always be a fraction returned in simplified form. Example calls:
     f1.add(f2) // means f1 + f2 f1.subtract(f2) // means f1 - f2 
    In divide, if an attempt is made to divide by a fraction with the value 0, default the result to 0/1. (Such division is actually undefined. 0/1 is not the "true" result, but we need to return something from this method).
  • Here are your method signatures:
     public Fraction simplify() public Fraction add(Fraction f) public Fraction subtract(Fraction f) public Fraction multiply(Fraction f) public Fraction divide(Fraction f) 
  • Make sure that your new methods enforce the following rules: the denominator must be non-negative (any negative sign goes in the numerator) and the denominator must never be zero (this would be undefined).

_________________________________________

See below for Fraction.java

public class Fraction { private int numerator = 0; // numerator (and keeps sign) private int denominator = 1; // always stores positive value public Fraction() { } public Fraction(int n, int d) { if (set(n,d)==false) set(0,1); } public boolean set(int n, int d) { if (d > 0) { numerator = n; denominator = d; return true; } else return false; } public String toString() { return (numerator + "/" + denominator); } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } public double decimal() { return (double)numerator / denominator; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions