Create the variables and methods needed for this class A Dic
Create the variables, and methods needed for this class:
A DicePlayer is a class that represents the player in a Craps game. A DicePlayer is a data class. The data we need to manage (the fields or class variables) are the players name and how much money the player has to bet.
Methods are needed to manage this data.
Solution
import java.util.Scanner; // inporting required packages
class DicePlayer
{
private String name; // creating requried variables
private double cash;
Scanner sc=new Scanner(System.in); // scanner variable to take i/p\'s
public void setName()
{
System.out.println(\"Enter the name of the player\ \");
name=sc.next(); // taking player name
}
public void setCash ()
{
System.out.println(\"Give the amount on the hand\ \");
cash=sc.nextDouble(); // taking cash
}
public String getName()
{
return name; // getting name when needed
}
public double getCash()
{
return cash; // getting cash when needed
}
public boolean addCash()
{
double add;
System.out.println(\"Give the amount to add\ \");
add=sc.nextInt();
if (add>0) // checking the taken value is non-negative
{
cash=cash+add; // if yes add it to cash
return true;
}
else
return false; // else retrun false
}
public boolean subCash()
{
double sub;
System.out.println(\"Give the amount to sub\ \");
sub=sc.nextInt();
if (sub>0 && sub<cash) // checking given value is non-negative and less than cash
{
cash=cash-sub; // if yes remove from cash
return false;
}
else
return true; // else return false
}
}
