i did this code for this question. Write a class called PocketChange that models a handful of change you might find in your pocket. The class has the following specifications:
• Instance Variables - all instance variables are private!
- int toonies - the number of toonies in the user's pocket
- int loonies - the number of loonies in the user's pocket
- int quarters - the number of quarters in the user's pocket
- int dimes - the number of dimes in the user's pocket
- int nickels - the number of nickels in the user's pocket
- int pennies - the number of pennies in the user's pocket
• Methods:
- PocketChange() - default constructor assigns 0 as a default value to all instance variables.
- PocketChange(toonies, loonies, quarters, dimes, nickels, pennies) - sets the values of all the instance variables to the programmer-specified parameter values.
- calculateTotal method will
calculate and return the total amount of money in the user's pocket, based on the values in each of the instance variables.
- toString() - returns a String with the following format:
- Toonies: t
- Loonies: L
- Quarters: q
- Dimes: d
- Nickels: n
- Pennies: p
where t, L, q, d, n, and p are the number of toonies, loonies, quarters, dimes, nickels and pennies.
In the main method:
Write a test program to test your class.
Get user input for the pocket change he has right now. And then calculate the total value of the change. i did this code but there one line that is not working:this(toonies:0,loonies=0,quaters=0,dimes=0,nickels=0,pennies=0);
the code that i did is:
private int toonies;
private int loonies;
private int quaters;
private int dimes;
private int nickels;
private int pennies;
public PocketChange(){
// this(toonies:0,loonies=0,quaters=0,dimes=0,nickels=0,pennies=0);
}
public PocketChange(int toonies,int loonies, int quarters, int dimes, int nickels, int pennies){
this.toonies=toonies;
this.loonies=loonies;
this.quaters=quarters;
this.dimes=dimes;
this.nickels=nickels;
this.pennies=pennies;
}
public double calculateTotal(){
return toonies*2+ loonies*1 +quaters*0.25+dimes*0.10+nickels*0.05+pennies*0.01;
}
public String toString(){
return "Toonies: "+toonies+ "\nLoonies"+loonies+"\nQuaters: "+quaters+
"\ndimes:"+dimes+"\nnickels:"+nickels+"\nPennies"+pennies;
}
2 answers