Results 1 to 11 of 11

Thread: Money Class

  1. #1
    Dozo23 is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Money Class

    I need to create a money class in java. here is the program i need to complete.

    Java Code:
    public class Money implements Comparable<Money> {
    	
    	private int dollars, cents;
    	
    	/**
    	 * Constructor makes a call to {@link #Money(int, int)} 
    	 * with (0,0) as parameters
    	 */
    	public Money() {
    		this(0,0);
    	}
    	
    	public Money(int dollars, int cents) {
    		// TODO Fill this in
    		// Hint: Use integer division by 100 to get dollars from cents
    		// and use modulus to get cent values between 0 and 100 (mod by 100)
    		
    		
    	
    	}
    	
    	
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// LAB 14
    		Money m1 = new Money(), m2= new Money(6,5);
    		System.out.println(m1.getCents());
    		System.out.println(m2.getDollars());
    		System.out.println(m2);
    		System.out.println(m1.compareTo(m2));
    		System.out.println(m1.equals(m2));
    		
    		// LAB 15
    		m1 = new Money(4,87);
    		m2 = new Money(5,243);
    		
    		m1.add(m2);
    		
    		System.out.println(m1.toString());
    		
    	}
    
    	public void add(Money other) {
    		// TODO Fill this in
    		// Hint: Add the cents first, check if you surpass 100 (check hint from 2-arg constructor),
    		// then add dollars
    	}
    
    	@Override
    	public int compareTo(Money other) {
    		// TODO Fill this in
    		/*
    		 * Steps:
    		 * 1) Test the dollars in both first
    		 * 2) If dollar values are equal, test
    		 * cents in both
    		 * 3) If both dollars and cents are equal return 0
    		 * 
    		 * Hint: compareTo can be viewed as a subtraction operation
    		 */
    	}
    	
    	@Override
    	public boolean equals(Object other) {
    		// TODO Fill this in
    		/*
    		 * Steps:
    		 * 1) Test if other is null
    		 * 2) Test if other is of type Money
    		 * 3) If the above tests pass, then cast other into
    		 * type Money
    		 * 4) Compare dollar and cents in both. 
    		 * If equal return true, otherwise return false
    		 */
    		if (this == other) return true;
    		if(other!=null && this.getClass().equals(other.getClass())) {
    			// Start from step 3
    		}
    	}
    	
    	@Override
    	public String toString() {
    		String c;
    		if(cents<10) {
    			c = "0" + cents;
    		} else {
    			c = "" + cents;
    		}
    		return "$ " + dollars + "." + c;
    	}
    	
    	public int getCents() {
    		return cents;
    	}
    	
    	public int getDollars() {
    		return dollars;
    	}
    
    }
    I am confused and i dont know how to complete the program. i have tried using a similar program i have but i am still lost.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Money Class

    What are you confuse about? Any specific problem you have there? I guess you need to replace the TODO with the concrete code right? Then try to write it first and tell us where you got confuse.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Money Class

    Java Code:
    public Money(int dollars, int cents) {
            // TODO Fill this in
            // Hint: Use integer division by 100 to get dollars from cents
            // and use modulus to get cent values between 0 and 100 (mod by 100)
    That hint only makes sense if the amount was passed in as a single parameter. Eg 754 for 7 dollars and 54 cents but the parameters are already dollars and cents. I would seek some clarification.

  4. #4
    Dozo23 is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Money Class

    Well i read the hints but they dont make sense to me.

    Java Code:
    public Money(int dollars, int cents) {
    		// TODO Fill this in
    		// Hint: Use integer division by 100 to get dollars from cents
    		// and use modulus to get cent values between 0 and 100 (mod by 100)
    	}
    can someone rephrase the hint? I also dont understand the other hints.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Money Class

    Like I said above 754 is 7 dollars and 54 cents. If you divide 754 by 100 you get ....? If you mod 754 by 100 you get ...?

  6. #6
    Dozo23 is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Money Class

    would this work?

    Java Code:
    int integer=100;
    		if(cents>=100)
    			dollars = cents/integer;
    		if(cents>=0 && cents<100)
    			cents = cents%integer;
    what about the other hints?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Money Class

    What happened when you tried it?

  8. #8
    Dozo23 is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Money Class

    not sure. i cant run the program cuz its not complete. but if i understand the hint it should be correct.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Money Class

    Then write a simple test class to see if it does work.

  10. #10
    Dozo23 is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Money Class

    How can i go about doing that? I have not done anything like that before.

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Money Class

    A test class is a very simple application which tests the functionality of the class. This can be something as simple as a class containing a main method which creates an instance of the class you want to test. From there you could test individual methods to assess how they would respond to calls.

    If you don't wish t go that route you could modify the main method within the application itself though this can cause issues if you forget to remove the code or remove required code.

    Regards.

Similar Threads

  1. get money now.. if you could...?
    By CS Geek in forum Advanced Java
    Replies: 1
    Last Post: 11-09-2009, 01:52 AM
  2. Work & Earn money from home, use rebate lower price save money $$$
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 05:57 AM
  3. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 07:05 AM
  4. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 04:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •