Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fractions

 
G. Graz
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to do simple conversion of a "long" fraction to it's Prime number
 
G. Graz
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry hit the wrong button ! Ok, so if a fraction is 1/2 then it's good , if the fraction is 3/2 , the method will convert it to 1-1/2. Here is my code:



my "else if" is causing me all the problems ( convert 3/2 to 1-1/2). Any suggestions what I am doing wrong ? I am getting incompatible types when I try to compile, but I though my toString was taking care of that ? Thank you for the insight !! Suggestions always welcome !! Thank you!

Greg
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the else block you can either use
result = Double.toString((a/b));
or
result = ""+(a/b);

Note: a/b is primitive double which does not have a toString method.
[ May 15, 2008: Message edited by: karthi keyan ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing out a / b and a % b. That's all you need to do. You may end up with � coming out as 0 1/2, and ou will get peculiar results with negative numbers, but you can work out yourself how to deal with that!
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write something as below:

 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satya Maheshwari:
You can write something as below:

That line won't work correctly, I am afraid.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
That line won't work correctly, I am afraid.


Thanks for correcting. Very naive on my part . It should be
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work, but I think this is better

. . . denominator < 0 ^ numerator < 0 . . .

Work it out. And you ought to throw an Exception if denominator is 0, before reaching that stage.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
That will work, but I think this is better

. . . denominator < 0 ^ numerator < 0 . . .

Work it out. And you ought to throw an Exception if denominator is 0, before reaching that stage.


Yes I agree 'denominator is 0' case should be handled.
[ May 19, 2008: Message edited by: Satya Maheshwari ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic