fraction math

Page 1 of 1

4 Replies - 929 Views - Last Post: 03 February 2013 - 04:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310955&amp;s=bc6a69934242c79260b9744246be7525&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jwhiteknight

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 09-January 13

fraction math

Posted 02 February 2013 - 06:15 PM

I am writing the code that will add, subtract, multiply, and divide fractions.
I am having trouble getting my methods correct. The user enter the numbers for the fractions
num1 and num2
den1 and den2

then the user chooses from a menu.. to apply the function to the fractions.
The errors are in the print statements and the return statement from each method.

```
import java.util.Scanner;

public class Fractions
{

public static void main (String arg[])
{
System.out.println ("This program will do basic math with fractions\n");
Scanner input = new Scanner (System.in);

int num1=0;
int dem1=0;
int num2=0;
int den2=0;
int choice;

System.out.printf ("please enter the numerator for the first fraction\n");
num1 = input.nextInt();

System.out.print("please enter the demoninator for the first fraction\n");
dem1 =input.nextInt();

System.out.print("Please enter the numerator for the second fraction\n");
num2 = input.nextInt();

System.out.print ("please enter the demoninator for the Second fraction\n");
dem2 = input.nextInt();

System.out.print(" choose what you would like to do to the fractons\n");
System.out.println("2 subtract fraction\n");
System.out.println("3 divid fraction\n");
System.out.print("4 multiply fraction\n");
choice =input.nextInt();

switch(choice)
{
case 1:
System.out.printf ("the fractions add to gether are %d , plus", addfract (int num1,int dem1, int num2, int dem2):

break;
case 2:
System.out.printf ("the fractions subtracted to gether are", subfract (int num1,int dem1,int num2 int dem2,);
break;

case 3:
System.out.printf ("the fractions muliplied to gether are", timesfract (int num1,int dem1,int num2),

break;

case 4:
System.out.printf("the fractions devided togeter are",dividefract( int num1, int den1,int num2,int den2))
}
}
public static int addfract (int num1,int den1,int num2, int den2)
{
int f1; // answer fraction num
int f2; // answer fraction dem
int x = num1 * den2; // first fraction
int y = den1 * den2;  // first fraction dem
int f = num2 * den1; // second fraction num
int a = den2 * den1; // second fraction dem
f1 = x + y; // adding fraction
f2 = f + a; // keeping the same den
return f1, f2;
}
public static int subfract(int num1,int den1,int num2,int den2)
{
int f1; // answer fraction num
int f2; // answer fraction den
int x = num1 * den2;// first fraction num
int y = den1 * den2;// first fraction den
int f = num2 * den1;// second fraction num
int a = den2 * den1;// second fraction den
f1 = x - y; // subtracting fraction
f1 = f + a; // keeping the same den

return f1, f2;
}
public static int timesfract(int num1,int den1,int num2,int den2)
{
int tnum;
int tdem;
tnum = num1 *num2;
tden = dem1 * den2;

return tnum tden;
}

public static int dividefract(int num1,int dem1,int num2,int den2)
{

int tnum = num1 * den2;
int tdem = dem1 * den2;
return tnum tden;
}
}

```

Is This A Good Question/Topic? 0

Replies To: fraction math

#2 lbfb

• New D.I.C Head

Reputation: 6
• Posts: 31
• Joined: 23-February 12

Re: fraction math

Posted 02 February 2013 - 06:31 PM

without looking at your code I can see that you called the fourth variable den2 when you originally declared it, but afterwards it is called dem2.

#3 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

Re: fraction math

Posted 02 February 2013 - 06:41 PM

Let's only take case 4 as a sample

```case 4:

System.out.printf("the fractions devided togeter are",dividefract( num1,den1,num2,den2))
```

Just remove int modifiers as you are calling static functions.

You just need to convey numbers without determining types.

Type determination is the method's job !

#4 lbfb

• New D.I.C Head

Reputation: 6
• Posts: 31
• Joined: 23-February 12

Re: fraction math

Posted 02 February 2013 - 07:12 PM

I have had a quick look at the code now, but I am not sure that I actually understand what you are trying to do.

In your four methods for adding, subtracting . . . the fractions, you try to return two values. I am not 100% sure here, but I don't think that you can return two values in Java (unless you use an array, or a Pair Object). But I don't see why you don't just complete the calculation in the method and return a single value anyway. I would have thought that the addFraction method would look something like this:
```public static int addFraction (int num1, int den1, int num2, int den2) {
int result = (num1*den2/den1*den2) + (num2*den1/den2*den1);
return result;
}

```

My maths could be wrong, but I think that would give you what you need.

oops sorry Burakaltr - I didn't mean to jump in on your answer.

And jwhiteknight, I just realised that result shouldn't be an integer in my code snippet.

#5 baavgai

• Dreaming Coder

Reputation: 6605
• Posts: 13,946
• Joined: 16-October 07

Re: fraction math

Posted 03 February 2013 - 04:45 AM

This will never work.

```return f1, f2;
```

You're actually returning only one of those values. It's a syntax quirk that this would even compile. ( Haven't tried it, might not. )

This is a perfect place to use a class. Indeed, that's possibly the point of your exercise.

```class Fraction {
public final int num, dem;
public Fraction(int num, int dem) { this.num = num; this.dem = dem; }
public Fraction add(Fraction other) { /* your code here */ }
//...
public String toString() { /* your code here */ }
}

```

Your menu code should be as simple as:
```System.out.printlne("the fractions add to gether are" + first.add(second)):

```

Also, be consistent with your indenting.

This post has been edited by baavgai: 03 February 2013 - 04:45 AM