hey again i hate to be a bother but i still can't get this to work
my driver program(the second code) won't call the add method or the minus method to subtract input
i tried to do this in my code below and have failed
if you have any suggestions please i really need to make this work thank you for any help the class(first code) and the driver program are below
public class Money
{
private int dollars;//instance variables
private int cents;
public Money(int d, int c)//constructor with two parameters for dollars and cents
{
dollars = d;
cents = c;
}
public Money(int da)//constructor with one parameter for dollars
{
dollars = da;
cents = 0;
}
public Money()//no-argument constructor
{
dollars = 0;
cents = 0;
}
public static Money add(Money m3, Money m4)//static method for addition
{
int dollars1 = m3.getDollars();
int dollars2 = m4.getDollars();
int cents1 = m3.getCents();
int cents2 = m4.getCents();
int sumDollars = dollars1 + dollars2;
int sumCents = cents1 + cents2;
if(sumCents >= 100)
{
sumDollars = sumDollars + 1;
sumCents = sumCents - 100;
}
Money output = new Money(sumDollars, sumCents);
return output;
}
public static Money minus(Money m1, Money m2)//static method for subtraction
{
int dollars1 = m1.getDollars();
int dollars2 = m2.getDollars();
int cents1 = m1.getCents();
int cents2 = m2.getCents();
int minusDollars = dollars1 - dollars2;
int minusCents = cents1 - cents2;
Money output1 = new Money(minusDollars, minusCents);
return output1;
}
public void add2(Money a)//method using calling object for addition
{
int dollars1 = this.getDollars();
int dollars2 = a.getDollars();
int cents1 = this.getCents();
int cents2 = a.getCents();
int sumDollars = dollars1 + dollars2;
int sumCents = cents1 + cents2;
if(sumCents >= 100)
{
sumDollars = sumDollars + 1;
sumCents = sumCents - 100;
}
this.setDollars(sumDollars);
this.setCents(sumCents);
}
public void minus2(Money c)//method using calling object for subtraction
{
int dollars1 = this.getDollars();
int dollars2 = c.getDollars();
int cents1 = this.getCents();
int cents2 = c.getCents();
int minusDollars = dollars1 - dollars2;
int minusCents = cents1 - cents2;
this.setDollars(minusDollars);
this.setCents(minusCents);
}
public int getDollars()//accessor method to get dollars
{
return dollars;
}
public int getCents()//accessor method to get cents
{
return cents;
}
public void setDollars(int dol)//mutator method to set amount of dollars
{
dollars = dol;
}
public void setCents(int cen)//mutator method to set amount of cents
{
cents = cen;
}
public String toString()//toString method
{
return ("$" + getDollars() + "." + getCents());
}
public boolean equals(Money yourMoney)//equals method
{
return ((this.dollars==yourMoney.dollars)&&(this.cents==yourMoney.cents));
}
}
import java.util.Scanner;
public class driver1
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Money good = new Money();
System.out.println("Enter a dollar amount.");
good.setDollars(keyboard.nextInt());
System.out.println("Enter a cents amount.");
good.setCents(keyboard.nextInt());
Money bad = new Money();
System.out.println("Enter a dollar amount.");
bad.setDollars(keyboard.nextInt());
System.out.println("Enter a cents amount.");
bad.setCents(keyboard.nextInt());
Money ok = new Money();
ok.add(good, bad);
System.out.println(ok);
ok.minus(good, bad);
System.out.println(ok);
}
}