Weekend Task - Coin Toss Game - Tossing Coins for a Dollar #23
Replies: 26 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
` } public class CoinToss { } |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Random; } } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Random; } } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.; } public static void main(String args[]) { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`class Coin { } public class Tossinggame { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; class Coin{ } public class CoinGame { } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; class Coin { } public class CoinGame { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Random; } } } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
class Coin {
String sideUp;
Coin() {
toss();
}
void toss() {
int randomm = (int) ((Math.random() * (2)) + 1);
if (randomm == 1)
sideUp = "heads";
else
sideUp = "tails";
System.out.println("The side up is: " + sideUp);
}
String getSideUp() {
return sideUp;
}
}
public class CoinToss {
static double value;
static String cointype;
static double balance;
static void findcoin() {
int c = (int) ((Math.random() * (3)) + 1);
if (c == 1) {
cointype = "quater";
value = .25;
} else if (c == 2) {
cointype = "dime";
value = .15;
} else {
cointype = "nickel";
value = .10;
}
System.out.println("You have selected: " + cointype);
}
static void winning() {
balance += value;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Coin cb = new Coin();
boolean choice = true;
while (choice) {
while (balance < 1) {
System.out.println("Enter 1 to select a coin!");
int n = sc.nextInt();
if (n == 1) {
findcoin();
System.out.println("Press 2 to toss the coin!");
int ct = sc.nextInt();
if (ct == 2) {
cb.toss();
if (cb.sideUp == "heads") {
winning();
}
System.out.println("The balance after this round is: " + balance);
} else {
System.out.println("Wrong input!");
System.exit(0);
}
} else {
System.out.println("Wrong input!");
System.exit(0);
}
}
if (balance == 1)
System.out.println("Yeeeh!!! YOU WIN!!");
else if (balance > 1)
System.out.println("YOU LOSE,BETTER LUCK NEXT TIME!");
System.out.println("Do you want to play again?(Y/N)");
if (sc.next().charAt(0) == 'Y') {
choice = true;
balance = 0;
} else
choice = false;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Weekend Task-Coin Toss GameCoinGame.javaimport java.util.Random;
import java.util.Scanner;
class Coin{
String sideUp;String coinName;double balance=0;
public Coin() {
System.out.println("Coin Tossing Game .........");
}
public Coin(String sideUp, String coinName, double balance) {
this.sideUp = sideUp;
this.coinName = coinName;
this.balance = balance;
}
public void toss()
{
int number=(int) (Math.random()*2);
sideUp=(number==0)?"Head":"Tail";
System.out.println("Toss side :" + sideUp);
}
public String getSideUp() {
return sideUp;
}
public double gameBalance(String coinName)
{
if(coinName.equalsIgnoreCase("Quarter") && sideUp.equalsIgnoreCase("Head"))
{
balance+=0.25;
}
else if(coinName.equalsIgnoreCase("Dime") && sideUp.equalsIgnoreCase("Head"))
{
balance+=0.5;
}
else if(coinName.equalsIgnoreCase("Nickel") && sideUp.equalsIgnoreCase("Head"))
{
balance+=0.30;
}
else if(coinName.equalsIgnoreCase("tailed"))
{
balance=0;
}
return balance;
}
public void gameStatus()
{
if (balance==1)
System.out.println("Congratulations!!! You Won the game......");
else
System.out.println("You lost the game...Better luck next time!!!");
}
}
public class CoinGame {
public static void main(String[] args) {
Coin obj=new Coin();
Scanner sc=new Scanner(System.in);
char ch='y';
while(ch!='n')
{
System.out.println("Enter 1 to toss the coin");
int n=sc.nextInt();
System.out.println("Game Started!");
if(n==1){
obj.toss();
}
else{
System.out.println("Sry You have enetered wrong option....pls enter 1 to toss the coin");
break;
}
System.out.println("Enter Name of the coin : \n1.Nickel \n2.Quarter \n3.Dime");
String name=sc.next();
System.out.println("--------------------------------------");
System.out.println("Coin Name : "+name);
System.out.println("Game balance :"+obj.gameBalance(name));
obj.gameStatus();
System.out.println("Do you want to play again (y/n)?: ");
ch=sc.next().charAt(0);
}
}}Output |
Beta Was this translation helpful? Give feedback.
-
Coin Toss Gameimport java.util.Scanner;
class coin{
String sideUp;
coin(){
}
public void toss(){
int number=(int) (Math.random()*2);
if(number==1)
sideUp="head";
else
sideUp="tail";
}
public String coinname(){
int num=(int) (Math.random()*3);
String coin_name=(num==1)?"quarter":(num==2)?"dime":"nickel";
return coin_name;
}
public String getSideUp() {
return sideUp;
}
}
public class CoinToss {
public static void main (String[] args){
Scanner sc= new Scanner(System.in);
coin game=new coin();
double balance =0.0;char choice='y';
while(choice=='y'){
System.out.println("game is started...");
System.out.println("press any number to toss the coin");
int n=sc.nextInt();
game.toss();
String Sidesup=game.getSideUp();
System.out.println("Toss side is :"+Sidesup);
/* System.out.println("Enter the coin name (quarter/dime/nickel)");
String coinname=sc.next();*/
System.out.println("press any character to get the coin");
char c=sc.next().charAt(0);
String coin_name=game.coinname();
System.out.println("yehh!!..you got " + coin_name+ " coin");
if(coin_name.equalsIgnoreCase("quarter") && Sidesup.equalsIgnoreCase("head"))
balance+=0.25;
else if(coin_name.equalsIgnoreCase("dime") && Sidesup.equalsIgnoreCase("head"))
balance+=0.15;
else if (coin_name.equalsIgnoreCase("nickel") && Sidesup.equalsIgnoreCase("head"))
balance+=0.25;
else
balance=balance;
if(balance==1.0){
System.out.println("balance :"+balance);
System.out.println("You Win!!!");}
else {
System.out.println("balance :"+balance);
System.out.println("Sorry..you lost!!");}
System.out.println("Do you want to continue(y/n)");
choice=sc.next().charAt(0); }
System.out.println("come again...!");
}}output: |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
Write a class named Coin. The Coin class should have the following field:
The Coin class should have the following methods:
Create a game program using the coin class. The program should have three instances of the coin class: one representing a quarter, one representing a dime, and one representing a nickel.
When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails up. The game is over when your balance reaches one dollar or more. If your balance is exactly one dollar, you win the game. You will lose if your balance exceeds one dollar.
Beta Was this translation helpful? Give feedback.
All reactions