Question

Question: Write the Main class code for the following application. The first three classes are given...

Question: Write the Main class code for the following application. The first three classes are given below.

Lottery

This lottery app allows users to sign up for an account, choose a random number or two, and then win a certain amount of money if their chosen number matches the number that the game draws. The house keeps the cash proceeds if nobody wins.

The app must repeatedly do the following:

Allow the user to (1) Add Player Account (2) Play (3) Update Bets (4) Save and Exit.   

class Player

  • private final int ID; //ID of this player account
  • private final double cashContributed; //Dollar amount that this player gambled
  • private static double jackpot; //Dollar amount of jackpot available to all players

Hint: A custom constructor for Player must look like this

public Player(int ID, double cashContributed){
this.ID = ID;
this.cashContributed = cashContributed ;
setJackpot(getJackpot() + cashContributed);//The cash contributed by this player is added to jackpot
}

class NormalPlayer extends Player

Write code for a NormalPlayer class that declares the following data member

  • private int numberSelected;//The random number that this player is betting on. Between 0 and 1000.

Hint: A custom constructor for NormalPlayer must look like this

public NormalPlayer(int ID, double cashContributed, int numberSelected){
super(ID, cashContributed);
setNumberSelected(numberSelected );
}

class Whale extends Player

Write code for a Whale class that declares the following data member

  • private int firstNumberSelected;//The first random number that this player is betting on. Between 0 and 1000.
  • private int secondNumberSelected;//The second random number that this player is betting on. Between 0 and 1000.

Hint: A custom constructor for Whale must look like this

public Whale(int ID, double cashContributed, int firstNumberSelected, int secondNumberSelected ){
super(ID, cashContributed);
setFirstNumberSelected(firstNumberSelected );
setSecondNumberSelected(secondNumberSelected );
}

Tester class

Write code for a Tester class that meets the following requirements

  • main():
    • Read the list of players from a text file called players.txt. The text file must hold NormalPlayer objects and Whale objects. Each of these objects on file must have a jackpot value of 0.
    • Set up a single array list of Player objects. Populate the array list with the players that were read in from the text file.
    • Display the players to the user
    • Have a menu with four options (1) Add Player (2) Play (3) Update Bets (4) Save and Exit. Hint - You may use a do...while loop and a switch statement
    • If the user enters 1, add a new, custom player (NormalPlayer or Whale) to the list.
    • If the user enters 2, generate a random number X between 0 and 1000.
      • Use a for loop to traverse the array list. Print a toString() description of each NormalPlayer whose selectedNumber equals X. Print a toString() description of each Whale whose firstSelectedNumber or secondSelectedNumber equals X.
      • If one or more players won, set jackpot to 0 and then print a message saying that the jackpot was distributed among these players.
      • If no winning players were found, print a message saying the house wins. Set jackpot to 0.
      • Remove all the players from the list
    • If the user enters 3, traverse the array list
      • If the list is empty, print a message saying that no bets were made.
      • Else set the numberSelected of each NormalPlayer to a random value between 0 and 1000 and set the firstNumberSelected and the secondNumberSelected of each Whale to two random values between 0 and 1000.
    • If the user enters 4. Set jackpot to 0. Save the list of players back out to the text file and terminate the program.
    • Else, take the user back to the menu so they can see the players again and make another selection.

SPECIAL NOTE: The Player class, NormalPlayer class, and Whale class are given below for your convenience. Please answer how to write the Tester class. You are free to create your own text document for the program to read in the Tester class. This is a Java question and BlueJ is preferred. Thank you.

Player class:

public class Player{

private int ID;
private double cashContributed;
private static double jackpot;

public Player(){
setID(101);
setCashContributed(1000.50);
setJackpot(10000.60);
}

public Player(int ID, double cashContributed){
this.ID = ID;
this.cashContributed = cashContributed;
setJackpot(getJackpot()+ cashContributed);
}

public int getID(){
return ID;
}

public void setID(int ID){
this.ID=ID;
}

public double getCashContributed(){
return cashContributed;
}

public void setCashContributed(double cashContributed){
this.cashContributed=cashContributed;
}

public double getJackpot(){
return jackpot;
}

public void setJackpot(double jackpot){
this.jackpot = jackpot;
}

public String toString(){
String s="ID: " + ID + "Cash Contributed: " + cashContributed + "Jackpot: " + jackpot;
return s;
}

}

NormalPlayer class:'

public class NormalPlayer extends Player
{
private int numberSelected;

public NormalPlayer(){
super();
setNumberSelected(500);
}

public NormalPlayer(int ID, double cashContributed, int numberSelected){
super(ID, cashContributed);
setNumberSelected(numberSelected );
}

public int getNumberSelected(){
return numberSelected;
}

public void setNumberSelected(int numberSelected){
this.numberSelected = numberSelected;
}

public String toString(){
String s = super.toString() + "Number Selected: " + numberSelected;
return s;
}
}
Whale class:

public class Whale extends Player
{
private int firstNumberSelected;
private int secondNumberSelected;
  
public Whale(){
super();
setFirstNumberSelected(250);
setSecondNumberSelected(750);
}
  
public Whale(int ID, double cashContributed, int firstNumberSelected, int SecondNumberSelected){
super(ID,cashContributed);
setFirstNumberSelected(firstNumberSelected);
setSecondNumberSelected(secondNumberSelected);
}
  
public int getFirstNumberSelected(){
return firstNumberSelected;
}
  
public void setFirstNumberSelected(int firstNumberSelected){
this.firstNumberSelected=firstNumberSelected;
}
  
public int getSecondNumberSelected(){
return secondNumberSelected;
}
  
public void setSecondNumberSelected(int secondNumberSelected){
this.secondNumberSelected=secondNumberSelected;
}
  
public String toString(){
String s = super.toString()+"First Number: " + firstNumberSelected+"Second Number: " +secondNumberSelected;
return s;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Player.java

public class Player {
  
private int ID;
private double cashContributed;
private static double jackpot;
  
public Player()
{
setID(101);
setCashContributed(1000.50);
setJackpot(10000.60);
}
  
public Player(int ID, double cashContributed)
{
this.ID = ID;
this.cashContributed = cashContributed;
setJackpot(getJackpot() + cashContributed);
}
  
public double getJackpot() {
return jackpot;
}

public void setJackpot(double aJackpot) {
jackpot = aJackpot;
}

public int getID() {
return ID;
}

public void setID(int ID) {
this.ID = ID;
}

public double getCashContributed() {
return cashContributed;
}

public void setCashContributed(double cashContributed) {
this.cashContributed = cashContributed;
}
  
@Override
public String toString()
{
String s="ID: " + ID + ", Cash Contributed: " + cashContributed + ", Jackpot: " + jackpot;
return s;
}
}

NormalPlayer.java

public final class NormalPlayer extends Player{
private int numberSelected;

public NormalPlayer()
{
super();
setNumberSelected(500);
}
  
public NormalPlayer(int ID, double cashContributed, int numberSelected)
{
super(ID, cashContributed);
setNumberSelected(numberSelected );
}
  
public int getNumberSelected()
{
return numberSelected;
}

public void setNumberSelected(int numberSelected)
{
this.numberSelected = numberSelected;
}

@Override
public String toString()
{
String s = super.toString() + ", Number Selected: " + numberSelected;
return s;
}
}

Whale.java

public class Whale extends Player{
private int firstNumberSelected;
private int secondNumberSelected;

public Whale()
{
super();
setFirstNumberSelected(250);
setSecondNumberSelected(750);
}
  
public Whale(int ID, double cashContributed, int firstNumberSelected, int SecondNumberSelected)
{
super(ID,cashContributed);
setFirstNumberSelected(firstNumberSelected);
setSecondNumberSelected(SecondNumberSelected);
}
  
public int getFirstNumberSelected()
{
return firstNumberSelected;
}

public void setFirstNumberSelected(int firstNumberSelected)
{
this.firstNumberSelected=firstNumberSelected;
}

public int getSecondNumberSelected()
{
return secondNumberSelected;
}

public void setSecondNumberSelected(int secondNumberSelected)
{
this.secondNumberSelected=secondNumberSelected;
}

@Override
public String toString()
{
String s = super.toString() + ", First Number: " + firstNumberSelected +
", Second Number: " + secondNumberSelected;
return s;
}
}

Tester.java (Main class)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Tester {
  
private static final String FILENAME = "players.txt";
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
String userIn;
  
ArrayList<Player> players = readPlayerData(FILENAME);
  
int choice = 0;
  
do
{
printMenu();
userIn = sc.nextLine().trim();
while(!isDigit(userIn))
{
System.out.print("\nEnter your choice: ");
userIn = sc.nextLine().trim();
}
choice = Integer.parseInt(userIn);
  
switch(choice)
{
case 1:
{
displayAllPlayers(players);
addPlayer(players);
break;
}
  
case 2:
{
play(players);
break;
}
  
case 3:
{
updateBets(players);
break;
}
  
case 4:
{
saveAndExit(FILENAME, players);
System.exit(0);
}
  
default:
System.out.println("\nInvalid choice.\n");
}
}while(choice != 4);
}
  
private static ArrayList<Player> readPlayerData(String fileName)
{
ArrayList<Player> players = new ArrayList<>();
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(" ");
if(data.length == 3)
{
// it is a Normal Player
int ID = Integer.parseInt(data[0]);
double cashContributed = Double.parseDouble(data[1]);
int selectedNumber = Integer.parseInt(data[2]);
players.add(new NormalPlayer(ID, cashContributed, selectedNumber));
}
else if(data.length == 4)
{
// it is a Whale Player
int ID = Integer.parseInt(data[0]);
double cashContributed = Double.parseDouble(data[1]);
int firstNumberSelected = Integer.parseInt(data[2]);
int secondNumberSelected = Integer.parseInt(data[3]);
players.add(new Whale(ID, cashContributed, firstNumberSelected, secondNumberSelected));
}
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Could not locate file: " + fileName);
System.exit(0);
}
return players;
}
  
private static void printMenu()
{
System.out.print("1. Add Player"
+ "\n2. Play"
+ "\n3. Update Bets"
+ "\n4. Save and Exit"
+ "\nEnter your choice: ");
}
  
private static boolean isDigit(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException nfe){
System.out.println(s + " is not a valid integer.");
return false;
}
}
  
private static void addPlayer(ArrayList<Player> players)
{
Scanner sc = new Scanner(System.in);
  
// generate a random number either 0 or 1: 0 --> NormalPlayer, 1 --> Whale
Random rand = new Random();
int playerType = rand.nextInt(2) + 1;
switch(playerType)
{
case 1: // Normal player
{
System.out.print("Enter the player ID: ");
String userIn = sc.nextLine().trim();
while(!isDigit(userIn))
{
System.out.print("\nEnter the player ID: ");
userIn = sc.nextLine().trim();
}
int ID = Integer.parseInt(userIn);
  
// check if player with the same ID already exists
boolean found = false;
for(Player p : players)
{
if(p.getID() == ID)
{
found = true;
break;
}
}
if(found)
{
System.out.println("\nPlayer with id " + ID + " already exists!\n");
}
else
{
System.out.print("Enter the cash contributed by the player: $");
double cashContributed = Double.parseDouble(sc.nextLine().trim());
int selectedNumber = rand.nextInt(1000) + 0;
Player player = new NormalPlayer(ID, cashContributed, selectedNumber);
players.add(player);

System.out.println("\nA Normal Player has been successfully added to the list.\n"
+ player.toString() + "\n");
}
break;
}
  
case 2: // Whale
{
System.out.print("Enter the player ID: ");
String userIn = sc.nextLine().trim();
while(!isDigit(userIn))
{
System.out.print("\nEnter the player ID: ");
userIn = sc.nextLine().trim();
}
int ID = Integer.parseInt(userIn);
  
// check if player with the same ID already exists
boolean found = false;
for(Player p : players)
{
if(p.getID() == ID)
{
found = true;
break;
}
}
if(found)
{
System.out.println("\nPlayer with id " + ID + " already exists!\n");
}
else
{
System.out.print("Enter the cash contributed by the player: $");
double cashContributed = Double.parseDouble(sc.nextLine().trim());
int firstNumber = rand.nextInt(1000) + 0;
int secondNumber = rand.nextInt(1000) + 0;
Player player = new Whale(ID, cashContributed, firstNumber, secondNumber);
players.add(player);

System.out.println("\nA Whale has been successfully added to the list.\n"
+ player.toString() + "\n");
}
break;
}
}
}
  
private static void displayAllPlayers(ArrayList<Player> players)
{
for(Player p : players)
{
System.out.println(p.toString());
}
System.out.println();
}
  
private static void play(ArrayList<Player> players)
{
Random rand = new Random();
int X = rand.nextInt(1000) + 0;
int winners = 0;
for(Player p : players)
{
if(p instanceof NormalPlayer)
{
if(((NormalPlayer) p).getNumberSelected() == X)
{
System.out.println(p.toString());
p.setJackpot(0);
winners++;
}
}
else if(p instanceof Whale)
{
if(((Whale) p).getFirstNumberSelected() == X && ((Whale) p).getSecondNumberSelected() == X)
{
System.out.println(p.toString());
p.setJackpot(0);
winners++;
}
}
}
if(winners == 0)
{
System.out.println("\nThe house wins!\n");
}
else
{
System.out.println("\nThe Jackpot has been distributed among " + winners + " players.\n");
}
  
players.removeAll(players);
}
  
private static void updateBets(ArrayList<Player> players)
{
if(players.isEmpty())
System.out.println("\nNo bets were made.\n");
else
{
Random rand = new Random();
for(Player p : players)
{
if(p instanceof NormalPlayer)
{
int selectedNumber = rand.nextInt(1000) + 0;
((NormalPlayer) p).setNumberSelected(selectedNumber);
}
else if(p instanceof Whale)
{
int firstSelectedNumber = rand.nextInt(1000) + 0;
int secondSelectedNumber = rand.nextInt(1000) + 0;
((Whale) p).setFirstNumberSelected(firstSelectedNumber);
((Whale) p).setSecondNumberSelected(secondSelectedNumber);
}
}
System.out.println("\nBets updated for all players!\n");
}
}
  
private static void saveAndExit(String fileName, ArrayList<Player> players)
{
for(Player p : players)
{
p.setJackpot(0);
}
if(players.isEmpty())
System.out.println("\nThe list is empty!\n");
else
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(fileName));
pw = new PrintWriter(fw);
for(Player p : players)
{
if(p instanceof NormalPlayer)
{
pw.write(p.getID() + " " + p.getCashContributed() + " " + ((NormalPlayer) p).getNumberSelected()
+ System.lineSeparator());
}
else if(p instanceof Whale)
{
pw.write(p.getID() + " " + p.getCashContributed() + " " + ((Whale) p).getFirstNumberSelected()
+ " " + ((Whale) p).getFirstNumberSelected() + System.lineSeparator());
}
}
pw.flush();
fw.close();
pw.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}

****************************************************************** SCREENSHOT *********************************************************

Add a comment
Know the answer?
Add Answer to:
Question: Write the Main class code for the following application. The first three classes are given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Problem Statement: Mini Lottery. This mini lottery company caters to regular players and high rollers. It...

    Problem Statement: Mini Lottery. This mini lottery company caters to regular players and high rollers. It allows the regular players and the high rollers to add money to the jackpot. It can select a random set of players and awards them the jackpot money. The app must repeatedly do the following:          Display the list of players. Allow the user to (1) add a player (2) have one player add money to the jackpot (3) select one or more random players...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

    Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin {    /**     * Constructor for objects of class...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • How to create a constructor that uses parameters from different classes? I have to create a...

    How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT