Find Us On:

# Java Help me solve me this

dragonzack

## Java Fraction Solution

Seeker

Java source code is attached Attachments
Fraction.zip (1 K)
Preview
System.out.println("First denominator: " + f1.getDenominator()); System.out.println("Second numerator: " + f2.getNumerator()); System.out.println("Second denominator: " + f2.getDenominator()); f1.display(); f2.display(); System.out.println("Reduced: " + f1.getReducedNum() + " / " + f1.getReducedDenom()); System.out.println("Reduced: " + f2.getReducedNum() + " / " + f2.getReducedDenom()); Fraction addedFraction = f1.add(f2); System.out.println("Sum

Preview
private void reduce(){ int lcm = findGCD(); this.reducedNum = this.numerator / lcm; this.reducedDenom = this.denominator / lcm; } public void display(){ System.out.println("Fraction: " + this.numerator + " / " + this.denominator); } public Fraction add(Fraction frac){ // multiply both

Screenshots

## Fraction source code in Java

SW_wizard

Implemented as per the requirement in two versions:

One version does not handle negative numbers
Second version handles negative numbers appropriately

Explanation as to why second version will work for negative numbers as opposed to first version is also available in the attached zip

Attachments
FractionsProject.zip (3 K)
Preview
account, and hence gcd calculation will not work correctly for project: Fractions_NegativeNumHandled negative numbers (either numerator or denominator or

Preview
simpler form denom = denom/gcd; //divide by gcd to get simpler form } private int findGCD(){ int a = Math.abs(num); //removing minus sign if any - only for gcd calculation purpose int b = Math.abs(denom); //removing minus sign if any - only for

Preview
do{ System.out.println("First denominator"); b = scan.nextInt(); }while(b==0); Fraction f1 = new Fraction(a,b); System.out.println("Second numerator"); a = scan.nextInt(); do{ System.out.println("Second denominator"); b = scan.nextInt();

Preview
numerator, int denominator){ num = numerator; denom = denominator; reduce(); //fraction reduced to its simplest form } private void reduce(){ int gcd = findGCD(); num = num/gcd; //divide by gcd to get simpler form denom = denom/gcd; //divide

Preview
new Scanner(System.in); int a,b; System.out.println("First numerator"); a = scan.nextInt(); do{ System.out.println("First denominator"); b = scan.nextInt(); }while(b==0); Fraction f1 = new Fraction(a,b); System.out.println("Second numerator"); a

Screenshots

## Fraction

mariobeka

I have implemented the class Fraction, based on what you were saying.
It handles any type of Integer Value (Negative + non Negative)
Attachments
Fraction.java (2 K) Preview
} public static void main (String[] args) throws NumberFormatException, IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Fraction f1, f2, sum; System.out.print("First numerator: "); int num_1 = Integer.parseInt(input.readLine()); System.out.print("First denumerator: "); int denom_1 = Integer.parseInt(input.readLine()); while (denom_1 == 0) { System.out.print("Denominator zero! It should be greater then or

Screenshots