Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Using a method to add two classes and produce a third

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Using a method to add two classes and produce a third

    Hey community i was assigned a home work asking me to create a method to add two instances of a fraction object and have the output be a new instance .


    here is the prompt i was given

    "Provide a method add that receives a second fraction as an argument, and returns a new fraction which is the sum of fractions. Example: Fraction r3 = r1.add( r2 );"

    could someone please explain how this is done ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using a method to add two classes and produce a third

    Do you know how to create an instance of a class? Hint: Use the new statement.
    Take values from the current class and the passed argument, create an instance of the class with those values and return it.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shamman84 (February 5th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using a method to add two classes and produce a third

    Thanks for the response Norm , i know how to create the instances im just having trouble understanding how my method is taking the values from both classes and producing a new instance with the new values .

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using a method to add two classes and produce a third

    Take it one step at a time.
    Where are the values located?
    One is local,
    the other is in the passed object. You'll need to call a method to get it.
    How are the two values added together to get a result?
    How do you use that resultant value to create the new instance?

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using a method to add two classes and produce a third

    What is my method returning can it return a new instance of the class if so what is the syntax ? public ___ add(Fraction a)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using a method to add two classes and produce a third

    Look at how the call is made:
    Fraction r3 = r1.add( r2 );"
    What data type is r3?
    That is what the add method must return.

  8. The Following User Says Thank You to Norm For This Useful Post:

    shamman84 (February 5th, 2012)

  9. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using a method to add two classes and produce a third

    Norm you are BRILLIANT thankyou for all of your help here is the code do you have any further pointers ?

    public Fraction add(Fraction f)
    {
    int n,d;
    if(denominator == f.getDenominator())
    {
    n = numerator + f.getNumerator();
    d = denominator ;
    return new Fraction (n,d);
    }
    else {
    d = denominator*f.getDenominator() ;
    n = ((numerator*f.getDenominator())+(f.getNumerator()* denominator));
    return new Fraction(n,d);
    }
    Last edited by shamman84; February 5th, 2012 at 09:21 PM.

Similar Threads

  1. Replies: 7
    Last Post: November 18th, 2011, 02:47 PM
  2. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  3. Improving the code to produce the same program
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 13
    Last Post: April 8th, 2011, 09:41 PM
  4. [SOLVED] Listen to two JRadioButton + ons JButton to produce output
    By voltaire in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2010, 03:46 PM
  5. Jigloo help to produce a text area which only scrolls down
    By rtumatt in forum AWT / Java Swing
    Replies: 0
    Last Post: February 2nd, 2010, 06:09 PM