java - Defining a class name Money -


i stuck add/subtract methods defining class named money objects represent amounts of money. class should have 2 instance variables of type int dollars , cents in amount of money. include constructor 2 parameters of type int dollars , cents, 1 one constructor of type int amount of dollars 0 cents , no-argument constructor. include methods add , minus addition , subtraction of amounts of money, , return value of type money. include reasonable set of accessor , mutator methods methods equals , tostring.

again, stuck on add/minus part, cannot nail down. plus equals part bit confusing. yes homework. trying best need bit of push.

please take look....

public class money {      private static  int dollars;     private static int cents;      public money()     {      }      public money(int dollars, int cents)     {         this.dollars = dollars;         this.cents = cents;     }      public money(int dollars)     {         this.dollars = dollars;     }        public int getdollars() {         return dollars;     }      public void setdollars(int dollars) {         this.dollars = dollars;     }      public int getcents() {         return cents;     }      public void setcents(int cents) {         this.cents = cents;     }       public static money add(money m1, money m2)     {         int cash = m1.dollars + m2.dollars;         int change = m1.cents + m2.cents;         return new money(dollars, cents);       }      public static int minusmoney(int m3, int m4)     {         return (m3-m4);     }     public boolean equals(double yourmoney)     {         boolean result;         if (yourmoney > 0)         {             dollars += cents;             result = true;         }         else         {             result = false;         }         return result;       }      public string tostring()     {         return ("$" + getdollars() + "." + getcents());     }      public static void main(string[] args)     {         money mymoney = new money(2,30);          system.out.println("you have " + mymoney.tostring());          money mymoney1 = new money(2,30);         money mymoney2 = new money(3,10);          system.out.println("you have " + money.add(mymoney1, mymoney2));      }  } 

the requirements state operations on object (such add , subtract) return new instance rather changing current one. thus:

class money {     private int dollars;     private int cents;      public money(int dollars, int cents) {         if (dollars < 0 || cents < 0)             throw new illegalargumentexception("negative dollars or cents");         this.dollars = dollars;         this.cents = cents;     }      public money add(money other) {         int cents = this.cents + other.cents;         return new money(this.dollars + other.dollars + cents / 100, cents % 100);     } } 

with respect equals, defined override object.equals means needs accept object not double:

public boolean equals(object other) {     return other != null && other.getclass() == money.class         && this.dollars == (money)other.dollars         && this.cents == (money)other.cents; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -