Hello: I am stuck on something I need a bit of guidance on. Here is what I am ne
ID: 3533023 • Letter: H
Question
Hello: I am stuck on something I need a bit of guidance on. Here is what I am needing to do:
Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a number of nickels, a number of pennies. Include a constructor, the accessors and mutators, and methods toString and equals. Also code the following methods: one returning the total amount of money in dollar notation with two significant digits after the decimal point, and other returning the money in quarters ( for instance, 0.75 if there are three quarters ), in dimes, in nickels and in pennies. Write a client class to test all the methods in your class.
I am stuck on the last portion of the problem. How do I structure the method to get the total money in dollar notation with two significant digits afte rthe decimal point and how do I get the method that returns the money in quarters, dimes, nickels and pennies?
I have already seen the guidance somebody received previously with the code that starts out like this:
"import java.text.DecimalFormat;
public class Money
{
private int quarters;
private int dimes;
private int nickels;
private int pennies;
// Constructors - - - - - - - - - - - - - -
public Money() {
// default, no-argument constructor
// defers to worker signature
this(0, 0, 0, 0);
}"
I do not want that reposted to me, I need additional help to answer this.
Here is what I have so far, thanks for the help.
import java.text.DecimalFormat;
public class Coins
{
private int quarters;
private int dimes;
private int nickels;
private int pennies;
private int total;
public Coins ()
{
}
public Coins ( int q, int d, int n, int p, int t )
{
quarters = q;
dimes = d;
nickels = d;
pennies = p;
total = t;
}
public int getQuarters()
{
return quarters;
}
public int getDimes()
{
return dimes;
}
public int getNickels()
{
return nickels;
}
public int getPennies()
{
return pennies;
}
public int getTotal()
{
return total;
}
}
Explanation / Answer
/Client.java
//----------------------------------------------
import java.text.DecimalFormat;
import java.util.Scanner;
public class Client
{
public static void main(String args[])
{
DecimalFormat df = new DecimalFormat("###.##");
Scanner in=new Scanner(System.in);
int option;
Coins c= getValues();
do
{
System.out.println("Choose one of the option");
System.out.println("1.Get Total ");
System.out.println("2. check toString() method");
System.out.println("3. display total in terms of quarter,dime ,nickel, pennies");
System.out.println("4. chech equall method");
System.out.println("5. exit");
option=in.nextInt();
if(option==1)
{
System.out.println("total= $"+df.format(c.getTotal()));
}
else if(option==2)
{
System.out.println(c);
}
else if(option==3)
{
System.out.println(c.getInQuartersDimesNickelsPennies());
}
else if(option==4)
{
System.out.println("enter the contentent of first coin");
Coins c1=getValues();
System.out.println("enter the contentent of first coin");
Coins c2=getValues();
if(c1.equall(c1, c2))
System.out.println("Both coins are equall in terms of total amount");
else
System.out.println("Both coins are not equall in terms of total amount");
}
else if(option>5)
System.out.println("Wrong choice");
}
while(option!=5);
}
public static Coins getValues()
{
int q,d,n,p;
Scanner in=new Scanner(System.in);
System.out.print("enter no of quarters: ");
q=in.nextInt();
System.out.print("enter no of dimes: ");
d=in.nextInt();
System.out.print("enter no of nickels: ");
n=in.nextInt();
System.out.print("enter no of pennies: ");
p=in.nextInt();
Coins c=new Coins(q,d,n,p);
return c;
}
}
//---------------------------------------------------------------------------
//Coins.java
import java.text.DecimalFormat;
public class Coins
{
private int quarters;
private int dimes;
private int nickels;
private int pennies;
private double total;
public Coins ()
{
quarters=0;
dimes=0;
nickels=0;
pennies=0;
total=0;
}
public Coins ( int q, int d, int n, int p)
{
quarters = q;
dimes = d;
nickels = d;
pennies = p;
total=0;
}
public int getQuarters()
{
return quarters;
}
public int getDimes()
{
return dimes;
}
public int getNickels()
{
return nickels;
}
public int getPennies()
{
return pennies;
}
public void setQuarter(int quarters)
{
this.quarters=quarters;
}
public void setDimes(int dimes) {
this.dimes=dimes;
}
public void setNickels(int nickels ) {
this.nickels=nickels;
}
public void setPennies(int pennies ) {
this.pennies=pennies;
}
public double getTotal()
{
total=quarters*0.25+dimes*0.10+nickels*0.05+pennies*0.01;
DecimalFormat df = new DecimalFormat("###.##");
df.format(total);
return total;
}
public boolean equall(Coins c1,Coins c2)
{
if(c1.getTotal()==c2.getTotal())
return true;
else
return false;
}
public String toString()
{
return "Quarters= "+quarters+" Dimes= "+dimes+" Nickels= "+nickels+" Pennies= "+pennies+" total= $"+getTotal();
}
public String getInQuartersDimesNickelsPennies()
{
return "Quarters= $"+(quarters*0.25)+" Dimes= $"+dimes*0.10+" Nickels= $"+nickels*0.05+" Pennies= $"+pennies*0.01+" total= $"+getTotal();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.