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

Code written in Java. Code through step 2 written. Extension 2: Birthday Problem

ID: 3597822 • Letter: C

Question

Code written in Java.

Code through step 2 written.

Extension 2: Birthday Problem (5 points) Procedure To make things simple, let's assume that all months have 31 days. 1. Open the Birthday class, found in the arrays package of the extensions folder. 2. Insert code to obtain from the user the number of people N that will enter the room. 3. For each person, randomly generate a month and day on which that person was born. 4. Using a two-dimensional array that is indexed by month and by day, keep track of the number of people born on that month and day. For example, perhaps you randomly decide the person was born in month 4 and day 28 5. After processing all of the N people, iterate over your array to compute: o For each month, the fraction (or percentage) of people born in that month. o The average of the 12 values you computed for the above o For each day, the fraction (or percentage) of people born on that day, whether in the same or in a different month. o The average of the 31 values you computed for the above The fraction (or percentage) of people born on exactly the same day. Be careful! A 1 entry means just one person was born on that particular day. You are looking for the fraction of people who share a birthday with at least one other person. *Birthday.java 3 1 package arrays; 2 import cse131 . ArgsProcessor ; 4 public class Birthday f 6 public static void main(String[ args) i ArgsProcessor ap new ArgsProcessor(args); 10 int N = ap.nextInt("How many people enter the room?"); 12 13 14 15 16

Explanation / Answer

import java.util.Scanner; public class Game { private Player p1; private Player p2; private Dice dice; private int scoreToWin; void displayGameMenu() { System.out.println(); System.out.println("(1) Start a new game"); System.out.println("(2) Play one round"); System.out.println("(3) Who is leading now?"); System.out.println("(4) Display game help"); System.out.println("(5) Exit game"); System.out.print("Choose an option: "); } void selectGameOption(int optionSelected) { switch (optionSelected) { case 1: this.startNewGame(); break; case 2: this.playOneRound(p1); this.playOneRound(p2); break; case 3: this.whoIsLeading(); break; case 4: this.displayGameInstruction(); break; default: break; } } void startNewGame() { String p1Name; String p2Name; Scanner sc = new Scanner(System.in); System.out.print("Please enter player one name: "); p1Name = sc.nextLine(); System.out.print("Please enter player two name: "); p2Name = sc.nextLine(); System.out.print("Please enter the maximum score required to win: "); scoreToWin = sc.nextInt(); p1 = new Player(p1Name); p2 = new Player(p2Name); dice = new Dice(); } void playOneRound(Player p) { int result; int firstDiceRoll = dice.rollDice(); int secondDiceRoll = dice.rollDice(); if (firstDiceRoll == secondDiceRoll) { result = (firstDiceRoll + secondDiceRoll) * 2; p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points(BONUS DOUBLE POINTS), " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } else { result = (firstDiceRoll + secondDiceRoll); p.setTotalScore(result); System.out.printf("%s rolled %d and %d, " + "and scored %d points, " + "for a total of %d points", p.getName(), firstDiceRoll, secondDiceRoll, result, p.getTotalScore() ); } System.out.println(); } void whoIsLeading() { if (p1.getTotalScore() == p2.getTotalScore()) { System.out.format("Its currently a draw, " + "%s has %d, %s has %d", p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore() ); } else if (p1.getTotalScore() > p2.getTotalScore()) { System.out.printf("%s is leading, %s has %d points, " + "%s has %d points", p1.getName(), p1.getName(), p1.getTotalScore(), p2.getName(), p2.getTotalScore()); } else if (p1.getTotalScore() = scoreToWin && p2.getTotalScore() >= scoreToWin) { System.out.println("Its a draw! Both players have exceeded the score limit"); return true; } else if (p1.getTotalScore() >= scoreToWin && p2.getTotalScore() 5 || optionSelected < 0) { System.out.print("Option entered invalid, please enter a number from 1 to 5: "); optionSelected = sc.nextInt(); } if (optionSelected == 5) { System.out.println("Exiting game"); break; } game.selectGameOption(optionSelected); boolean anyoneWin = game.checkIfAnyoneHasWon(); if (anyoneWin) { System.out.println(); System.out.println("Game ended."); break; } } } }