Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arithmatic functions between reciever and argument

Status
Not open for further replies.

jjinx

Programmer
Oct 6, 2002
12
US
I need to add the reciever object and the arguement of a method in a seperate class.

class CashRegister{

public void computesubtotal(){
subtotal.plus(itemprice);
}//end computesubtotal

}//end CashRegister


class DollarAmount{

public DollarAmount plus(int increment){

.......what goes here to add subtotal and itemprice
}//end plus

}//end DollarAmount
 
Well, I need more informations to help :
- is your "DollarAmount" class a subclass of your "CashRegister" class ?
- is your "subtotal" var (in "CashRegister" class) of "DollarAmount" type ? Water is not bad as soon as it stays out human body ;-)
 
Targol,

The two vars are of compatable types.
The subtotal is of type DollarAmount wihich is stored as a long and increment is an int.

The DollarAmount class is not a subclass of CashRegister.

I need to know how to access the reciver object of the plus method from within the plus method.


ex: subtotal.plus(increment)

I must add increment to the subtotal.
 
If you store the current value of your DollarAmount class inside it, it's quite simple :
Code:
class CashRegister{

    DollarAmount subtotal = new DollarAmount();

    public void computesubtotal(){  
      subtotal.plus(itemprice);
    }//end computesubtotal

    // now you can use subtotal.getValue();
}//end CashRegister

class DollarAmount{

    private long curentValue=0;

    public void plus(int increment){
      curentValue += increment;
    }//end plus

    public long getValue() { return curentValue; }
    public void setValue(long newValue) { curentValue = newValue; }

}//end DollarAmount
Water is not bad as soon as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top