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

Write the complete code for a public Java class called ChangeMachine that can ho

ID: 3551689 • Letter: W

Question

Write the complete code for a public Java class called ChangeMachine that can hold nickels, dimes and

quarters. Include a private instance variable to hold each type of coin that the machine holds. Write

accessor methods for each of the coin counts (hint: accessor method names should start with get).

Include an accessor that will return how much money is in the machine. Also include mutator methods

to either add or remove a single coin of each type from the machine (hint: these methods should start

with set).

Explanation / Answer

//Write the complete code for a public Java class called ChangeMachine that can hold nickels, dimes and quarters.
class ChangeMachine
{
// Include a private instance variable to hold each type of coin that the machine holds
private int nickels;
private int dimes;
private int quarters;
public ChangeMachine()
{
setNickels(0);
setDimes(0);
setQuarters(0);
}
public ChangeMachine(int n,int d,int q)
{
setNickels(n);
setDimes(d);
setQuarters(q);
}
//Write accessor methods for each of the coin counts (hint: accessor method names should start with get).
public int getNickels()
{
return nickels;
}
public int getDimes()
{
return dimes;
}
public int getQuarters()
{
return quarters;
}
//Include an accessor that will return how much money is in the machine.
public double getMoney()
{
return (0.25*quarters+0.1*dimes+0.05*nickels);
}
// Also include mutator methods to either add or remove a single coin of each type from the machine
//(hint: these methods should start with set).
public void setNickels(int n)
{
nickels = n;
}
public void setDimes(int d)
{
dimes = d;
}
public void setQuarters(int q)
{
quarters = q;
}
}

public class tester
{
public static void main(String[] args)
{
ChangeMachine CM = new ChangeMachine(4,5,6);
System.out.println("Money in Change Machine given by $"+ CM.getMoney());
}
}