Question

For this program, you are to write and test the class Money. This is not a complete program, just the Money class.

Here is the UML class diagram for the Money class.

Money

- dollars: int
- cents: int

+ setMoney (int, int)
+ add(Money):void
+subtract(Money): void
+ equals(Money): boolean
+ lessThan(Money): boolean
+ toString():String

Instance data: There are two integer values for the instance data representing the dollars and cents. These must be declared as private.

Instance methods: There are six methods in the class. All methods are public.

1. setMoney(int dollarsIn, int centsIn) –Set the dollars and cents to the values of the parameters. If either input is negative, set the dollars and cents to 0.
2. add(Money moneyIn) – add the dollars and cents of the parameter to the current object. If the cents exceeds 100, adjust the dollars and cents accordingly.
3. subtract(Money moneyIn) – subtract the parameter from the current object If the cents fall below 0, then the dollars and cents must be adjusted. If the number of dollars falls below 0, then set both the dollars and cents to 0
4. boolean equals(Money moneyIn): return true if both the dollars and cents of the parameter match the dollars and cents of the current object, otherwise return false
5. boolean lessThan(Money moneyIn): return true if the current object represents less money than the parameter. otherwise return false
6. String toString(): return the values of the object as a String formatted as $dd.cc

Documentation: Use Javadoc style for all comments.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

/*
* Class: Money
* Desc: Class defines money in terms of dollars and cents. It can set a value, add, subtract, check for equivalence, or if it is less than another money value
* It also prints out the amount of money in the correct format of dollars and cents
*/
public class Money {

private int dollars, cents;

/**
* @param dollars
* @param cents
*/
public void setMoney(int dollars, int cents)
{
this.dollars = dollars;
this.cents = cents;
}

/**
* Adds money to current value, based on parameter sent in
* @param money
*/
public void add(Money money)
{
dollars += money.dollars;
dollars += (cents + money.cents)/100;
cents = (cents + money.cents)%100;
}

/**
* Subtracts parameter sent in from current object
* @param money
*/
public void subtract(Money money)
{...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Problems: Student Details & Fibonacci Series
Homework Solution
$20.00
Java
Programming
OOP
Computer Science
Fibonacci Series
Students
Classes
Instances
Loops
Conditions
Statements
Variables
Integers
Mathematics
Arrays in Programming Language Java
Homework Solution
$20.00
Java
Programming
Computer Science
Arrays
Integers
Random Numbers
Methods
Functions
Even Elements
Odd Elements
Prime Numbers
Minimum Values
Maximum Values
Average Values
Loops
Conditions
Statements
Pyramid Using Asterisks in Java
Homework Solution
$30.00
Java
Programming
Codes
Algorithms
Computer Science
Statements
Variables
Loops
Input
Output
Integers
Strings
Asterisks
Symbols
Pyramid
Rows
Odd Numbers
Wordlist Generator in Java (Using OOP)
Homework Solution
$125.00
Java
Programming
OOP
Wordlist Generator
Inheritance
Polymorphism
Arrays
String Class Methods
Charlist
Input
Output
Statements
Variables
Loops
Conditions
Java Project: Right Triangle Of Stars
Homework Solution
$30.00
Java
Programming
Computer Science
Mathematics
Loops
Input
Output
Conditions
Right Triangle
Base
Hypotenuse
Stars
Statements
Variables
Even Numbers
Odd Numbers
Transformations
Get help from a qualified tutor
Live Chats