Write a program in java to assign two fractional number and print their difference

So I am a beginner at programming and I'm having trouble with this assignment for my programming class. The assignment is to create a Java program using two classes that can perform arithmetic on fractions. I think my main issue is with the object-oriented concepts I need to use for this project like classes and objects. I'm still pretty confused about how constructors, accessors, mutators etc work.

The first class, Fraction.java, contains the method definitions for all the arithmetic operations as well as methods to find the greatest common denominator, to reduce the fractions, and to print the results. I have created the printFraction, reduceFraction, gcd, and addFraction methods but I have yet to try the subtract, multiply, or divide.

The second class, TestFraction.java, is designed to implement the Fraction class and test its capabilities. I have written some code in this class to test the Fraction class while I'm still working and the methods I have (print, reduce, and gcd) seem to work besides the addFraction one. It looks like it prints a memory address or something instead of the fraction I'm looking for. I have an idea why but I'm not sure how to fix it.

Here's my code so far. Like I said, I'm a beginner and I'm sure it's not very good but I'm mostly worried with finishing and getting the program to work. If you have any advice, please try to make it easy for me to understand. Here's a sample output: sample output

Fraction.java:

public class Fraction { private int numerator; private int denominator; //no-arg constructor Fraction() { numerator = 0; denominator = 1; } //constructor Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } //accessor for numerator int getNumerator() { return numerator; } //mutator for numerator void setNumerator(int num) { numerator = num; } //accessor for denominator int getDenominator() { return denominator; } //mutator for denominator void setDenominator(int denom) { denominator = denom; } //printFraction method concatenates the numerator and denominator with a "/" string between them static void printFraction(int numerator, int denominator) { System.out.print(numerator + "/" + denominator); } //reduceFraction method uses the gcd method to print a reduced version of the fraction given public static void reduceFraction(int numerator, int denominator) { int smaller; if (numerator < denominator) { smaller = numerator; } else { smaller = denominator; } for (int i = smaller; i > 0; --i) { if (numerator % i == 0 && denominator % i == 0) { System.out.print("Reduced form: " + (numerator/gcd(numerator, denominator)) + "/" + (denominator/gcd(numerator, denominator))); break; } } } //recursive method that calls itself until it reduces completely to the gcd public static int gcd(int numerator, int denominator) { if (numerator % denominator == 0) { return denominator; } return gcd(denominator, numerator % denominator); } public static Fraction addFraction(Fraction a, Fraction b) { return new Fraction((a.numerator * b.denominator + a.denominator * b.numerator), (a.denominator * b.denominator)); }

TestFraction.java(very incomplete, consists of just basic tests for now):

import java.util.Scanner; public class TestFraction { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first fraction's numerator and denominator separated by spaces: "); Fraction myFraction1 = new Fraction(input.nextInt(), input.nextInt()); System.out.print("Enter the second fraction's numerator and denominator separated by spaces: "); Fraction myFraction2 = new Fraction(input.nextInt(), input.nextInt()); Fraction.printFraction(myFraction1.getNumerator(), myFraction1.getDenominator()); System.out.println(); Fraction.printFraction(myFraction2.getNumerator(), myFraction2.getDenominator()); System.out.println(); System.out.print(Fraction.gcd(myFraction1.getNumerator(), myFraction1.getDenominator())); System.out.println(); System.out.print(Fraction.gcd(myFraction2.getNumerator(), myFraction2.getDenominator())); System.out.println(); Fraction.reduceFraction(myFraction1.getNumerator(), myFraction1.getDenominator()); System.out.println(); System.out.println(Fraction.addFraction(myFraction1, myFraction2)); } }

public class { public static void main (String []args) { double a,b,c,d; a=16.0;b=25.0; c=math.sqrt(a); d=math.sqrt(b); System.out.println("Square of a="+c); System.out.println("Square of b=+d"); } } Here is ur solution it will surely work

Given two integers representing the Numerator and Denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

Examples: 



Input: Numerator = 1, Denominator = 2 Output: "0.5" 1/2 = 0.5 with no repeating part. Input: Numerator = 50, Denominator = 22 Output: "2.(27)" 50/22 = 2.27272727... Since fractional part (27) is repeating, it is enclosed in parentheses.

Prerequisites: Recurring Sequence in a Fraction

Approach: The idea is to first calculate the integral quotient (absolute part before decimal point) and then calculate the fractional part. To check if the fractional part is repeating, insert the remainder (numerator % denominator) in a map with key as remainder and value as the index position at which this remainder occurs. If at any point of time, the remainder becomes zero, then there doesn’t exist a repeating fraction otherwise if the remainder is already found in the map, then there exists a repeating fraction.

Below is the implementation of above approach.  

string calculateFraction(int num, int den)

    int sign = (num < 0) ^ (den < 0) ? -1 : 1;

    res += to_string(initial);

    while (rem > 0 && !repeating) {

        if (mp.find(rem) != mp.end()) {

    cout << calculateFraction(num, den) << endl;

    cout << calculateFraction(num, den) << endl;

    public static String calculateFraction(int num, int den)

        StringBuilder result = new StringBuilder();

        if ((num < 0) ^ (den < 0))

        long rem = num % den * 10;

            if (m.containsKey(rem)) {

                String part1 = result.substring(0, index);

            m.put(rem, result.length());

            result.append(String.valueOf(quo));

        return result.toString();

    public static void main(String[] args)

        String resString1 = calculateFraction(num, den);

        String resString2 = calculateFraction(num, den);

        System.out.println(resString1);

        System.out.println(resString2);

def calculateFraction(num, den) :

    sign = -1 if (num < 0) ^ (den < 0) else 1

    while (rem > 0 and not repeating) :

if __name__ =="__main__":

    print(calculateFraction(num, den))

    print(calculateFraction(num, den))

function calculateFraction(num, den)

    var sign = (num < 0) ^ (den < 0) ? -1 : 1;

    var initial = parseInt(num / den);

    res.push(initial.toString());

    while (rem > 0 && !repeating) {

        var temp = parseInt(rem / den);

        res.push(temp.toString());

        res.splice(index,0, "(");

document.write( calculateFraction(num, den) + "<br>");

document.write( calculateFraction(num, den));


Article Tags :

Practice Tags :