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

JAVA Write a class, Coin, that has private fields - cents (int), and name (Strin

ID: 3784317 • Letter: J

Question

JAVA

Write a class, Coin, that has private fields - cents (int), and name (String). Provide accessors for cents and name. Provide a default constructor that creates a penny. Provide a constructor that receives the cents value of a coin and creates the appropriate coin with the appropriate cents and name. If invalid cents are received, create a penny in that situation.

Write a main method (CoinTest) that creates 1 coin of each penny, nickle, dime, quarter. Print the worth(in cents) and the name of each coin.

Run the program. Capture the console output.

Put the program code and output at the end of your text file.

Explanation / Answer

Hi Friend, Please find my implementation.

Please let me know in case of any issue.

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

public class Coin {

   private int cents;

   private String name;

  

   public Coin() {

       cents = 1;

       name = "penny";

   }

  

   public Coin(int c) {

      

       if(c <= 0)

           cents = 1;

       else

           cents = c;

      

       name = "penny";

      

       if(c == 5)

           name = "nickle";

       else if(c == 10)

           name = "dime";

       else if(c == 25)

           name = "quarter";

   }

  

   public String getName(){

       return name;

   }

  

   public int getCents(){

       return cents;

   }

}

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

public class CoinTest {

   public static void main(String[] args) {

       // creating Coin object

       Coin coin = new Coin(34);

       System.out.println("Name: "+coin.getName()+", Coin: "+coin.getCents());

       Coin coin1 = new Coin(25);

       System.out.println("Name: "+coin1.getName()+", Coin: "+coin1.getCents());

       Coin coin2 = new Coin(10);

       System.out.println("Name: "+coin2.getName()+", Coin: "+coin2.getCents());

       Coin coin3 = new Coin(14);

       System.out.println("Name: "+coin3.getName()+", Coin: "+coin3.getCents());

   }

}

/*

*

Sample run:

Name: penny, Coin: 34

Name: quarter, Coin: 25

Name: dime, Coin: 10

Name: penny, Coin: 14

*/