Jump to content

Fraction help

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
I've made two classes for a java book I'm working in, my main class and my Fraction class. The class' function is to perform mathematical operations on fractions, such as add, subtract, multiply, divide.

All the methods work perfect, except the 'add' method. It always returns a value with +1 in the numerator. For example, if the answer is 23/30, it will return 24/30. Can anyone help me figure out what I did wrong?

import java.util.*;


public class Main {

	public static void main(String[] args){

		Scanner input = new Scanner(System.in);

		String num1, denom1, num2, denom2;

		System.out.println("Enter first fraction: ");

		String userFraction = input.nextLine();

		num1 = userFraction.substring(0, 1);

		denom1 = userFraction.substring(2);

		System.out.println("Enter second fraction: ");

		userFraction = input.nextLine();

		num2 = userFraction.substring(0, 1);

		denom2 = userFraction.substring(2);

		

		Fraction f1 = new Fraction(num1, denom1);

		Fraction f2 = new Fraction(num2, denom2);

		System.out.println((f1.add(f2)));

		System.out.println((f1.subtract(f2)));

		System.out.println((f1.multiply(f2)));

		System.out.println((f1.divide(f2)));

	}

}


public class Fraction {

	private int numerator, denominator;

	

	public Fraction(String num, String denom){

		numerator = Integer.parseInt(num);

		denominator = Integer.parseInt(denom);

	}

	public int getNumerator(){

		return numerator;

	}

	public int getDenominator(){

		return denominator;

	}

	public void setNumerator(int num){

		numerator = num;

	}

	public void setDenominator(int denom){

		denominator = denom;

	}

	public String toString(){

		return numerator + "/" + denominator;

	}

	public Fraction add(Fraction f){

		int num = numerator * f.getDenominator() +

				f.getNumerator() + denominator;

		int denom = denominator * f.getDenominator();

		Fraction result = new Fraction(num + "", denom + "");

		return result;

	}

	public Fraction subtract(Fraction f){

		int num = numerator * f.getDenominator() -

				f.getNumerator() * denominator;

		int denom = denominator * f.getDenominator();

		Fraction result = new Fraction(num + "", denom + "");

		return result;

	}

	public Fraction multiply(Fraction f){

		int num = numerator * f.getNumerator();

		int denom = denominator * f.getDenominator();

		Fraction result = new Fraction(num + "", denom + "");

		return result;

	}

	public Fraction divide(Fraction f){

		int num = numerator * f.getDenominator();

		int denom = denominator * f.getNumerator();

		Fraction result = new Fraction(num + "", denom + "");

		return result;

	}

}

thank you guys in advance :)

edit: problem solved - thanks Wim DC :)

Edited by Cruel Hand, 13 December 2011 - 04:47 AM.
problem solved


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Shouldn't this be "*" ?

int num = numerator * f.getDenominator() +

				f.getNumerator() [COLOR="#FF8C00"][SIZE=5]+[/SIZE][/COLOR] denominator;



#3
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET

wim DC said:

Shouldn't this be "*" ?

int num = numerator * f.getDenominator() +

				f.getNumerator() [COLOR="#FF8C00"][SIZE=5]+[/SIZE][/COLOR] denominator;


well, which method are you talking about? If you add two fractions, then a/b + c/d becomes (a(d) + b©)/bd. subtracting is (a(d) - b©)/bd. a/b * c/d = a©/b(d) and division is a/b * d/c which becomes a(d)/b©.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

then a/b + c/d becomes (a(d) + b©)/bd.
Then why do you do (a(d) + b +c)/bd
Read my previous post again, but this time note the bigger orange +

#5
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
that was the problem!! Thank you :D I can't believe I didn't notice that hahaha




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users