Question

In: Computer Science

The code is needed in Java. There are many ways to design a solution to this...

The code is needed in Java.

  1. There are many ways to design a solution to this problem but you are evaluated on meeting the specific specifications as given in this quiz.
  2. Use proper statement indentation and meaningful variable names in the code. (3 points)
  3. Place a multi-line comment (not multiple single line comments) giving a description of what this application does before the class header. Include your name and the date with the description. (2 points)
  4. Output spacing, formatting, and spelling are to match the example console output illustrated above, but of course the user input will be different. (5 points)
  5. Add a meaningful comment for every statement explaining why the statement is in the code. (5 points)
  6. Prompt the user for data.
  7. Validate the data so only acceptable values will be processed. If the user enters invalid data, the application should display the appropriate error message and prompt the user again until the user enters valid data. See example console display above. (5 points)
  8. The user will have the option of running the application again with all new input values. When the user responds to the “Play Again?” prompt, the application should only accept a value of “yes” or “no”. (5 points)
  9. If the user enters invalid data at any time in the application, the application should display an appropriate error message and prompt the user again until the user enters valid data. See example console display test above.
  10. Create and use an interface with an abstract method called printPlayers that does not return any values. This interface will be implemented in the class with the main method and the other methods listed below. (4 points)
  11. Create a class named Player with four instance variables. One is for the player’s name, the second is for the amount of money the player has, the third is an array of guesses, and the fourth is for the wager amount. (4 points)
  12. Create getter and setter methods for the instance variables. (4 points)
  13. Create two Player constructors. One will have no parameters and one will have the name and the amount of money parameters. (4 points)
  14. You will use this class in the application to create Player objects. (5 points)
  15. Use the Console class from chapter 7 in the textbook to validate user entered data. (4 points)
  16. Use an ArrayList to store Player objects in the main method. (5 points)
  17. Use a LinkedList to store the three winning numbers. (5 points)
  18. The application must contain and implement these nine methods. Name them whatever you like. The functionality they will perform in the application is summarized below:
    • main method- This method will declare and initialize necessary entities and control execution of the code. Anything not handled is the other methods will be accomplished by the main method. (5 points)
    • random number generator method- This method will generate a value between 0 and 20 inclusive. (3 points)
    • Initial welcome display and initial user input method - This method will display the welcome information and will prompt, accept, and validate the user's response to the first prompt . (2 points)
    • winner method - This method determines if the player wins or loses, and displays the appropriate information to the user. (5 points)
    • User wage validation and input method- This method will display the wagering information and will prompt, accept, and validate the user’s wager. (5 points)
    • User guess validation and input method- This method will display the guessing prompt and will prompt, accept, and validate the user’s three guess. (5 points)
    • Create player method – This method will prompt, accept, and validate the data needed to create a player object. (5 points)
    • Play again method– This method will prompt, accept, and validate the data needed to continue playing with brand new players from the beginning and print appropriate information in response. A call to the printPlayer method will print the players and their ending balances. (5 points)
    • printPlayer method – This method will print the players' names and their ending balances. (5 points)

Console Example

Welcome to Casino Royal

++++++++++++++++++++++++

How many players are in the game? 2

Enter a player’s name: Rocky

How much money is Rocky willing to put on the table? 200

Enter a player’s name: Donna

How much money is Donna willing to put on the table? 1500

Rocky, you must play three numbers at a time.

The valid numbers are between 0 and 20 inclusive.

Rocky, enter your number? xyz

Error! Invalid integer. Try again.

Rocky, enter your number? 100

Error! Number must be less than 21.

Rocky, enter your number? -10

Error! Number must be greater than -1.

Rocky, enter your number? 0

Rocky, enter your number? 10

Rocky, enter your number? 20

Donna, you must play three numbers at a time.

The valid numbers are between 0 and 20 inclusive.

Donna, enter your number? 1

Donna, enter your number? 2

Donna, enter your number? 3

Rocky, you can now place a single bet of any size on these numbers

as long as you have that much money on the table.

Rocky, how much do you wager? 1.99

Donna, you can now place a single bet of any size on these numbers

as long as you have that much money on the table.

Donna, how much do you wager? 3000

Donna, you did not have $3,000.00 on the table to bet. Try a lower wager.

Donna, how much do you wager? -10

Donna, you cannot wager less than 0 dollars. Try a higher wager.

Donna, how much do you wager? quit

Invalid input. You must enter a number to play. Try again.

Donna, how much do you wager? 100

Very good. Spinning the wheel.....

The winning numbers are: 12, 14, and 5

Sorry Rocky, you lose. You have $198.01 left on the table.

The winning numbers are: 12, 14, and 5

Sorry Donna, you lose. You have $1,400.00 left on the table.

Play Again? (yes/no) y

You must enter yes or no. Try again.

Do you want to continue (yes/no)? nope

You must enter yes or no. Try again.

Do you want to continue (yes/no)? yes

These are the players and their ending balances:

Rocky's balance is $198.01

Donna's balance is $1400.0

Take your money off the table when you leave. Come back soon.

Welcome to Casino Royal

++++++++++++++++++++++++

How many players are in the game? 1

Enter a player’s name: Jane Doe

How much money is Jane willing to put on the table? 9999

Jane, you must play three numbers at a time.

The valid numbers are between 0 and 20 inclusive.

Jane, enter your number? 5

Jane, enter your number? 8

Jane, enter your number? 17

Jane, you can now place a single bet of any size on these numbers

as long as you have that much money on the table.

Jane, how much do you wager? 9999

Very good. Spinning the wheel.....

The winning numbers are: 4, 14, and 9

Sorry Jane, you lose. You have $0.00 left on the table.

Play Again? (yes/no) no

These are the players and their ending balances:

Jane's balance is $0.0

Take your money off the table when you leave. Come back soon.

Solutions

Expert Solution

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

class Player
{
private String name;
private double amount,wagerAmount;
private int[]guess;

public Player()
{
this.name="";
this.amount=0;
this.guess=new int[3];
this.wagerAmount=0;
}

public Player(String name, double amount) {
this.name = name;
this.amount = amount;
}

public String getName() {
return name;
}

public double getAmount() {
return amount;
}

public double getWagerAmount() {
return wagerAmount;
}

public int[] getGuess() {
return guess;
}

public void setName(String name) {
this.name = name;
}

public void setAmount(double amount) {
this.amount = amount;
}

public void setWagerAmount(double wagerAmount) {
this.wagerAmount = wagerAmount;
}

public void setGuess(int[] guess) {
this.guess = guess;
}
  
  
  
  
}
public class Main
{
public static void main(String[] args)
{
Scanner scnr =new Scanner(System.in);
String userChoce="yes";
NumberFormat nf = NumberFormat.getInstance(new Locale("en", "US"));
int numPlayers;
int winnigNumbers[]=new int[3];
do
{
numPlayers=welcome();
Player players[]=new Player[numPlayers];
for(int i=0;i<numPlayers;i++)
{
players[i]=constructPlayer();
}
for(int i=0;i<numPlayers;i++)
{
System.out.println(players[i].getName()+", you must play three numbers at a time.\n" +
"\n" +
"The valid numbers are between 0 and 20 inclusive.");
getBattingNumber(players[i]);
}
for(int i=0;i<numPlayers;i++)
{
players[i].setWagerAmount(getWageAmount(scnr,nf,players[i]));
}
System.out.println("Very good. The wheel is spinning.");
for(int i=0;i<3;i++)
winnigNumbers[i]=genrateRandom();
for(int i=0;i<numPlayers;i++)
displayResult(winnigNumbers,nf,players[i]);
  
userChoce=askContinue(scnr);
}while(userChoce.equalsIgnoreCase("yes"));
System.out.println("\nTake your money off the table when you leave. Come back soon.");
}

public static int genrateRandom()
{
// create instance of Random class
Random rand = new Random();
int max=10,min=0;
return min + rand.nextInt(max - min + 1);
}
public static void getBattingNumber(Player player)
{
Scanner scnr =new Scanner(System.in);
int battingNum[]=new int[3];
for(int i=0;i<battingNum.length;i++)
{
while(true)
{
System.out.print(player.getName()+", enter your number? ");
try
{
battingNum[i]=scnr.nextInt();
if( battingNum[i]>=0 && battingNum[i]<21)
break;
else if(battingNum[i]>20)
System.out.println("Error! Number must be less than 21.");
else if(battingNum[i]<0)
System.out.println("Error! Number must be greater than -1.");
}   
catch(Exception e)
{   
System.out.println("Error! Invalid integer. Try again.");
}
}
}
player.setGuess(battingNum);

}
public static int welcome()
{
Scanner input =new Scanner(System.in);
System.out.print("Welcome to Casino Royal\n" +
"\n++++++++++++++++++++++++\n" +
"How many players are in the game? ");
int numPlayers=0;
do
{
try
{
numPlayers=input.nextInt();
if(numPlayers>0)
break;
else
System.out.println("Please enter the positive intger.");
}
catch(Exception e)
{   
System.out.println("Error! Invalid integer. Try again.");
}
}while(numPlayers<=0);
  
return numPlayers;
}

private static Player constructPlayer()
{
String name;
double amount;
Scanner input =new Scanner(System.in);
System.out.print("Enter a player’s name: ");
name=input.next();
System.out.print("How much money is "+name+" willing to put on the table?");
amount=input.nextDouble();
return new Player(name, amount);
  
}

private static double getWageAmount(Scanner scnr, NumberFormat nf, Player player)
{
double amount;
System.out.println(player.getName()+", you can now place a single bet of any size on these numbers\n" +   
"as long as you have that much money on the table.");
while(true)
{
System.out.print(player.getName()+", how much do you wager?");
try
{
amount=scnr.nextDouble();
if(amount>0 && amount<=player.getAmount())
break;
else if(amount<0)
System.out.println(player.getName()+", you cannot wager less than 0 dollars. Try a higher wager.");
else if(amount>player.getAmount())
System.out.println(player.getName()+", you did not have $"+nf.format(amount)+" on the table to bet. Try a lower wager.");
}
catch(Exception e)
{   
System.out.println("Invalid input. You must enter a number to play. Try again.");
}
}
return amount;
}

private static void displayResult(int[] winnigNumbers, NumberFormat nf, Player player)
{
System.out.print("The winning numbers are: ");
for(int i=0;i<winnigNumbers.length-1;i++)
System.out.print(winnigNumbers[i]+",");
System.out.println(" and "+winnigNumbers[winnigNumbers.length-1]);
int numMatchig=checkMatching(winnigNumbers,player);
if(numMatchig==0)
{
double remainingAmount=player.getAmount()-player.getWagerAmount();
player.setAmount(remainingAmount);
System.out.println("Sorry "+player.getName()+", you lose. You have $"+nf.format(remainingAmount)+" left on the table.");
}
else if(numMatchig==1)
{
double remainingAmount=player.getAmount()+player.getWagerAmount();
player.setAmount(remainingAmount);
System.out.println("Congratulations you have a winning number. You now have $"+nf.format(remainingAmount)+" on the table.");
}
else if(numMatchig==2)
{
double remainingAmount=player.getAmount()+(2*player.getWagerAmount());
player.setAmount(remainingAmount);
System.out.println("Congratulations you have two winning number. You now have $"+nf.format(remainingAmount)+" on the table.");
}
else if(numMatchig==3)
{
double remainingAmount=player.getAmount()+(3*player.getWagerAmount());
player.setAmount(remainingAmount);
System.out.println("Congratulations you have all winning number. You now have $"+nf.format(remainingAmount)+" on the table.");
}
}

private static int checkMatching(int[] winnigNumbers, Player player)
{
int count=0;
for(int i=0;i<player.getGuess().length;i++)
{
for(int j=0;j<winnigNumbers.length;j++)
if(player.getGuess()[i]==winnigNumbers[j])
{
count++;
break;
}
}
return count;
}

private static String askContinue(Scanner scnr)
{
String ans;
System.out.print("Play Again? (yes/no) ");
ans=scnr.next();
while(true)
{
if(ans.equals("yes") || ans.equals("no"))
return ans;
System.out.println("You must enter yes or no. Try again.");
System.out.print("Do you want to continue (yes/no)?");
ans=scnr.next();
}
}
}

output   


If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required -...
SOLUTION IN JAVA LANGUANGE NEEDED. JAVA LANGUAGE QUESTION. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for...
Design a 2x1 mux and then use as many of them as needed to make a...
Design a 2x1 mux and then use as many of them as needed to make a bigger mux which can be used to compare two 2-bit binary numbers.
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
If the perturbation is a constant potential, how many terms are needed for an exact solution?...
If the perturbation is a constant potential, how many terms are needed for an exact solution? Question 2 options: a) Infinite b) One c) It depends on the nature of the system. d) Zero e) Two
(a) How many grams of CaCl2 are needed to make 798.0 g of a solution that...
(a) How many grams of CaCl2 are needed to make 798.0 g of a solution that is 32.5% (m/m) calcium chloride in water? Note that mass is not technically the same thing as weight, but (m/m) has the same meaning as (w/w). -How many grams of water are needed to make this solution? (b) What is the volume percent % (v/v) of an alcohol solution made by dissolving 147 mL of isopropyl alcohol in 731 mL of water? (Assume that...
This code in java: A typical ball is circular in shape. Many games are played with...
This code in java: A typical ball is circular in shape. Many games are played with ball e.g cricket ball football etc. Impingement a scenario in which you create cricket ball and football or any other game ball. All balls differ in color, size and material
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
How many grams of NaOH are needed to prepare 600.0 mL of a solution with pH...
How many grams of NaOH are needed to prepare 600.0 mL of a solution with pH = 10.00 at 25 °C?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT