Docsity

Slot Machine Assignment 6 - Programming I | COP 2210, Study notes of Computer Science

Toggle sidebar
bg1
Slot Machine Assignment (6) (Copy and paste when necessary)
SlotMachine.java
/* File: SlotMachine.java
*
* @ Name
*
* I affirm that this program is entirely my own work and
* none of it is the work of any other person.
*/
/**
* A class that models a typical slot machine in a casino.
*/
public class SlotMachine
{ // Instance Variables:
private int slot1 ; // The value of the first slot
private int slot2 ; // The value of the second slot
private int slot3 ; // The value of the third slot
private int wager ; // The amount wagered
/**
* The constructor that creates a slot machine object
* to input the slot values.
* @param inSlot1 The value of the first slot
* @param inSlot2 The value of the second slot
* @param inSlot3 The value of the third slot
* @param theWager The amount wagered into the slot machine
*/
public SlotMachine (int inSlot1, int inSlot2, int inSlot3, int theWager)
{
slot1 = inSlot1 ; // The value of the first slot
slot2 = inSlot2 ; // The value of the second slot
slot3 = inSlot3 ; // The value of the third slot
wager = theWager ; // The amount wagered
}
/**
* Get the three slot numbers
* @return The three slot numbers
*/
public String getSlotsAndWager()
pf3
pf4
pf5

Partial preview of the text

Download Slot Machine Assignment 6 - Programming I | COP 2210 and more Study notes Computer Science in PDF only on Docsity!

Slot Machine Assignment (6) (Copy and paste when necessary) SlotMachine.java /* File: SlotMachine.java

  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / /*
  • A class that models a typical slot machine in a casino. / public class SlotMachine { // Instance Variables: private int slot1 ; // The value of the first slot private int slot2 ; // The value of the second slot private int slot3 ; // The value of the third slot private int wager ; // The amount wagered /*
  • The constructor that creates a slot machine object
  • to input the slot values.
  • @param inSlot1 The value of the first slot
  • @param inSlot2 The value of the second slot
  • @param inSlot3 The value of the third slot
  • @param theWager The amount wagered into the slot machine / public SlotMachine (int inSlot1, int inSlot2, int inSlot3, int theWager) { slot1 = inSlot1 ; // The value of the first slot slot2 = inSlot2 ; // The value of the second slot slot3 = inSlot3 ; // The value of the third slot wager = theWager ; // The amount wagered } /*
  • Get the three slot numbers
  • @return The three slot numbers */ public String getSlotsAndWager()

// String that stores the three slot numbers String slots = "Numbers Entered: " + slot1 + ", " + slot2 + ", & "

  • slot3 + "\n" ; // String that stores the wager String wages = "Amount wagered: $" + wager + "\n" ; // String that neatly puts the previous String variables together String input = slots + wages ; return input ; } /**
  • A method to check if all three slot values are identical
  • @return True iff. three slots are the same / public boolean same3Slots() { boolean same3Slots = false ; // Default assumption // Iff. all conditions are true then the boolean value will be true if (slot1 == slot2 && slot2 == slot3 && slot1 == slot3) { same3Slots = true ; } return same3Slots ; } /*
  • A method to check if two of the three slot values are identical
  • @return True iff. two slots are the same */ public boolean same2Slots() { boolean same2Slots = false ; // Default assumption // Under any of the following conditions, the boolean value will be true if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) { same2Slots = true ; } return same2Slots ; }

if (same3Slots()) // If I have the same 3 slot numbers, { int prize = (100 * wager) ; // Then this is the amount that I won! receipt = "Congratulations! You have won the jackpot! You won: $"

  • prize + "! Also, reclaim your wager of: $" + wager ; } else if (same2Slots()) // If I have two slot numbers that are equal, { int prize = (4 * wager) ; // Then this is the amount that I won! receipt = "Congratulations! You have won a prize! You won: $"
  • prize + "! Also, reclaim your wager of: $" + wager ; } else if (slotsAreCST()) // If I have consecutive slot numbers in // any order, except for "wrap arounds," { int prize = (20 * wager) ; // Then this is the amount that I won! receipt = "Congratulations! You have won a prize! You won: $"
  • prize + "! Also, reclaim your wager of: $" + wager
  • "\nRemember that consecutive numbers can be in any "
  • "order, this is why I changed the ratio from 60 : 1 to"
  • " 20 : 1 as instructed." ; } if (wrongSlotNumber()) // If I typed in garbage number, { double prize = (0.0 / 0) ; // Then the machine crashes! receipt = "Sorry, you mistyped your values. Please type them "
  • "correctly to play again, or else... " + prize ; } else // If I have anything else, { int prize = (0 * wager) ; // Then the amount I won is (0 - wager)! receipt = "Sorry, you lost your wager; please enter your wager "
  • "to play again." ; } return receipt ; } } SlotMachineTest.java

/* File: SlotMachineTest.java

  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person.
  • The class that simulates a real slot machine; it just needs random numbers */ import javax.swing.JOptionPane ; // user input public class SlotMachineTest { public static void main(String args[]) { System.out.println("@ Name (\u00A7U0#)") ; String text ; // Main variable that accepts user input text = "Let's Play with the Slot Machine Today!" ; // Sounds legit! // Inputting the proper introduction to the Slot Machine "window" JOptionPane.showMessageDialog(null, text) ; text = JOptionPane.showInputDialog("Enter The 1st Number") ; int slot1 = Integer.parseInt(text) ; // Value for slot one text = JOptionPane.showInputDialog("Enter The 2nd Number") ; int slot2 = Integer.parseInt(text) ; // Value for slot two text = JOptionPane.showInputDialog("Enter The 3rd Number") ; int slot3 = Integer.parseInt(text) ; // Value for slot three text = JOptionPane.showInputDialog("Enter the Wager & Spin!") ; int wager = Integer.parseInt(text) ; // Amount wagered to play // The object that utilizes the four initialized parameters below SlotMachine Game = new SlotMachine(slot1, slot2, slot3, wager) ; // The accessor method that collects all the data before playing System.out.println(Game.getSlotsAndWager()) ; // This method enforces rules of the game and prints the receipt

@ Name (§U0#) Numbers Entered: 9, 8, & 7 Amount wagered: $ Congratulations! You have won a prize! You won: $100! Also, reclaim your wager of: $ Remember that consecutive numbers can be in any order, this is why I changed the ratio from 60 : 1 to 20 : 1 as instructed. BUILD SUCCESSFUL (total time: 0 seconds) / / Output for E. @ Name (§U0#) Numbers Entered: 5, 3, & 4 Amount wagered: $ Congratulations! You have won a prize! You won: $100! Also, reclaim your wager of: $ Remember that consecutive numbers can be in any order, this is why I changed the ratio from 60 : 1 to 20 : 1 as instructed. BUILD SUCCESSFUL (total time: 0 seconds) / / Output for F. @ Name (§U0#) Numbers Entered: 8, 9, & 10 Amount wagered: $ Sorry, you mistyped your values. Please type them correctly to play again, or else... NaN BUILD SUCCESSFUL (total time: 0 seconds) / / Output for G. @ Name (§U0#) Numbers Entered: 5, 6, & 0 Amount wagered: $ Sorry, you lost your wager; please enter your wager to play again. BUILD SUCCESSFUL (total time: 0 seconds) */