Please write in JAVA ONLY Write a class encapsulating the co

Please write in JAVA ONLY!

Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a numbers of nickels, and a number of pennies.

Include a constructor, the accessors and mutators, and method toString.

Also code the following methods: one returning the total amount of money in dollar notation with two significant digits after the decimal point, and others 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 your methods (including your accessors and mutators).

Solution

Hi, Please find my code.

Please let me lnow in case of any issue.

########## Coin.java ###############

public class Coin{

  

   // instance variables

   private int dollars;

   private int quarters;

   private int dimes;

   private int nickels;

   private int pennies;

  

   // patameterized constructors

   public Coin(int d, int q, int dim, int n, int p){

       dollars = d;

       quarters = q;

       dimes = dim;

       nickels = n;

       pennies = p;

   }

   // getters and setters

  

   public int getDollars() {

       return dollars;

   }

   public void setDollars(int dollars) {

       this.dollars = dollars;

   }

   public int getQuarters() {

       return quarters;

   }

   public void setQuarters(int quarters) {

       this.quarters = quarters;

   }

   public int getDimes() {

       return dimes;

   }

   public void setDimes(int dimes) {

       this.dimes = dimes;

   }

   public int getNickels() {

       return nickels;

   }

   public void setNickels(int nickels) {

       this.nickels = nickels;

   }

   public int getPennies() {

       return pennies;

   }

   public void setPennies(int pennies) {

       this.pennies = pennies;

   }

  

   public double getDollarFromQuarters(){

       return (quarters*25)/100.0;

   }

  

   public double getDollarFromDimes(){

       return (dimes*10)/100.0;

   }

  

   public double getDollarFromNickels(){

       return (5*nickels)/100.0;

   }

  

   public double getDollarFromPennies(){

       return pennies/100.0;

   }

  

   public String getTotal(){

       double total = dollars+getDollarFromDimes()+getDollarFromNickels()+

               getDollarFromPennies()+getDollarFromQuarters();

       return \"$\"+String.format(\"%.2f\", total);

   }

}

############ CoinTest.java ###################

public class CoinTest {

  

   public static void main(String[] args) {

      

       Coin coin = new Coin(1, 3, 4, 5, 6);

      

       System.out.println(\"Total: \"+coin.getTotal());

      

       coin.setDimes(45);

      

       System.out.println(\"Total: \"+coin.getTotal());

   }

}

/*

Sample Run:

Total: $2.46

Total: $6.56

*/

Please write in JAVA ONLY! Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number o
Please write in JAVA ONLY! Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number o
Please write in JAVA ONLY! Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number o
Please write in JAVA ONLY! Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site