1. Member
Join Date
Feb 2013
Posts
11
Rep Power
0

## 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);

System.out.println(m1.toString());

}

// TODO Fill this in
// Hint: Add the cents first, check if you surpass 100 (check hint from 2-arg constructor),
}

@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. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
757
Rep Power
7

## 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. ## 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. Member
Join Date
Feb 2013
Posts
11
Rep Power
0

## 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. ## 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. Member
Join Date
Feb 2013
Posts
11
Rep Power
0

## 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;```

7. ## Re: Money Class

What happened when you tried it?

8. Member
Join Date
Feb 2013
Posts
11
Rep Power
0

## 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. ## Re: Money Class

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

10. Member
Join Date
Feb 2013
Posts
11
Rep Power
0

## Re: Money Class

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

11. Senior Member
Join Date
Oct 2010
Posts
317
Rep Power
4

## 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.

#### Posting Permissions

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