Results 1 to 20 of 24
 05292013, 03:44 PM #1Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Fractions
So, for one of my class projects, we need to create a program that will calculate slope through the y2y1/x2x1 formula.
Here's my current code:
Java Code:import java.util.Scanner; public class slope { public static void main(String args[]){ Scanner input = new Scanner(System.in); double y1, y2, x1, x2, ya, xa; System.out.println("Enter your y1 value: "); y1 = input.nextDouble(); System.out.println("Enter your y2 value: "); y2 = input.nextDouble(); System.out.println("Enter your x1 value: "); x1 = input.nextDouble(); System.out.println("Enter your x2 value: "): x2 = input.nextDouble(); ya = y2  y1; xa = x2  x1; System.out.println("Your slope is: " + ya + "/" + xa); } }
I used to just have an m double, and its value was ya/xa. But I realized that when writing equations, people don't use slopes with decimals, they use fractions. So I removed the m double, and simply manually typed in a division bar to represent a fraction.
Java Code:System.out.println("Your slope is: " + ya + "/" + xa);
So, I wanted to add the m double back. But how can I make the m double show as a fractional result. EG: I want 2.5 to show as 5/2.
 05292013, 03:50 PM #2
Re: Fractions
You might want to rethink your approach. For example, what if x1 is 0 and x2 is pi? How do you want to represent that as a fraction?
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 03:53 PM #3Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Re: Fractions
I'm not sure what you mean.
If x1 is 0 and x2 is pi, the bottom part of the fraction would be (3.14159  0), so 3.14159.
 05292013, 03:55 PM #4
Re: Fractions
...then why do you need to represent 2.5 as 5/2?
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 03:56 PM #5Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Re: Fractions
Because it's slope. It needs to be a fraction.
 05292013, 03:59 PM #6
Re: Fractions
So why doesn't 3.14159 have to be a fraction?
You're missing my point. Not every number *can* be represented as a fraction. Your example 2.5 is nice and easy for 5/2, but many (most) other numbers will not be so clean. I think you should take a step back and really think about your actual goal here.How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 04:02 PM #7Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Re: Fractions
Every number can be a fraction.
3.79 > 379/100
456.78 > 22839/50 (45678/100 when not reduced)
Look, if you can help me great. But I didn't post here to argue...
 05292013, 04:16 PM #8
Re: Fractions
Sorry, but this is absolutely false. Recommended reading: Irrational number  Wikipedia, the free encyclopedia
You haven't mentioned any restrictions on the numbers your users are entering, so there isn't going to be a onesizefitsall solution for you, since some numbers will be absolutely impossible to reduce to a fraction. I'm not trying to argue, but the best thing I can do to help you is to help you understand the problem in the first place.How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 06:22 PM #9Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Re: Fractions
I came on here for help. Not for you to rant at me. It's required that my program output slope in FRACTIONAL form. I don't care if sometimes it doesn't work, or if the fraction isn't pretty. Now, if you aren't going to help me, please leave.
 05292013, 06:22 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,000
 Blog Entries
 7
 Rep Power
 23
 05292013, 06:34 PM #11
Re: Fractions
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 06:37 PM #12
Re: Fractions
I am trying to help you, but getting you to think about the problem more. If you're looking for somebody to just dump code in your lap, you came to the wrong place.
Let's start over: what have you tried? I suggest creating a standalone SSCCE that consists of a single method that takes a double argument and returns a String fraction. Can you get it to return the "ugly" fraction, based on your assertion that every number can be written as one? Can you reduce those ugly fractions? Which step are you stuck on? What are you confused about?How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 05292013, 07:11 PM #13Member
 Join Date
 May 2013
 Location
 New Hampshire, USA
 Posts
 5
 Rep Power
 0
Re: Fractions
Hi Kartright,
Where this is a school assignment, it wouldn't be beneficial to either of us if we just dropped a working function in your lap, so I agree with Kevin in that aspect. However, I'd be happy to help you work to that goal. As Kevin said, start by trying to write a function that simply takes a double (your slope m), and returns the fraction as a String.
Here's some additional helpful info for the math that should happen to get the top and bottom of your fraction.
My recommendation would be a loop that just takes your decimal, and multiplies it by each positive integer, starting from 1, until it get's a whole number. For an example number of 4.25, it would basically do this:
4.25 * 1 = 4.25 (not a whole number)
4.25 * 2 = 8.5 (not a whole number)
4.25 * 3 = 12.75 (not a whole number)
4.25 * 4 = 17 (GREAT! A whole number!)
The loop should stop once you find a multiplier that will make your number a whole number, so for that example, the loop would stop at 4, and save the numbers 4 and 17. That's actually your formula for getting the fraction. it would be 17/4. Remember that you can use the modulus operator (%) to see if your result is divisible by 1, and if it is, then it's a whole number.
Give it a shot, and post back here if you have any questions further!
Good luck,
Richard
 05292013, 07:59 PM #14Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,630
 Rep Power
 7
Re: Fractions
Why not write a fraction class to do your math. Maintain the numerator and the denominator as instance fields. When you divide one class instance by another
e.g. frac1.div(frac2) then you would really be multiplying by the reciprocal of one of the fractions. Adding or subtracting two fractions could be done using an LCM function to find the greatest common multiple as the denominator. The fractions could be reduced using a GCD (greatest common divisor function). Actually for two numbers A and B
A x B = LCM(A,B) x GCD(A,B)
You could even use your toString() method to display in fractional form.
It doesn't have to be elaborate.
Edit: One more thing. You can also maintain a decimal equivalent. Using some basic number theory you can determine the nonrepeating and the repeating part and length of the decimal expansion as long as you have the numerator and denominator.
Regards,
JimLast edited by jim829; 05292013 at 08:02 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05292013, 08:53 PM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,000
 Blog Entries
 7
 Rep Power
 23
Re: Fractions
That is too naive (read: too slow); better use the Double.doubleToLongBits( ... ) method so that you have a numerator and a denominator in one step. The denominator is a power of two, so the gcd( ... ) of the two numbers is easy to find. The result fits in two long numbers if the exponent of the orignal number wasn't too large; otherwise BigIntegers will do the job.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 05292013, 09:13 PM #16Member
 Join Date
 May 2013
 Location
 New Hampshire, USA
 Posts
 5
 Rep Power
 0
Re: Fractions
Jos,
While I personally agree, I gave the method the way I did based on what I expect he has knowledge of. Looking at the source containing only very basic language, I assume he's not familiar with BitIntegers. I'd also assume that the purpose of the exercise is to practice a fundamental concept he's just learned, such as basic logic or arithmetic in Java, so I wanted to leave it at that.
 05292013, 09:24 PM #17Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
Re: Fractions
Narrow my requirements? I need a fraction. Can't possibly narrow it down anymore.
Also, I don't need you to help me think more about the problem. It's my 7th assignment in the class, it's as basic as can be. My teacher encourages me to think just fine.
So, in the future Kevin, avoid my topics.
Also, I wasn't looking for code to be dumped on my lap. I was looking for an approach, and Richard gave one to me. Thanks Richard, I figured it out.
 05292013, 09:29 PM #18Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,630
 Rep Power
 7
Re: Fractions
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05292013, 09:34 PM #19Member
 Join Date
 May 2013
 Posts
 20
 Rep Power
 0
 05302013, 07:59 AM #20
Re: Fractions
Get in the habit of using standard Java naming conventions!
Similar Threads

How to reduce fractions in Java?
By uselessparadigm in forum New To JavaReplies: 9Last Post: 04092012, 08:02 PM 
trouble using fractions
By jeanOliver in forum New To JavaReplies: 2Last Post: 09062011, 08:48 PM 
Reducing fractions
By SwEeTAcTioN in forum New To JavaReplies: 10Last Post: 01132010, 05:51 PM 
NumberFormat which leave fractions as they are
By itaipee in forum AWT / SwingReplies: 2Last Post: 12092009, 07:11 PM 
How do I reduce Fractions in this program?
By Popedreadlock in forum New To JavaReplies: 8Last Post: 12082008, 01:28 AM
Bookmarks