Question: 1. Add two constructors to your fraction class. One should be a default constructor which creates the fraction 0/1. The other should accept numerator and

1. Add two constructors to your fraction class. One should be a default constructor which creates the fraction 0/1. The other should accept numerator and denominator arguments.

2. Add a method dblValue to your fraction class which returns the double precision approximate value of the fraction. Eg, if a client has a fraction f which represents 1 / 2 then f.dblValue() should return the double number 0.5.

3. Add a private method simplify to your fraction class which converts the fraction to its simplest form. For example, the fraction 20/60 should be stored as 1/3.

4. Add a toString method to your fraction class which returns the fraction as a String in the form “x/y”, where x and y are numerator and denominator respectively.

5. Write a client program which allows the user to add up any number of non-zero fractions. The program should display the running total as an exact fraction (in simplified form as x/y) and as an approximate double value. The user can finish by entering a fraction which represents zero.

NOTE:  Fraction class from code below

Program in NETBEANS

package com.frac;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Fraction
{
    // question 1
    private int numerator;
    private int denominator ;

    // Function to get input
    public void get_input( int n ,int d)
    {
        // for question 4
        if (d == 0){
            d = 1;
        } else if (d<0){
            n *= -1;
            d *= -1;
        }
        numerator=n;
        denominator=d;
        reduce();
    }

    private int gcd(int n1, int n2){
        if (n2 == 0){
            return n1;
        }
        return gcd(n2, n1 % n2);
    }

    // question 2
    public boolean isZero(){
        return numerator == 0;
    }

    // question 3
    public boolean equals(Fraction o){
        return numerator == o.numerator && denominator == o.denominator;
    }

    // question 3
    public Fraction add(Fraction o){
        int denom = denominator * o.denominator;
        int num = (denom / denominator) * numerator + (denom / o.denominator) * o.numerator;
        Fraction res = new Fraction();
        res.numerator = num;
        res.denominator = denom;
        res.reduce();
        return res;
    }

    public int getDenominator() {
        return denominator;
    }

    public int getNumerator() {
        return numerator;
    }

    public void setDenominator(int denominator) {
        if (denominator < 0){
            numerator *= -1;
            denominator *= -1;
        } else if (denominator == 0){
            denominator = 1;
        }
        this.denominator = denominator;
        reduce();
    }

    public void setNumerator(int numerator) {
        this.numerator = numerator;
        reduce();
    }

    // Function to print output
    public void printOutput ()
    {
        System.out.println("Fraction is :"+ numerator +"/" + denominator);
    }

    private void reduce(){
        int gd = this.gcd(this.numerator, this.denominator);
        numerator = numerator / gd;
        denominator = denominator / gd;
    }

}

class Mainprogram {
    // Declare a Main Function to get inputs and call fraction class methods
    public static void main(String args[])
    {
        Scanner scn=new Scanner(System.in);
        // Code 1 to check repeated character in string
        // Take String From User
        System.out.println ( "Enter the String");
        String str = scn.next();
        // Flg is variable that will change if any same character
        boolean flag=true;
        for (int i = 0; i < str.length(); i++)
            for (int j = i + 1; j < str.length(); j++)
                if (str.charAt(i) == str.charAt(j))
                    flag=false;
        // if flag = false then string has repeated characters
        if (!flag)
            System.out.println("The String " + str + " has repeated characters");
        else
            System.out.println("The String " + str + " has no repeated characters");
        // Now from here we Code 2 of fraction class has started i.e FractionFDemo .java
        // make a object of Fraction Class
        Fraction obj = new Fraction();
        Fraction obj2 = new Fraction();
        int n,d;
        // Repeat the loop until numerator is negative
        do
        {
            // Take numerator and denominator from user
            System.out.println ( "Enter the numerator and denominator ");
            n = scn.nextInt();
            d = scn.nextInt();
            // If numerator>0 then only call class methods
            obj.get_input(n,d);
            if(n>0)
            {
                // Call methods to print the Fraction
                obj.printOutput();

                System.out.println ( "Enter the numerator and denominator ");
                n = scn.nextInt();
                d = scn.nextInt();

                obj2.get_input(n, d);
                Fraction sum = obj.add(obj2);
                // question 3
                // check equality
                System.out.println("isEqual: "+ obj.equals(obj2));
                // calculate sum
                System.out.println("Sum = ");
                sum.printOutput();
            }

        } while(!obj.isZero()); // question 2


    }
}

Step by Step Solution

3.38 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Fractionjava package comfrac To change this license header choose License Headers in Project Properties To change this template file choose Tools Templates and open the template in the editor public c... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Economics Questions!