*Please fill out TODO 1~9 import java.text.DecimalFormat; import java.util.Array
ID: 3667818 • Letter: #
Question
*Please fill out TODO 1~9
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class CS225_Lab4
{
private final Scanner scan = new Scanner(System.in);
private final DecimalFormat df = new DecimalFormat("$#,##0.00");
private final double [] cashPrizes = {.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000, 5000, 10000, 100000, 500000, 1000000};
private ArrayList<Double> remainingPrizesBoard;
private boolean gameIsOver;
private ArrayList<Double> offerHistory;
/////////////////////////////////////////////////////////////////////
// The constructor initializes remainingPrizesBoard to a new board
// containing a random ordering of all the prizes.
/////////////////////////////////////////////////////////////////////
CS225_Lab4()
{
System.out.println("Let's play DEAL OR NO DEAL!");
gameIsOver = false; // New game, so game is not over!
initializeRandomPrizeBoard();
// TODO 6: Instantiate offerHistory using the new keyword
}
/////////////////////////////////////////////////////////////////////
// Prints the locations available to choose from
// (0 through numRemainingPrizes-1)
/////////////////////////////////////////////////////////////////////
private void printRemainingPrizeBoard()
{
// Copies remaining prizes ArrayList into prizes ArrayList (a temp ArrayList)
ArrayList<Double> prizes = new ArrayList();
for (double prize : remainingPrizesBoard)
prizes.add(prize);
Collections.sort(prizes);
// TODO 1: Print the prizes in the prize ArrayList. All values should be on
// a single line (put a few spaces after each prize) and add a new line
// at the end (simple for loop)
}
/////////////////////////////////////////////////////////////////////
// Generates the banks offer. The banker algorithm computes the average
// value of the remaining prizes and then offers 85% of the average.
/////////////////////////////////////////////////////////////////////
private double getBankerOffer()
{
// TODO 2: Write code which returns the banker's offer as a double,
// according to the description in this method's comment above.
}
/////////////////////////////////////////////////////////////////////
// Takes in the selected door number and processes the result
/////////////////////////////////////////////////////////////////////
private void chooseDoor(int door)
{
// TODO 7: Add the current bank offer (remember, we have a method
// for this) to the our offerHistory
if (door == -1)
{
// This block is executed when the player accepts the banker's offer. Thus the game is over.
// TODO 3: Set the gameIsOver variable to true
// Inform the user that the game is over and how much money they accepted from the banker.
// Print the offer history (there is a method to call for this).
}
else
{
// This block is executed when the player selects one of the remaining doors.
// TODO 4: Obtain the prize behind the proper door and remove the prize from the board
// Print out which door the user selected and what prize was behind it (this prize is now gone)
}
// If only one prize remaining, game is over!!!
if (remainingPrizesBoard.size() == 1)
{
// This block is executed when there is only one more prize remaining...so it is what they win!
// TODO 5: Set the gameIsOver variable to true
// Let the user know what prize they won behind the final door!
// Print the offer history (there is a method to call for this).
}
}
/////////////////////////////////////////////////////////////////////
// This method is called when the game is over, and thus takes as a
// parameter the prize that was accepted (either from the banker's offer
// or the final door).
//
// Prints out the offers made by the banker in chronological order.
// Then, prints out the money left on the table (for example, if the
// largest offer from the banker was $250,000, and you ended up winning
// $1,000, whether from a later banker offer or from the last door,
// then you "left $249,000 on the table).
/////////////////////////////////////////////////////////////////////
private void printOfferHistory(double acceptedPrize)
{
// Print out the history of offers from the banker
// TODO 8: Print out the banker offer history (will need to loop through
// your offerHistory member variable) and find the max offer made.
// Print one offer per line, like:
// Offer 1: $10.00
// Offer 2: $5.00
// .....
// TODO 9: If the max offer was greater than the accepted prize, then print out
// the $$$ left out on the table (see the example in this method's header above).
// Otherwise, congratulate the user that they won more than the banker's max
// offer and display the banker's max offer.
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////DO NOT EDIT ANY PORTIONS OF METHODS BELOW///////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Processes all the code needed to execute a single turn of the game
/////////////////////////////////////////////////////////////////////
public void playNextTurn()
{
System.out.println("-----------------------------------------------------------------------");
// Print out remaining prizes
System.out.println("There are " + remainingPrizesBoard.size() + " prizes remaining, listed in ascending order: ");
printRemainingPrizeBoard();
// Display all prize doors
System.out.println(" The prizes have been dispersed randomly behind the following doors:");
for (int i = 0; i < remainingPrizesBoard.size(); i++)
System.out.print(i + " ");
System.out.println();
// Print out banker's offer and ask user what to do
System.out.println("The banker would like to make you an offer of...................." + df.format(getBankerOffer()));
System.out.print("What would you like to do? Enter '-1' to accept the banker's offer, " +
"or select one of the doors above (0-" + (remainingPrizesBoard.size()-1) + "): ");
// Get selection from user and choose door
int selectedDoorNum = scan.nextInt();
if (selectedDoorNum >= -1 && selectedDoorNum < remainingPrizesBoard.size()) // Make sure valid selection
chooseDoor(selectedDoorNum);
else
System.out.println(selectedDoorNum + " is not a valid selection.");
System.out.println();
System.out.println();
}
/////////////////////////////////////////////////////////////////////
// Basically, a getter method for the gameIsOver method. The client
// will continually call playNextTurn() until gameIsOver() evaluates
// to true.
/////////////////////////////////////////////////////////////////////
public boolean gameIsOver()
{
return gameIsOver;
}
/////////////////////////////////////////////////////////////////////
// Copies the constant prizes (from an array) into a temporary array
// and uses that array to populate the initial board into the member
// variable 'remainingPrizesBoard'.
/////////////////////////////////////////////////////////////////////
private void initializeRandomPrizeBoard()
{
// Start with a fresh board with nothing in it
remainingPrizesBoard = new ArrayList<Double>();
// Copies cashPrizes array into prizes ArrayList (a temp ArrayList)
ArrayList<Double> prizes = new ArrayList();
for (double prize : cashPrizes)
prizes.add(prize);
// Randomizes prizes into remainingPrizesBoard
while (!prizes.isEmpty())
{
Random r = new Random();
int i = r.nextInt(prizes.size()); // Pick a random remaining prize
remainingPrizesBoard.add(prizes.get(i)); // Copy into our "board"
prizes.remove(i);
}
// Debug print which will show the random board contents
//for (double d : remainingPrizesBoard)
// System.out.print(df.format(d) + " -- ");
//System.out.println();
}
}
Explanation / Answer
Updated code: My changes are in bold
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class CS225_Lab4
{
private final Scanner scan = new Scanner(System.in);
private final DecimalFormat df = new DecimalFormat("$#,##0.00");
private final double [] cashPrizes = {.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000, 5000, 10000, 100000, 500000, 1000000};
private ArrayList<Double> remainingPrizesBoard;
private boolean gameIsOver;
private ArrayList<Double> offerHistory;
/////////////////////////////////////////////////////////////////////
// The constructor initializes remainingPrizesBoard to a new board
// containing a random ordering of all the prizes.
/////////////////////////////////////////////////////////////////////
CS225_Lab4()
{
System.out.println("Let's play DEAL OR NO DEAL!");
gameIsOver = false; // New game, so game is not over!
initializeRandomPrizeBoard();
// TODO 6: Instantiate offerHistory using the new keyword
offerHistory = new ArrayList<Double>();
}
/////////////////////////////////////////////////////////////////////
// Prints the locations available to choose from
// (0 through numRemainingPrizes-1)
/////////////////////////////////////////////////////////////////////
private void printRemainingPrizeBoard()
{
// Copies remaining prizes ArrayList into prizes ArrayList (a temp ArrayList)
ArrayList<Double> prizes = new ArrayList();
for (double prize : remainingPrizesBoard)
prizes.add(prize);
Collections.sort(prizes);
// TODO 1: Print the prizes in the prize ArrayList. All values should be on
// a single line (put a few spaces after each prize) and add a new line
// at the end (simple for loop)
for (double prize : prizes)
System.out.print(df.format(prize) + " ");
System.out.println();
}
/////////////////////////////////////////////////////////////////////
// Generates the banks offer. The banker algorithm computes the average
// value of the remaining prizes and then offers 85% of the average.
/////////////////////////////////////////////////////////////////////
private double getBankerOffer()
{
// TODO 2: Write code which returns the banker's offer as a double,
// according to the description in this method's comment above.
double avg = 0.0;
double offer;
for (double prize : remainingPrizesBoard)
avg += prize;
avg /= remainingPrizesBoard.size();
offer = 85 * avg / 100;
return offer;
}
/////////////////////////////////////////////////////////////////////
// Takes in the selected door number and processes the result
/////////////////////////////////////////////////////////////////////
private void chooseDoor(int door)
{
// TODO 7: Add the current bank offer (remember, we have a method
// for this) to the our offerHistory
double offer = getBankerOffer();
offerHistory.add(offer);
if (door == -1)
{
// This block is executed when the player accepts the banker's offer. Thus the game is over.
// TODO 3: Set the gameIsOver variable to true
// Inform the user that the game is over and how much money they accepted from the banker.
// Print the offer history (there is a method to call for this).
gameIsOver = true;
System.out.println("The game is over since you accepted the banker's offer");
System.out.println("You accepted $" + offer + " offer from Banker");
System.out.println("The offer history is :");
printOfferHistory(offer);
}
else
{
// This block is executed when the player selects one of the remaining doors.
// TODO 4: Obtain the prize behind the proper door and remove the prize from the board
// Print out which door the user selected and what prize was behind it (this prize is now gone)
double prize = remainingPrizesBoard.get(door);
remainingPrizesBoard.remove(door);
System.out.println("The door you have selected is " + door + " and the prize behind it is $" + prize);
}
// If only one prize remaining, game is over!!!
if (remainingPrizesBoard.size() == 1)
{
// This block is executed when there is only one more prize remaining...so it is what they win!
// TODO 5: Set the gameIsOver variable to true
// Let the user know what prize they won behind the final door!
// Print the offer history (there is a method to call for this).
gameIsOver = true;
System.out.println("There is only one door remaining and the prize behind is $" + remainingPrizesBoard.get(0));
printOfferHistory(remainingPrizesBoard.get(0));
}
}
/////////////////////////////////////////////////////////////////////
// This method is called when the game is over, and thus takes as a
// parameter the prize that was accepted (either from the banker's offer
// or the final door).
//
// Prints out the offers made by the banker in chronological order.
// Then, prints out the money left on the table (for example, if the
// largest offer from the banker was $250,000, and you ended up winning
// $1,000, whether from a later banker offer or from the last door,
// then you "left $249,000 on the table).
/////////////////////////////////////////////////////////////////////
private void printOfferHistory(double acceptedPrize)
{
// Print out the history of offers from the banker
// TODO 8: Print out the banker offer history (will need to loop through
// your offerHistory member variable) and find the max offer made.
// Print one offer per line, like:
// Offer 1: $10.00
// Offer 2: $5.00
// .....
int i = 1;
double maxOffer = 0.0;
for(double offer : offerHistory)
{
System.out.println("Offer "+i+": $"+df.format(offer));
if(maxOffer < offer)
maxOffer = offer;
i++;
}
// TODO 9: If the max offer was greater than the accepted prize, then print out
// the $$$ left out on the table (see the example in this method's header above).
if(acceptedPrize < maxOffer)
System.out.println("Ypou left $" + (maxOffer - acceptedPrize) + " on the table");
// Otherwise, congratulate the user that they won more than the banker's max
// offer and display the banker's max offer.
else
{
System.out.printnln("Congratulations!! You have won more than the banker's max offer.");
System.out.println("Banker's max offer is $"+maxOffer);
}
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////DO NOT EDIT ANY PORTIONS OF METHODS BELOW///////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Processes all the code needed to execute a single turn of the game
/////////////////////////////////////////////////////////////////////
public void playNextTurn()
{
System.out.println("-----------------------------------------------------------------------");
// Print out remaining prizes
System.out.println("There are " + remainingPrizesBoard.size() + " prizes remaining, listed in ascending order: ");
printRemainingPrizeBoard();
// Display all prize doors
System.out.println(" The prizes have been dispersed randomly behind the following doors:");
for (int i = 0; i < remainingPrizesBoard.size(); i++)
System.out.print(i + " ");
System.out.println();
// Print out banker's offer and ask user what to do
System.out.println("The banker would like to make you an offer of...................." + df.format(getBankerOffer()));
System.out.print("What would you like to do? Enter '-1' to accept the banker's offer, " +
"or select one of the doors above (0-" + (remainingPrizesBoard.size()-1) + "): ");
// Get selection from user and choose door
int selectedDoorNum = scan.nextInt();
if (selectedDoorNum >= -1 && selectedDoorNum < remainingPrizesBoard.size()) // Make sure valid selection
chooseDoor(selectedDoorNum);
else
System.out.println(selectedDoorNum + " is not a valid selection.");
System.out.println();
System.out.println();
}
/////////////////////////////////////////////////////////////////////
// Basically, a getter method for the gameIsOver method. The client
// will continually call playNextTurn() until gameIsOver() evaluates
// to true.
/////////////////////////////////////////////////////////////////////
public boolean gameIsOver()
{
return gameIsOver;
}
/////////////////////////////////////////////////////////////////////
// Copies the constant prizes (from an array) into a temporary array
// and uses that array to populate the initial board into the member
// variable 'remainingPrizesBoard'.
/////////////////////////////////////////////////////////////////////
private void initializeRandomPrizeBoard()
{
// Start with a fresh board with nothing in it
remainingPrizesBoard = new ArrayList<Double>();
// Copies cashPrizes array into prizes ArrayList (a temp ArrayList)
ArrayList<Double> prizes = new ArrayList();
for (double prize : cashPrizes)
prizes.add(prize);
// Randomizes prizes into remainingPrizesBoard
while (!prizes.isEmpty())
{
Random r = new Random();
int i = r.nextInt(prizes.size()); // Pick a random remaining prize
remainingPrizesBoard.add(prizes.get(i)); // Copy into our "board"
prizes.remove(i);
}
// Debug print which will show the random board contents
//for (double d : remainingPrizesBoard)
// System.out.print(df.format(d) + " -- ");
//System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.