Adding and subtracting rational numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lmacayb
    New Member
    • Mar 2010
    • 1

    Adding and subtracting rational numbers

    Hello I am having trouble getting strated with my program. I am trying to write a java program that adds and subtracts rational numbers. Can anyone help? This is what I have so far:


    Code:
    import java.util.Scanner;
    public class Rational
    {
    priviate int numerator;
    priviate int denominator;
    public Rational()
    {
    	numerator=0;
    	denominator=1;
    }
    public Rational(int num,int dem)
    {
    	num=num;
    	dem=dem;
    }
    
    public void setNumerator(int num)
    {
    	num=num;
    }
    public void setDenominator(int dem)
    {
    	dem=dem;
    }
    public void getNumerator()
    {
    	return num;
    }
    public void getDenominator()
    {
    	return dem;
    }
    public static Add
    {
    }
    public static Subtract
    {
    }
    }
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    That's a good start in that you have outlined what needs to be done!

    You need to write a main to test this class. It should at least create a new rational, then get and print the numerator and denominator.

    But, do your constructor/getters/setters really do any thing? Think about where they need to get/set the data from/to.

    Now, how do you add/subtract fractions? Write down the steps that you would do manually for say 1/2 + 2/3 and then compose the code.

    HTH --Sam

    Comment

    • pbrockway2
      Recognized Expert New Member
      • Nov 2007
      • 151

      #3
      I would suggest you compile your code. (every time you make a change.) The compiler messages are very useful.

      If there is a compiler message you cannot understand post the exact and entire thing and indicate which line of your code it refers to.

      Comment

      Working...