Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have two classes, Amount and Amount Drivers. My sum and difference methods are

ID: 3624609 • Letter: I

Question

I have two classes, Amount and Amount Drivers. My sum and difference methods are printing 0.00 instead of the real amounts and I cannot figure out why. I have to follow this exact format for full credit. Please help me fix my problem..This is what I have so far:


public class Amount {

private int dollars;
private int cents;
public static final String ds = "$";
public static final String cs = "u00A2";

public Amount (int dollars, int cents)

{if (dollars<0)
dollars=0;

else this.dollars=dollars;

if (cents < 0 || cents>99)
cents=0;

else this.cents=cents;
}


public Amount() {
// TODO Auto-generated constructor stub
}


public int getDollars ()
{ return dollars;
}

public int getCents()
{ return cents;
}

public void setDollars(int dollars)
{ if (dollars<0)
{dollars=0;}
else dollars=this.dollars;
}

public void setCents (int cents)
{ if (cents<0 || cents>99)
{cents=0;}
else
cents=this.cents;
}


public boolean isLarger (Amount a)
{
if ((dollars > a.dollars) && (cents > a.cents)) return true;

else return false;
}


public double allDollars(Amount a1)
{
double total=0;
total=a1.getDollars()+a1.getCents();
return total;
}

public int roundDollars(double unRounded)
{
int dollarRounded;
{ dollarRounded = (int)(unRounded + 0.5); return dollarRounded; }
}


public boolean equals (Amount a)

{
if ((dollars == a.dollars) && (cents == a.cents)) return true;

else return false;
}


public String toString()
{
String s1 = ""; // empty string.
s1 = s1 + getCents();

String s2="";
s2=s2+getDollars();

return ds + s2 + " and " + cs + s1;
}

}


import java.util.Scanner;
import java.text.NumberFormat;

public class AmountCalculator extends Amount {


public static Amount add (Amount a1, Amount a2) // Returns an object of class Amount.
{
int c = 0, d = 0;
Amount a3 = new Amount();
c = a1.getCents( ) + a2.getCents( );

if (c > 9) { c = c - 10; d = 1; }
else {d = 0;}
a3.setCents(c);

d=a1.getDollars( ) + a2.getDollars( );
a3.setDollars(d);

return a3;
}

public static Amount difference (Amount a1, Amount a2)
{
int c=0;
int d=0;
Amount a3 = new Amount ();

if (a1.isLarger(a2)==true)
{c=a1.getCents()-a2.getCents();
d=a1.getDollars()-a2.getDollars();
a3.setCents(c);
a3.setDollars(d);
}

else
{ c=a2.getCents()-a1.getCents();
d=a2.getDollars()-a1.getDollars();
a3.setCents(c);
a3.setDollars(d);
}
return a3;
}


public static void main (String args[])
{

System.out.println("**Demonstrating the Amount Class**");

NumberFormat nf = NumberFormat.getInstance(); //Used to format the decimal places of my double to 2 places
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);

boolean flag=true;

while (flag)
{
Scanner reader=new Scanner(System.in);
System.out.print("Type C to continue or Q to quit: ");
String answer=reader.nextLine();
String uppercaseAnswer=answer.toUpperCase();

if (uppercaseAnswer.equals("C"))

{
System.out.print("Enter amount 1 as dollars and cents: ");
String aa=reader.nextLine();

System.out.print("Enter amount 2 as dollars and cents: ");
String ab=reader.nextLine();

String a1=aa.trim();
String a2=ab.trim();

char space=' ';
int space1=a1.indexOf(space);
int space2=a2.indexOf(space);

String dollars1=a1.substring(0,space1);
String cents1=a1.substring(space1+1);

String dollars2=a2.substring(0,space2);
String cents2=a2.substring(space2+1);

int dollar1=Integer.parseInt(dollars1);
int cent1=Integer.parseInt(cents1);
int dollar2=Integer.parseInt(dollars2);
int cent2=Integer.parseInt(cents2);

Amount amount1 = new Amount(dollar1, cent1);
Amount amount2 = new Amount (dollar2, cent2);

Amount a3=add(amount1, amount2);
Amount a4=difference(amount1, amount2);

String amountTwo=dollar2 + "." + cent2;
double amounTwo=Double.parseDouble(amountTwo);


System.out.println("Amount one is: " + amount1);
System.out.println("Amount two is: " + amount2);

System.out.println("Sum of the two amounts is " + a3);

if (amount1.isLarger(amount2)==true)
{System.out.println("The larger amount is: " + amount1);}
else if (amount2.isLarger(amount1)==true)
{System.out.println("The larger amount is: " + amount2);}

System.out.println("The difference is " + a4);

if (amount1.equals(amount2))
{System.out.println("The amounts are equal");}
else
{System.out.println("The amounts are not equal");}

System.out.println("Amount-1 in dollars is $" + amount1.getDollars() + "." + amount1.getCents());

System.out.println("Amount-2 rounded is " + amount1.roundDollars(amounTwo));
}

else
System.out.println("Goodbye!");
flag=false;
}

}

}


Explanation / Answer

Rewrote your difference() and isLarger() methods.

public boolean isLarger (Amount a)
{
return (dollars > a.dollars || (dollars == a.dollars && cents > a.cents));
}


public static Amount difference (Amount a1, Amount a2)
{
int c=0;
int d=0;
Amount a3 = new Amount ();

if(a1.isLarger(a2))
{
d = a1.getDollars() - a2.getDollars();
c = a1.getCents() - a2.getCents();
if(c < 0)
{
c += 100;
d --;
}
}
else
{
d = a2.getDollars() - a1.getDollars();
c = a2.getCents() - a1.getCents();
if(c < 0)
{
c += 100;
d --;
}
}

a3.setCents(c);
a3.setDollars(d);

return a3;
}