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

Main topics: Loop Statements Library in Methods User Defined Methods Just focus

ID: 3688092 • Letter: M

Question

Main topics:

Loop Statements

Library in Methods

User Defined Methods

Just focus on these topics, please. I haven`t learned anything past these topics. It can`t have any complex things in it.

Program Specification:

Write a Java program that allows the user to play roulette. In our version of roulette, the roulette wheel

has the following characteristics:

• Slots are numbered from 0 to 36

• Even numbered slots are colored Red

• Odd numbered slots are colored Black

• The 0 slot is colored Green

The user starts with 100 chips, and each round can place one the following two types of bets - or quit

(cash out):

• Bet on a specific number [0, 36] (payout is 35:1)

• Bet on either Red or Black color (payout is 1:1)

• Cash out

Your program should behave as follows:

• Display a welcome message

• Repeatedly do the following:

– Ask the user to choose a bet type (or cash out)

if number bet or color bet

· Ask the user for the number - if number bet

· Ask the user for color - if color bet

Ask the user to choose an amount to bet (between 1 chip and the total number of chips the

user currently has), and record the bet amount

Spin the wheel and report the number and color

Inform the use of their win/loss status, and updated the user’s chip total appropriately

Until the user chooses to cash out or has no more chips left to bet

• Once the user chooses to cash out a final report is displayed indicating the user’s winnings or loses

(relative to their starting with 100 chips).

Rules and Requirements:

• All user input must be validated

• Given the following method heading, you must write the corresponding definition for a void return

method that displays a welcome message containing the bet types and payouts to the screen.

public static void welcome()

• Given the following method heading, you must write the corresponding definition for a int return

method that displays a menu and reads, validates, and returns the user’s choice to place a specific

bet type or cash out.

public static int getMenuChoice(Scanner stdIn)

• Given the following method heading, you must write the corresponding definition for a int return

method that reads, validates, and returns a number to bet on, which must be between 0 and 36.

public static int getNumber(Scanner stdIn)

• Given the following method heading, you must write the corresponding definition for a String return

method that reads, validates, and returns a color to bet on, which must be "Red" or "Black".

public static String getColor(Scanner stdIn)

• Given the following method heading, you must write the corresponding definition for a int return

method that takes the number of chips the user currently has: chipsNow as its only input. Then

reads, validates, and returns a bet amount, which must be between 1 and chipsNow.

public static int getBet(Scanner stdIn, int chipsNow)

• Given the following method heading, you must write the corresponding definition for a String return

method that takes the number just spun on the wheel : spinNum as its only input. Then determines

and returns the spin color based on spinNum. i.e. "Red", "Black", or "Green"

public static String determineColor(int spinNum)

• Given the following method heading, you must write the corresponding definition for a void return

method that takes the number of chips the user currently has: chipsNow as its only input. Then

displays the user’s winnings or loses (relative to their starting with 100 chips) to the screen.

public static void report(int chipsNow)

Notes and Tips:

• Assuming number is an int variable, the following expression will evaluate to true if number is even:

(number % 2 == 0)

• No input validation should take place in the main method.

• Try a bottom-up approach, in which you first implement all of the non-main methods, and then

provide an implementation for the main method.

• It may be helpful to implement a single wheel spin first, and then allow the user to play multiple

spins next.

Sample run(s):

############################

# WELCOME TO ROULETTE #

############################

# NUMBER BETS PAYOUT: 35:1 #

# COLOR BETS PAYOUT: 1:1 #

############################

You have 100 chips

1. Pick a number to bet on

2. Pick a color to bet on

3. Cash out

Enter a choice [1-3]: 0

Enter a choice [1-3]: 4

Enter a choice [1-3]: 1

Enter the number to bet on [0-36]: -1

Enter the number to bet on [0-36]: 37

Enter the number to bet on [0-36]: 0

Enter the number of chips to bet [1-100]: 0

Enter the number of chips to bet [1-100]: 101

Enter the number of chips to bet [1-100]: 5

Spinng the wheel ...

Spin number: 31

Spin color : Black

Sorry, you lost

You now have 95 chips

1. Pick a number to bet on

2. Pick a color to bet on

3. Cash out

Enter a choice [1-3]: 2

Enter the color to bet on [Red or Black]: umm

Enter the color to bet on [Red or Black]: red

Enter the number of chips to bet [1-95]: 50

Spinng the wheel ...

Spin number: 16

Spin color: Red

Congrats, you won!

You now have 145 chips

1. Pick a number to bet on

2. Pick a color to bet on

3. Cash out

Enter a choice [1-3]: 3

Thanks for playing, you won a total of 45 chips

Explanation / Answer

package assignment;

import java.util.Random;
import java.util.Scanner;

public class RouleteeWheel {
   private int chips;

   public RouleteeWheel() {
       chips = 100;
   }
   public static void main(String[] args) {
       Scanner stdIn = new Scanner(System.in);
       RouleteeWheel wheel = new RouleteeWheel();
       RouleteeWheel.welcome();
       int option = 0;
       int number = -1;
       String color = null;
       int bet = 0;
       int wheelnum = 0;
       do {
           if(wheel.getChips() <= 0)
               break;
           System.out.println("You have "+wheel.getChips()+" chips ");
           option = RouleteeWheel.getMenuChoice(stdIn);
           if(option == 1) {
               number = RouleteeWheel.getNumber(stdIn);
           } else if(option == 2) {
               color = RouleteeWheel.getColor(stdIn);
           }
           if(option == 1 || option == 2) {
               bet = RouleteeWheel.getBet(stdIn, wheel.getChips());
               System.out.println("Spinning the wheel..");
               wheelnum = RouleteeWheel.randNum();
               System.out.println("Spin number: "+wheelnum+" Spin color: "+RouleteeWheel.determineColor(wheelnum));
               if(option == 1 ) {
                   if(wheelnum == number) {
                       wheel.setChips(wheel.getChips()+ 35 * bet);
                   } else
                       wheel.setChips(wheel.getChips() -   bet);
               }
               if(option == 2 ){
                   if(color.equalsIgnoreCase(RouleteeWheel.determineColor(wheelnum)))
                       wheel.setChips(wheel.getChips() + bet);
                   else
                       wheel.setChips(wheel.getChips() - bet);
               }
           }
       } while(option != 3 );
   }

  
   public int getChips() {
       return chips;
   }
   public void setChips(int chips) {
       this.chips = chips;
   }
  
   public static void welcome() {
       System.out.println("############################" +

       "# WELCOME TO ROULETTE #" +

       "############################" +

       "# NUMBER BETS PAYOUT: 35:1 #" +

       "# COLOR BETS PAYOUT: 1:1 #" +

       "############################");
   }

   public static int getMenuChoice(Scanner stdIn) {
       System.out.println(

       "1. Pick a number to bet on " +

       "2. Pick a color to bet on " +

       "3. Cash out");

       int option = 0;
       do {
           System.out.println("Enter a choice [1-3]:");
           option = stdIn.nextInt();
       } while (option < 1 || option > 3);

       return option;
   }
  
   public static int getNumber(Scanner stdIn) {
       int option = -1;
       do {
           System.out.println("Enter the number to bet on [0-36]:");
           option = stdIn.nextInt();
       } while (option < 0 || option > 36);

       return option;
   }
  
   public static String getColor(Scanner stdIn) {
       String option = null;
       do {
           System.out.println("Enter the color to bet on [Red or Black]:");
           option = stdIn.next();
           if("red".equalsIgnoreCase(option.trim()) || "black".equalsIgnoreCase(option.trim()))
                   break;
       } while (option == null || !"red".equalsIgnoreCase(option.trim()) || !"black".equalsIgnoreCase(option.trim()));

       return option;
   }
  
   public static int getBet(Scanner stdIn, int chipsNow) {
       int option = -1;
       do {
           System.out.println("Enter the number of chips to bet [1-"+chipsNow+"]:");
           option = stdIn.nextInt();
       } while (option < 1 || option > chipsNow);

       return option;
   }
  
   public static int randNum() {
       int max = 36;
       int min = 0;
       Random rand = new Random();
       return rand.nextInt((max - min) + 1) + min;
   }
  
   public static String determineColor(int spinNum) {
       if(spinNum == 0)
           return "Green";
       if(spinNum % 2 == 0)
           return "Red";
       else
           return "Black";
   }
}


=====output=======================

############################# WELCOME TO ROULETTE ############################## NUMBER BETS PAYOUT: 35:1 ## COLOR BETS PAYOUT: 1:1 #############################
You have 100 chips

1. Pick a number to bet on
2. Pick a color to bet on
3. Cash out
Enter a choice [1-3]:
1
Enter the number to bet on [0-36]:
5
Enter the number of chips to bet [1-100]:
10
Spinning the wheel..
Spin number: 4
Spin color: Red
You have 90 chips

1. Pick a number to bet on
2. Pick a color to bet on
3. Cash out
Enter a choice [1-3]:
2
Enter the color to bet on [Red or Black]:
Black
Enter the number of chips to bet [1-90]:
15
Spinning the wheel..
Spin number: 27
Spin color: Black
You have 105 chips

1. Pick a number to bet on
2. Pick a color to bet on
3. Cash out
Enter a choice [1-3]:
3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote