K, how do I write my compare and subtract methodfor the following code to work?
ID: 3615234 • Letter: K
Question
K, how do I write my compare and subtract methodfor the following code to work? NOTE: There was a lot more to thisprogram and I figured it all out, but I couldn't figure these last2 out so I deleted the extra nonsense and color coded themethods and constructors for your convenience. Thanks to whoeverhelps. (It might be easier to understand the code better if youcopy/paste to a word document.) Also, I'm not allowed to change thecode in the main method. Only what's in the MoneyBag Class. Thanksto all who help.
class MoneyBag
{
private int quarters;
private int dimes;
private int nickles;
private int pennies;
private int total;
//CUSTOMCONSTRUCTIOR---------------------------------------------------------------------------------------------------------
public MoneyBag(int q, int d, int n, intp)
{
quarters = q;
dimes = d;
nickles =n;
pennies =p;
}
//GET TOTALMETHOD-------------------------------------------------------------------------------------------------------------
public int getValue()
{
total =(quarters*25)+(dimes*10)+(nickles*5)+pennies;
return total;
}
//COMPARINGMETHOD--------------------------------------------------------------------------------------------------------------
public int compare(MoneyBag Value)
{
}
//SUBTRACTING MONEYBAG CONTENTSMETHOD-------------------------------------------------------------------------
public MoneyBag subtract(MoneyBagsubtract)
{
}
}
public class Lab9
{
public static void main(String args[])
{
MoneyBag mb0 = newMoneyBag();
MoneyBag mb1 = newMoneyBag(1, 2, 3, 4);
System.out.print("Comparing mb0 and mb1: ");
mb0.compare(mb1);
System.out.println("Subtracting $5.00");
mb2 =mb2.subtract(500);
}
}
Explanation / Answer
please rate-thanks the main program must be redone for 2, possibly 3 reasons 1) mb2 is undefined it must be defined 2) subtract takes a moneyBag as a parameter, you are passing it aninteger, unless subtract is wrong and it should accept an integer 3)in addition where are the results supposed to be, I would expectthe main should call a printmethod? and there is no method to print the results maybe you should post the assignment with the code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.