I'm trying to make this piece of code print the actual numbers, and not the hexadecimal location.
public class MoneyDriver
{
//This is a driver for testing the class
public static void main(String[] args)
{
final int BEGINNING = 500;
final Money FIRST_AMOUNT = new Money(10.02);
final Money SECOND_AMOUNT = new Money(10.02);
final Money THIRD_AMOUNT = new Money(10.88);
Money balance = new Money(BEGINNING);
System.out.println("The current amount is " +
balance.toString());
balance = balance.add(SECOND_AMOUNT);
System.out.println("Adding " + SECOND_AMOUNT +
" gives " + balance.toString());
balance = balance.subtract(THIRD_AMOUNT);
System.out.println("Subtracting " + THIRD_AMOUNT +
" gives " + balance.toString());
boolean equal = SECOND_AMOUNT.equals(FIRST_AMOUNT);
if(equal)
System.out.println(SECOND_AMOUNT + " equals "
+ FIRST_AMOUNT);
else
System.out.println(SECOND_AMOUNT.toString() +
" does not equal " + FIRST_AMOUNT);
equal = THIRD_AMOUNT.equals(FIRST_AMOUNT);
if(equal)
System.out.println(THIRD_AMOUNT + " equals " +
FIRST_AMOUNT);
else
System.out.println(THIRD_AMOUNT + " does not equal "
+ FIRST_AMOUNT);
}
}
This is the main class which is called by moneydriver
public class Money
{
private long dollars;
private long cents;
public Money(double amount)
{
if (amount < 0)
{
System.out.println(
"Error: Negative amounts of money are not allowed.");
System.exit(0);
}
else
{
long allCents = Math.round(amount*100);
dollars = allCents/100;
cents = allCents%100;
}
}
public Money add(Money otherAmount)
{
Money sum = new Money(0);
sum.cents = this.cents + otherAmount.cents;
long carryDollars = sum.cents/100;
sum.cents = sum.cents%100;
sum.dollars = this.dollars
+ otherAmount.dollars + carryDollars;
return sum;
}
public Money subtract (Money amount)
{
Money difference = new Money(0);
if (this.cents < amount.cents)
{
this.dollars = this.dollars - 1;
this.cents = this.cents + 100;
}
difference.dollars = this.dollars - amount.dollars;
difference.cents = this.cents - amount.cents;
return difference;
}
public int compareTo(Money amount)
{
int value;
if(this.dollars < amount.dollars)
{
value = -1;
}
else if (this.dollars > amount.dollars)
{
value = 1;
}
else if (this.cents < amount.cents)
{
value = -1;
}
else if (this.cents > amount.cents)
{
value = 1;
}
else
{
value = 0;
}
return value;
}
}
The objectives is to write equals method (on main
class). The method compares the instance variables of the calling object with instance variables of the parameter object for equality and returns true if the dollars and the cents of the calling object are the same as the dollars and the cents of the parameter object. Otherwise, it returns false.
Write toString
method (on main
class). This method will return a String that
looks like money, including the dollar sign. Remember that if you have less than 10 cents, you will need to put a 0 before printing the cents so that it appears correctly with 2 decimal places.
If both of the method is implemented correctly
According to tutorialspoint, you're supposed to do either
String toString()
static String toString(int i)
But the supplied moneydriver already has the tostring
method, but doesn't display the numbers, instead it displays a hexadecimal location of the variable.
The equals method is already used in moneydriver, so I'm kinda lost on that too.
The correct output should look like this
The current amount is $500.00 Adding $10.02 gives $510.02 Subtracting $10.88 gives $499.1 $10.02 equals $10.02 $10.88 does not equal $10.02
Completely lost in this, thanks in advance for help.