Create a Java project called Lab6 . Create a class Coins that encapsulates the c
ID: 3657654 • Letter: C
Question
Create a Java project called Lab6 . Create a class Coins that encapsulates the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a number of nickels, and a number of pennies. Include a constructor, the getters and setters, and methods toString and equal Include the following methods: One returning the total amount of money in dollar notation write two significant digits after the decimal point One returning the money in quarters (for instance, 0.75 if there three quarters) One returning the money in dimes One returning the money in nickels One returning the money in pennies Write a client class to test all the methods in your class.Explanation / Answer
please rate - thanks
public class coinTester
{
public static void main(String[] args) {
coins c=new coins();
c.setQuarters(2);
c.setDimes(3);
c.setNickels(5);
c.setPennies(1);
System.out.println(c.toString());
coins c1=new coins();
c1.setQuarters(2);
c1.setDimes(3);
c1.setNickels(5);
c1.setPennies(1);
System.out.println(c1.toString());
coins c2=new coins();
c2.setQuarters(2);
c2.setDimes(3);
c2.setNickels(10);
c2.setPennies(1);
System.out.println(c2.toString());
if(c.equals(c1))
System.out.println("c and c1 are the same");
else
System.out.println("c and c1 are not the same");
if(c.equals(c2))
System.out.println("c and c2 are the same");
else
System.out.println("c and c2 are not the same");
System.out.println("C Total amount is "+c.amtTotal());
System.out.println("From Quarters "+c.amtQuarters());
System.out.println("From Dimes "+c.amtDimes());
System.out.println("From Nickels "+c.amtNickels());
System.out.println("From Pennies "+c.amtPennies());
}
}
------------------------------
import java.text.DecimalFormat;
class coins
{DecimalFormat two = new DecimalFormat("$0.00");
private int quarters,dimes,nickels,pennies;
public coins()
{quarters=0;
dimes=0;
nickels=0;
pennies=0;
}
public void setQuarters(int n)
{quarters=n;
}
public void setDimes(int n)
{dimes=n;
}
public void setNickels(int n)
{nickels=n;
}
public void setPennies(int n)
{pennies=n;
}
public int getQuarters()
{return quarters;
}
public int getDimes()
{return dimes;
}
public int getNickels()
{return nickels;
}
public int getPennies()
{return pennies;
}
public String toString()
{return "Quarters "+quarters+" Dimes "+dimes+
" Nickels "+nickels+" Pennies "+pennies+" ";
}
public boolean equals(coins n)
{ if(quarters==n.quarters&&dimes==n.dimes&&
nickels==n.nickels&&pennies==n.pennies)
return true;
return false;
}
public String amtQuarters()
{
return ""+two.format(quarters*.75);
}
public String amtDimes()
{return ""+two.format(dimes*.1);
}
public String amtNickels()
{return ""+two.format(nickels*.05);
}
public String amtPennies()
{return ""+two.format(pennies*.01);
}
public String amtTotal()
{return ""+two.format(quarters*.75+dimes*.10+nickels*01+pennies*.01);
}
}