Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree3Likes

Thread: Fractions

  1. #1
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Fractions

    So, for one of my class projects, we need to create a program that will calculate slope through the y2-y1/x2-x1 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);
    	}
    }
    Now, here's my problem:
    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);
    But then I also realized that there will be times when your x or y values may be decimals or fractions. And I can't very well have a slope that's ((5/2)/(3/2)) or 3.8/4.9.
    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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default 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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default Re: Fractions

    ...then why do you need to represent 2.5 as 5/2?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Fractions

    Because it's slope. It needs to be a fraction.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default Re: Fractions

    Quote Originally Posted by Kartright View Post
    Because it's slope. It needs to be a fraction.
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default 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...

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default Re: Fractions

    Quote Originally Posted by Kartright View Post
    Every number can be a fraction.
    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 one-size-fits-all 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default 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.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,997
    Blog Entries
    7
    Rep Power
    19

    Default Re: Fractions

    Quote Originally Posted by KevinWorkman View Post
    Sorry, but this is absolutely false. Recommended reading: Irrational number - Wikipedia, the free encyclopedia
    Not that it matters much here, but I beg to differ: every number in the IEEE/754 set is an element of the set Q (rational numbers).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default Re: Fractions

    Quote Originally Posted by JosAH View Post
    Not that it matters much here, but I beg to differ: every number in the IEEE/754 set is an element of the set Q (rational numbers).

    kind regards,

    Jos
    Fair enough. And realistically, a user isn't going to enter an infinite number of decimals- unless operations like square roots are allowed in the input. I'm probably being a bit pedantic, but my point is that the OP needs to narrow his requirements a bit.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,688
    Rep Power
    8

    Default Re: Fractions

    Quote Originally Posted by Kartright View Post
    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.
    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Join Date
    May 2013
    Location
    New Hampshire, USA
    Posts
    5
    Rep Power
    0

    Default 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

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default 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 non-repeating and the repeating part and length of the decimal expansion as long as you have the numerator and denominator.

    Regards,
    Jim
    Last edited by jim829; 05-29-2013 at 08:02 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,997
    Blog Entries
    7
    Rep Power
    19

    Default Re: Fractions

    Quote Originally Posted by RichardPressler View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Join Date
    May 2013
    Location
    New Hampshire, USA
    Posts
    5
    Rep Power
    0

    Default 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.

  17. #17
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Fractions

    Quote Originally Posted by KevinWorkman View Post
    Fair enough. And realistically, a user isn't going to enter an infinite number of decimals- unless operations like square roots are allowed in the input. I'm probably being a bit pedantic, but my point is that the OP needs to narrow his requirements a bit.
    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.

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Fractions

    Quote Originally Posted by Kartright View Post
    So, in the future Kevin, avoid my topics.
    Your loss! With that attitude, a lot more folks may be avoiding your topics.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Fractions

    Quote Originally Posted by jim829 View Post
    Your loss! With that attitude, a lot more folks may be avoiding your topics.

    Regards,
    Jim
    Thanks for the advice Jim. But I simply dislike getting ranted at over a simple request for help.

  20. #20
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

Page 1 of 2 12 LastLast

Similar Threads

  1. How to reduce fractions in Java?
    By uselessparadigm in forum New To Java
    Replies: 9
    Last Post: 04-09-2012, 08:02 PM
  2. trouble using fractions
    By jeanOliver in forum New To Java
    Replies: 2
    Last Post: 09-06-2011, 08:48 PM
  3. Reducing fractions
    By SwEeTAcTioN in forum New To Java
    Replies: 10
    Last Post: 01-13-2010, 04:51 PM
  4. NumberFormat which leave fractions as they are
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2009, 06:11 PM
  5. How do I reduce Fractions in this program?
    By Popedreadlock in forum New To Java
    Replies: 8
    Last Post: 12-08-2008, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •