Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1.  
  2. /**
  3. * A class for managing money.
  4. *
  5. * Travis Smith
  6. * 10/21/11 CS221 S1 Lab6
  7. */
  8. public class Money
  9. {
  10. //declare variables
  11. private final int CENTS_PER_DOLLAR = 100;
  12. private int cents;
  13.  
  14. //constructors
  15. public Money ()
  16. { cents = 0;
  17.  
  18. }//end default constructor
  19.  
  20. public Money (int totalCents)
  21. { cents = totalCents;
  22.  
  23. }//end 1 int constructor
  24.  
  25. public Money (int dollars, int cents)
  26. { this.cents = cents + dollars * CENTS_PER_DOLLAR;
  27. }//end 2 int constructor
  28.  
  29. //getters
  30. public int getDollars()
  31. { int dollars = (cents - (cents % CENTS_PER_DOLLAR)) / CENTS_PER_DOLLAR;
  32. return dollars
  33. }//end get dollars
  34.  
  35. public int getCents()
  36. { int answer = cents % CENTS_PER_DOLLAR;
  37. return answer;
  38. }//end get cents
  39.  
  40. public String toString()
  41. { return ("$" + this.getDollars + "." + this.getCents);
  42. }//end to string
  43.  
  44.  
  45. //setters
  46. public void setDollars(int dollars)
  47. { this.dollars = dollars;
  48. }//end set dollars
  49.  
  50. public void setCents (int cents)
  51. { this.cents = cents;
  52. }//end set cents
  53.  
  54. }//end class
Add Comment
Please, Sign In to add comment