• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to add objects  RSS feed

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to add three different objects of type money. How do I add them? I think it is not possible to add objects.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, there's no such thing as adding objects. You can, although, add properties of an object directly -or- use inherent methods in the class to add the object properties.

A good example of this is the Calendar class. I could add time to an existing calendar one of two ways, use the inherent calendar.add() method or convert the calendar into a long using calendar.getTime().getTime(), perform calculations, and then create a new calendar (or sometimes set the existing) to be the new value such as:

Adding 10 seconds:



Of course, the first one is better since it relies on internal methods for performance whereas the second one has its application at times as well.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean aritmetically add the three objects together (total = money1 + money2 + money3), then no you can't add them.
Does your money class have a method that returns its value ?
If so, then you would need to call this method on each object and add the returned values together.
For example, if your class had a method called getValue() which returned a double, you could type
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make it more clear.

1. I have month class which will return getbeginprinciple() and getendpriniple() and getschedprinciple().

2. In process class I wanted to add these three objects. To process class I am passing month object.
public void validate(Object obj, EditRules rules) {
Month mthy = (Monthly) obj;

How do I add month class objects in process class?

Thanks.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you made it less clear (are we talking about money or month objects ?)
Can you give an example of the three objects you want to add together (i.e. what fields they contain and what values these fields have) and also the result you want from adding them all together.
[ October 28, 2005: Message edited by: Joanne Neal ]
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for making it less clear


I am getting values from xml file. In month class I have like this
import com.abc.common.util.LCMoney;
public LCMoney getBegSchPrinBal() {
return begSchPrinBal;
}

public LCMoney getUnSchedPrincipal() {
return unSchedPrincipal;
}
public LCMoney getEndingSchPrinBal() {
return endingSchPrinBal;
}

In process class I wanted to add these two objects and equate with getEndingSchPrinBal() to see whether values are same or not.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats com.abc.common.util.LCMoney? We need to see a class definition for this. As I said previously, the objects cannot be added but the properties of the objects can. Convert the date into java primitives (since its money it should be easy to represent as a primitive) and perform calculations there.
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
com.abc.common.util.LCMoney is the home grown class used for money calculations.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its home grown, then its up to you to decide how to add the properties of the object. Add a getter method to retrieve the value as a long. Just keep in mind that if you are adding values from two objects together and creating a new object with this total value, your still not really adding the objects.

If you would like a visual... lets say Jim and Bill each have $50. If you add them together the combined value of Jim and Bill is $100. But, by adding them, we did not create a super human Jim/Bill hybrid, we just added properties of them together, they are still separate objects.
[ October 28, 2005: Message edited by: Scott Selikoff ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This paper about testing develops some neat ideas about money and arithmetic:

http://members.pingnet.ch/gamma/junit.htm

See if it offers any inspiration.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!