Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fraction class in java

I created a fraction Java class but it gives wrong results. Sometimes it gives good answers and sometimes bad or maybe it doesn’t give any results just random digits? Any help or hints are welcome

import java.util.Scanner;

public class Ulamek{
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        System.out.println("Podaj licznik i mianownik pierwszego ulamka: ");
        Fraction ulamek1 = new Fraction(input.nextInt(), input.nextInt());
        System.out.println("Podaj licznik i mianownik drugiego ulamka: ");
        Fraction ulamek2 = new Fraction(input.nextInt(), input.nextInt());
        System.out.println("Jaka operacje chcesz wykonac? ( +, -)");
        char c = input.next().charAt(0);
        System.out.print(ulamek1);
        System.out.print(" " + c + " ");
        System.out.println(ulamek2);
        switch(c){
            case '-':
            System.out.println(ulamek1.sub(ulamek2));
            break;
            case '+':
            System.out.println(ulamek1.sum(ulamek2));
            break;
        }
    }
}

class Fraction{
    private int numerator;
    private int denominator;

    public Fraction(int numerator, int denominator){
        if(denominator < 0){
           this.numerator = -numerator;
           this.denominator = -denominator;
        }else{
              this.numerator = numerator;
              this.denominator = denominator;
        }
    }

    public Fraction sum(Fraction other){
        int nww =  nww(other.denominator, this.denominator);
        int newNumerator = nww/this.denominator*this.numerator + nww/other.denominator*numerator;
        return new Fraction(newNumerator, nww).reduce();
    }

    public Fraction sub(Fraction other){
        int nww = nww(this.denominator, other.denominator);
        int newNumerator = nww/this.denominator*this.numerator - nww/other.denominator*other.numerator;
        return new Fraction(newNumerator, nww).reduce();
    }

    public Fraction reduce(){
        int nwd = nwd(this.numerator, this.denominator);
        return new Fraction(this.numerator/nwd, this.denominator/nwd);
    }

    private int nwd(int a, int b){
        if(a < 0){
            a = -a;
        }
        if(b < 0){
            b = -b;
        }
       while(b != 0){
          int temp = b;
          b = a % b;
          a = temp;
       }
       return a;
    }

    private int nww(int a, int b){
        return a*b/nwd(a, b);
    }

    public String toString(){
        return numerator + "/" + denominator;
    }

}

I can’t find a mistake, could anyone help me find it? I have just started learning Java so I am very beginner.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A typo. You left out the other. on this line

int newNumerator = nww/this.denominator*this.numerator + nww/other.denominator*numerator;

It should read

int newNumerator = nww/this.denominator*this.numerator + nww/other.denominator*other.numerator;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading