Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By harshit shah

Thread: Writing a program that adds 2 rational numbers (in the form of fractions) in BlueJ.

  1. #1
    dangolf is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Writing a program that adds 2 rational numbers (in the form of fractions) in BlueJ.

    I have an assignment whose instructions are as follows. ( I pasted the code that I already have. My second week taking an intro to Java class and this is totally throwing me in the deep-end. Any input is appreciated).

    1.) Rational class represents a rational number, which is a number that has two integer parts: a numerator and a denominator. A rational number expresses the ratio of the numerator to the denominator. For example, the rational number 1/2 expresses the ratio of 1 to 2, while the rational number 3/2 expresses the ration of 3 to 2. You can think of these as improper fractions, but you will need to represent them as two numbers (the numerator and the denominator), rather than as a decimal number.
    Think about what attributes a Rational has. What instance variables will you need to declare?
    You will make one (1) constructor for the Rational class that takes no parameters and creates a Rational representing 0/1 (0/0 would be illegal!). You should then use getters and setters to actually change the numerator and denominator of that Rational.
    A Rational will need a toString() method so that you can print it out. Remember, a toString() method does not actually print anything, it just returns a String that can be printed elsewhere!
    You'll need to make a getter and setter method for each of the Rational's instance variables.
    A Rational will also need an add() method. This method should take in another Rational as an argument, and return a new Rational as the result. For example, if I make a Rational object that represents 3/4 and another object that represents 1/2, I should be able to add them together to produce a third object that represents 5/4. This way I can get the result, but still have my 3/4 and 1/2 objcts to do more math with!
    Remember how parameters are just variables, and variables can be Object types? The same thing is happening here: the "variable" of our parameter will be a Rational-shaped box, and we will put inside it a Rational object.
    Similarly, we can make a new Rational object (using the constructor and appropriate setters) and then return that object, the exact same way we would return an int or a String.
    We can add rational numbers by finding a common denominator, multiplying by the appropriate value, and then adding the numerators. You do NOT need to find the lowest common denominator--it is fine and expected to have non-reduced fractions!

    Finally, a Rational will need a toMixed() method that returns a new Mixed object that represents the same value. This will let you easily switch between Rationals and Mixeds.

    In order to figure out the whole number part, you can use integer division (or the Math.floor() function)--this will literally give you the whole number part of the resulting quotient.
    In order to figure out the remaings of the whole-number division (that will become the new numerator), you can use the modulo operator (%) to literally get the remainder of the division! If you have questions on this, please ask!

    2.)Mixed class represents a mixed number: the combination of a whole number and a proper fraction--a rational number where the numerator is smaller than the denominator. For example, the rational number 3/2 can be expressed as the mixed number 1 1/2, while the rational number 1/2 can be expressed as the mixed number 0 1/2.
    Think about what attributes a Mixed has. What instance variables will you need to declare?
    You will make at least one (1) constructor for the Mixed class, which takes integers for the whole part, numerator, and denominator as parameters.

    A Mixed will need a toString() method so that you can print it out.
    You do not need to make a getter and setter method for each of the Mixed's instance variables.
    You do not need to make an add() method for the Mixed (though you might think about how you would--it is not as easy as you think!)
    Finally, the Mixed class will need a toRational() method that returns a new Rational object that represents the same value. This will let you easily switch between Mixeds and Rationals.

    3.)RationalAdder class asks the user for two Rationals to add, then adds them up and prints out both the Rational and Mixed versions of the sum.
    This class will look the programs we've been making so far, with just a main() method rather than constructors and instance variables.
    This class should prompt the user to enter two rational numbers, using the Scanner to read what they type. You can have the user type in one part at a time (e.g., the first numerator, first denominator, second numerator, second denominator). See Extensions below for other options.
    Using the provided values, instantiate two Rational objects, then add them together to produce a third Rational object that is the sum.
    Print out the sum both as a Rational, and after you have converted it to a Mixed object.
    You can use the provided tester class to help check that each piece of your program works. Simply uncomment the section you wish to test after you add a feature. You can also use the BlueJ interface: for example, instantiate a Rational object and then call it's methods by right-clicking on the red box (you can also use the "inspect" option to look at the current values of the instance variables).

    For rational class:

    import java.util.*;

    public class Rational
    {
    private int numerator;
    private int denominator;


    public Rational(){
    numerator = 0
    denominator = 1
    }


    public int getRational(){
    return numerator/denominator

    }
    //Multiplies the two denominators to get a common denominator
    public int getCommonDenominator(){
    return den1*den2;
    }

    public int getNumerator(){
    int num1;
    int num2
    numereator = num1 + num2;
    return
    }

    //Adds the two denominators and multiplies them by the common denominator
    public int getDenominator(){
    int den;
    den = denominator1 + denominator2;
    return getCommonDenominator() * den;
    }


    //The following four methods simply set the input from the user to variables of this class
    public void setNumerator(int toNumerator1){
    numerator1 = toNumerator1;
    }

    public void setDenominator1(int toDenominator1){
    denominator1 = toDenominator1;
    }

    //Sends the added numerator's and denominator's of the two fractions to RationalAdder to be printed
    public String toString(){
    return getNumerator() + "/" + getDenominator();
    }

    }


    For the RationalAdder class:

    import java.util.*;

    public class RationalAdder
    {
    private Rational rational;

    public static void main(String[] args)
    {
    //Allows the program to continue on to using the getInput method and displayOutputMethod
    RationalAdder rational = new RationalAdder();
    rational.start();
    }

    //Allows getInput and displayOutput to be executed
    public void start(){
    getInput();
    displayOutput();
    }

    //User puts in input
    private void getInput(){

    int numerator1;
    int denominator1;
    int numerator2;
    int denominator2;

    Scanner scanner = new Scanner(System.in);

    System.out.print("First numerator: ");
    numerator1 = scanner.nextInt();
    System.out.print("First denominator: ");
    denominator1 = scanner.nextInt();
    System.out.print("Second numerator: ");
    numerator2 = scanner.nextInt();
    System.out.print("Second denominator: ");
    denominator2 = scanner.nextInt();

    rational = new Rational(numerator1, denominator1, numerator2, denominator2);
    }

    //Final information is displayed
    private void displayOutput(){
    System.out.println(rational.toString());
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing a program that adds 2 rational numbers (in the form of fractions) in Blue

    You'll get better answers if you ask specific questions.

  3. #3
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Writing a program that adds 2 rational numbers (in the form of fractions) in Blue

    please go through forum rules and add code tags link
    lenois likes this.

Similar Threads

  1. BlueJ program
    By ukgate13 in forum New To Java
    Replies: 1
    Last Post: 11-23-2012, 06:12 PM
  2. trying to understand this rational numbers class
    By sonny in forum New To Java
    Replies: 9
    Last Post: 03-28-2010, 11:29 PM
  3. Rational numbers - problem with comparsions
    By omygoodness in forum New To Java
    Replies: 9
    Last Post: 01-03-2010, 09:20 PM
  4. How do I reduce Fractions in this program?
    By Popedreadlock in forum New To Java
    Replies: 8
    Last Post: 12-08-2008, 01: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
  •