JAVA HELP Create a Coin class with the following information

**********JAVA HELP***********

Create a Coin class with the following information:

- A double field for the value of the coin

- A boolean field indicating the side of the coin that is facing up- heads (true) or tails (false)

- A constructor that assigns values to both fields to begin.

- An accessor method that returns the side of the coin that is facing up

- A method called toss, which randomly determines the side of the coin that is facing up.

Write a seperate main class that contains a main method. In the main, we will play a game with the user in which we will toss all three coins and add up their values. Keep tossing the coins until the value exceeds $1. If they are able to get exactly $1 they win the game. To begin, create 3 coin objects, giving one a value of 25c, one a value of 10c and another a value of 5c. Next, ask the user if they wish to start a game. If yes, toss all three coins and if they fall heads up, add their value to the total. If the total is below $1, toss all three coins again. Output the result and total after each toss, and output if they won or busted. Then ask if they wish to play again, and if yes, reset the totals and repeat the tossing process.

Solution

Coin.java

import java.util.Random;

public class Coin {
  
//Declaring instance variables
private double value;
private boolean side;

//Parameterized constructor
public Coin(double value) {
   super();
   this.value = value;
   this.side =false;
}

//setters and getters
public double getValue() {
   return value;
}
public void setValue(double value) {
   this.value = value;
}
public boolean isSide() {
   return side;
}
public void setSide(boolean side) {
   this.side = side;
}

/* This method will toss the coin and returns \"true\" if the outcome is \"head\"
* returns \"false\" if the outcome is \"tail\"
*/
public boolean toss()
{
   int random;
   Random r = new Random();
   random=r.nextInt(2) ;
   if(random==0)
   {
       return false;
   }
   else
   return true;
  
   }
}

__________________________________

MainClass.java (This class contains main() method .so run this program )

import java.util.Scanner;

public class MainClass {

   public static void main(String[] args) {

       // Declaring variables
       double sum = 0.0;
       boolean coin1;
       boolean coin2;
       boolean coin3;

       // Scanner class object is used to read the input entered by the user
       Scanner sc = new Scanner(System.in);

       // getting the input \'Y\' or \'N\' entered by the user
       System.out.print(\"Do you want to Start the game(Y/N) ::\");
       char ch1 = sc.next(\".\").charAt(0);

       if (ch1 == \'y\' || ch1 == \'Y\') {
           while (true) {
               // Creating three coins class objects
               Coin c1 = new Coin(25);
               Coin c2 = new Coin(10);
               Coin c3 = new Coin(5);

               // calling the toss() method on the Coin class object
               coin1 = c1.toss();
               if (coin1 == false) {
                   c1.setValue(0);
               }
               // calling the toss() method on the Coin class object
               coin2 = c2.toss();
               if (coin2 == false) {
                   c2.setValue(0);
               }
               // calling the toss() method on the Coin class object
               coin3 = c3.toss();
               if (coin3 == false) {
                   c3.setValue(0);
               }
               // Calculating the sym
               sum += c1.getValue() + c2.getValue() + c3.getValue();

               if (sum < 100) {
                   System.out.println(\"Sum :\" + sum);
                   continue;
               } else if (sum == 100) {
                   System.out.println(\"Sum :\" + sum);
                   System.out.println(\":: You Wins ::\");
               } else {
                   System.out.println(\"Sum :\" + sum);
                   System.out.println(\":: Burst ::\");
                   sum = 0;
               }

               // Asking user if he want to play again.
               System.out.print(\"Do you want to continue(Y/N) ::\");
               char ch = sc.next(\".\").charAt(0);
               if (ch == \'Y\' || ch == \'y\')
                   continue;
               else {
                   System.out.println(\":: Program Exit ::\");
                   break;
               }
           }

       } else {
           System.out.println(\":: Game Exit ::\");
       }

   }

}

_______________________________________

Output:

Do you want to Start the game(Y/N) ::Y
Sum :10.0
Sum :35.0
Sum :40.0
Sum :45.0
Sum :45.0
Sum :85.0
Sum :110.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :15.0
Sum :30.0
Sum :30.0
Sum :65.0
Sum :80.0
Sum :85.0
Sum :95.0
Sum :100.0
:: You Wins ::
Do you want to continue(Y/N) ::Y
Sum :125.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :5.0
Sum :45.0
Sum :45.0
Sum :55.0
Sum :80.0
Sum :120.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :0.0
Sum :35.0
Sum :75.0
Sum :75.0
Sum :115.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :35.0
Sum :75.0
Sum :90.0
Sum :90.0
Sum :105.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :0.0
Sum :30.0
Sum :55.0
Sum :80.0
Sum :95.0
Sum :135.0
:: Burst ::
Do you want to continue(Y/N) ::Y
Sum :35.0
Sum :75.0
Sum :80.0
Sum :85.0
Sum :100.0
:: You Wins ::
Do you want to continue(Y/N) ::N
:: Program Exit ::

____________________Thank You

**********JAVA HELP*********** Create a Coin class with the following information: - A double field for the value of the coin - A boolean field indicating the s
**********JAVA HELP*********** Create a Coin class with the following information: - A double field for the value of the coin - A boolean field indicating the s
**********JAVA HELP*********** Create a Coin class with the following information: - A double field for the value of the coin - A boolean field indicating the s
**********JAVA HELP*********** Create a Coin class with the following information: - A double field for the value of the coin - A boolean field indicating the s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site