aspose file tools*
The moose likes Beginning Java and the fly likes Fractions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Fractions " Watch "Fractions " New topic
Author

Fractions

G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
I am trying to do simple conversion of a "long" fraction to it's Prime number
G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
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

Joined: Sep 06, 2003
Posts: 259
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 ]

http://www.skillassert.com


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

Joined: Jan 01, 2007
Posts: 368
You can write something as below:



Thanks and Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Originally posted by Satya Maheshwari:
You can write something as below:

That line won't work correctly, I am afraid.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

Joined: Jan 01, 2007
Posts: 368
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fractions
 
Similar Threads
a wrong question on the examn.
java fraction class
no clue
Does OCJP provide marks for partial correct answers?
What irrational fears do you have?