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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting a static method

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I have a static method I wrote:

public static Money add(Money money1, Money money2)
{
int newDollars = money1.getDollars() + money2.getDollars();
int newCents = money1.getCents() + money2.getCents();

if (newCents >= 100)
{
newDollars++;
newCents -= 100;
}
return new Money(newDollars, newCents);
}

What I need to do is to change it from a static method to a method that essentially produces the same result but using a calling object and an argument. I am not even sure how to start this. Any suggestions?????
 
Hi

Code:
[b]public class[/b] Pocket
{
  Money pocketMoney;

  [b]public[/b] Pocket()
  {
    pocketMoney=[b]new[/b] Money(0,0);
  }

  [b]public[/b] Money add(Money money)
  {
    [b]int[/b] newDollars=pocketMoney.getDollars()+money.getDollars();
    [b]int[/b] newCents=pocketMoney.getCents()+money.getCents();

    [b]if[/b] (newCents>=100) {
      newDollars++;
      newCents-=100;
    }

    pocketMoney=[b]new[/b] Money(newDollars,newCents);

    [b]return[/b] pocketMoney;
  }
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top