Results 1 to 4 of 4
Thread: Returning an object
- 11-02-2010, 03:26 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Returning an object
I am working on a project that does different operations on fractions. I have created fraction objects called f1 and f2, with numerator and denominator as their parameters.
My teacher has instructed us to make it work with this in the main method:
Now assuming that everything in the plus method works correctly, how would I return a fraction?Java Code:fraction f1 = new fraction(3,4); fraction f2 = new fraction(1,8); fraction result = f1.plus(f2);
In the fraction class I have:
but I don't know how to return an object.Java Code:public fraction plus(fraction f)
Would I just return the two parameters like:
?Java Code:return (numerator,denominator);
Sorry if my terminology or explanation is poor, but I'm in my first semester of java.
- 11-02-2010, 03:40 AM #2
If you have an object in the function, like this:
Then you can just return this.Java Code:public fraction plus(fraction f) { this.value += f.value; }
If you do something like this...
Then you'll want to return n instead.Java Code:public fraction plus(fraction f) { fraction n = new fraction(this.value + f.value); }
Personally I'd go with the first method, but it all depends what you have so far. I'm just using .value as a placeholder for whatever your data already is, since I can't see your actual source.
- 11-02-2010, 04:01 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Thanks Zack! I used the second method and all is working now.
- 11-02-2010, 11:30 AM #4
Similar Threads
-
Selecting min size and returning reference to object
By Saletra in forum New To JavaReplies: 1Last Post: 08-28-2010, 01:27 PM -
Method, returning reference to an object
By Saletra in forum New To JavaReplies: 3Last Post: 08-23-2010, 08:22 PM -
returning an object from a method
By bigj in forum New To JavaReplies: 7Last Post: 01-08-2010, 12:39 PM -
returning arrays
By cjohnson412 in forum New To JavaReplies: 4Last Post: 11-25-2008, 01:30 PM -
Why is my list returning nothing?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-05-2008, 03:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks