peter cech
peter cech

Reputation: 53

How to create a program that adds subtracts devides and multiplies 2 fractions?

first i would like to start saying that i am new to programing and i don t know much. with that said i would appreciate if anyone could help me with my program that it is supposed to read 2 fractions and an operator for example "2/3 + 4/5". i have some of the code done but it still give me an error when i run it here is what i have so far:

public class Fraction {

    private static int numer;
    private static int denom;

     public Fraction(int num, int den)
        {
            numer = num;
            denom = den;
            simplify();
        }


    int findGcd(int a, int b)
        { 
        int temp;
            while(b != 0)
            {
                    temp = b;
                    b = a % b;
                    a = temp;
            }
        return a;
    }


    void simplify()
        {
            int gcd = findGcd(numer, denom);
            numer /= gcd;
            denom /= gcd;
        }

    public int getNumer(){
        return numer;
    }

    public int getDenom(){
        return denom;
    }



    Fraction add(Fraction x) {
        Fraction result;

        if (x.getDenom()== getDenom()) {
            result = new Fraction(x.getNumer() + getNumer(), denom);
        } else {
            denom = this.getDenom() * x.getDenom();
            numer = this.getNumer() * x.getDenom() + x.getNumer() * this.getDenom();
            return new Fraction(numer,denom);


        }
        return result;
    }

    public String toString(){
          return (Integer.toString(numer) + "/" +
                     Integer.toString(denom));
    }



    public static void main (String []args){
            Fraction a = new Fraction(1,3);
            Fraction b = new Fraction(4,5);
            System.out.println(a.toString());
            System.out.println(b.toString());
    }
}

thank you for your help i really appreciate it.

Upvotes: 2

Views: 672

Answers (2)

arshajii
arshajii

Reputation: 129497

Why are you making your fields static? static fields belong to the class as opposed to each instantiation (not what you want here). Try removing the static keyword.

On another note, you mentioned that you'd like to read input from the user. You might want to look into using a Scanner for this (in case you don't already know about this handy class).

Once you read the input, something like 2/3 + 4/5, you can split your string using a space as your delimiter. Now, you can parse a fraction from the first (2/3) and third (4/5) elements of the newly formed string array, and perform the operation that corresponds to the second element of the array (+).

Upvotes: 3

Rohit Jain
Rohit Jain

Reputation: 213223

There is a difference between static variable and instance variable.

  • Static variable is a class variable which is common for all instances.. So, if you change these variables for one instance, it will be changed for all the instances.
  • Instance variable, on the other hand, are specific to each instance.. They are binded to an instance of a class.

That being said.. You need to modify your code a little bit..

Change your static variables in your class to instance variables..

private static int numer;
private static int denom;

The above two variables should be instance variables.. So that they are unique for each instance you create for your class..

So, change them to: -

private int numer;
private int denom;

And for reading user input, A.R.S has already given you link to a valuable class..

Upvotes: 0

Related Questions