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

I cannot figure out the algorithm for my difference method. It will not work if

ID: 3624621 • Letter: I

Question

I cannot figure out the algorithm for my difference method. It will not work if the larger number has a smaller cent value than the amount that is being subtracting from it. For example 90.91-10.11 would work correctly but 90.11-10.91 would not calculate the cents correctly. Here is my program:

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

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 this.dollars=dollars;
}

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

public class AmountCalculator {


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 > 99) { c = c%100; d = 1; }
else {d = 0;}
a3.setCents(c);

d=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**");

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=new Amount();
a3=add(amount1, amount2);
Amount a4=new Amount();
a4=difference(amount1, amount2);

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



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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote