Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [URGENT Test] Desperately Confused, Instance Methods Invocation?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kenster102.5




PostPosted: Thu Nov 19, 2009 3:48 am   Post subject: [URGENT Test] Desperately Confused, Instance Methods Invocation?

I used to be really good a Instance methods, now I am starting to really get confused. I have a re-test tomorrow on the writing a code for an instance method.
I am really confused with explicit and implicit, and how implicit uses the reserved word "this.num" or "this.den". If someone can answer this dilemma before lets say 8:15 then I will be okay.

Okay here is the question

Complete the definition of the following instance method for the Fraction class

Java:


public Fraction plus (Fraction f)


The method should return a Fraction object whose value is the sum of the implicit object parameter and the explicit parameter, f. The method should leave both its implicit and explicit parameters unchanged.

Java:

import java.awt.*;

public class FractionTest
{
  public static void main (String[]args)
  {
   
//I don't understand how to declare my implicit method if it is to be created as this.num and this.den


   Fraction f = new Fraction();
   
    f.num = 3;
    f.den = 4;
    Fraction g = new Fraction();
    g = this.plus(f);
    System.out.println(g.num, g.den); //I am really not sure about this anymore
  }
  class Fraction()
  {
    int num;
    int den;
   
    public Fraction plus (Fraction f)
    {
   
    return(f.num + this.num, f.den + this.den); 
    }
  }
}



I really really feel incompetent if I can't understand how a method using "this." can be invoked, I am really trying hard to understand. So please someone help me.
Thank you
Ken
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Thu Nov 19, 2009 8:15 am   Post subject: RE:[URGENT Test] Desperately Confused, Instance Methods Invocation?

'this' is a reference to itself, so if you do someInstance.plus(), when the function runs 'this' (inside the function) will be a reference to someInstance.

Btw, your fraction addition is wrong
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: