Programming Assignment #2 Slot Machine Application & CountFlips & Extra Credit

Programming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming Pr...
Author: Lewis Marshall
0 downloads 0 Views 532KB Size
Programming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford

MIS 301 Introduction to Java Programming Professor Patti Ota

Part 1: Design and implement an application that simulates a simple slot machine, which displays three images randomly chosen in three slot positions: a. Slot Machine Driver //Driver for SlotsMachinePanel package programming.assignment.pkg2; import javax.swing.JFrame; public class SlotMachine { //-------------------------------// Creates the main program frame. //-------------------------------public static void main (String[] args) { JFrame frame = new JFrame ("Slot Machine"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SlotMachinePanel()); frame.pack(); frame.setVisible(true); } }

b. Slot Machine Panel: //----------------------------------// Slot Machine Panel //----------------------------------package programming.assignment.pkg2; import import import import

java.awt.*; java.awt.event.*; javax.swing.*; java.util.Random;

public class SlotMachinePanel extends JPanel { private JPanel buttonPanel, newSlotsPanel, primary; private JButton spin, cashout; private JLabel spinlabel, cashoutlabel, slotsPanelLabel1, slotsPanelLabel2; private JLabel imageLabel; private ImageIcon icon; private int spinResult = 0; private int currentTokens = 5; //--------------------------------------------------------------------//Constructor: Sets up the SlotMachine GUI. //--------------------------------------------------------------------public SlotMachinePanel () { //Creation of primary (Background Panel) primary = new JPanel ();

Heriford - Page 2

primary.setPreferredSize (new Dimension(325,275)); primary.setBackground (Color.red); //Creation of newSlotsPanel (Slot Screen Panel) newSlotsPanel = new JPanel (); newSlotsPanel.setPreferredSize (new Dimension (300,175)); newSlotsPanel.setBackground (Color.white); slotsPanelLabel1 = new JLabel ("Current Tokens:" + currentTokens); slotsPanelLabel2 = new JLabel ("Result of Spin:" + spinResult); //Creation of buttonPanel (Button Panel) buttonPanel = new JPanel(); buttonPanel.setPreferredSize (new Dimension(300,80)); buttonPanel.setBackground (Color.blue); //Creation of spin button spin = new JButton("Spin"); spin.addActionListener (new SpinListener()); spinlabel = new JLabel ("Only costs 1 token to spin! spinlabel.setForeground (Color.white);

Good luck!");

//Creation of cashout button cashout = new JButton("Cash Out"); cashout.addActionListener (new CashoutListener()); cashoutlabel = new JLabel ("No Button Pushed"); //Creation of image loader icon = (new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/Ca sino-Chips.jpg"))); // imageLabel = new JLabel (icon); //Layering of Panels add (primary); primary.add(newSlotsPanel); primary.add(buttonPanel); //Adding Labals on newSlotsPanel newSlotsPanel.add(slotsPanelLabel1); newSlotsPanel.add(slotsPanelLabel2); newSlotsPanel.add(imageLabel); //Adding Buttons and Labels on Button Panel buttonPanel.add(spin); buttonPanel.add(cashout); buttonPanel.add(spinlabel); } //***************************************************************** // Represents a listener for Spin Button (action) events. //***************************************************************** private class SpinListener implements ActionListener { //-------------------------------------------------------------// Updates the Spin and label when the button is pushed. //--------------------------------------------------------------

Heriford - Page 3

public void actionPerformed (ActionEvent event) { //Informing of pushed buttom spinlabel.setText("Spin again or cash out"); //Initiate Random Number choice for images Random generator = new Random(); int imageNum; imageNum = generator.nextInt(27); switch (imageNum) { case 0: //aaa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/aa a.jpg"))); spinResult = 5; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 5 tokens! Nice win! Spin again!"); break; case 1: //aac imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/aa c.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 2: //aao imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/aa o.jpg"))); spinResult = 1; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 1 token. Spin again!"); break; case 3: //aca imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ac a.jpg"))); spinResult = 2; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 2 tokens. Spin again!"); break; case 4: //acc imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ac c.jpg"))); spinResult = 4; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 4 tokens. Nice win! Spin again!"); break; case 5: //aco imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ac o.jpg")));

Heriford - Page 4

spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 6: //aoa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ao a.jpg"))); spinResult = 1; spinlabel.setText("Won 1 token. Nice! Spin again!"); currentTokens = currentTokens + spinResult - 1; break; case 7: //aoc imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ao c.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 8: //aoo imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ao o.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 9: //caa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ca a.jpg"))); spinResult = 3; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 3 tokens. Nice win! Spin again!"); break; case 10: //cac imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ca c.jpg"))); spinResult = 2; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); break; case 11: //cao imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ca o.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 12: //cca imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/cc a.jpg"))); spinResult = 4;

Heriford - Page 5

currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 4 tokens. Nice win! Spin again!"); break; case 13: //ccc imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/cc c.jpg"))); spinResult = 10; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Jackpot! Won 10 tokens. Spin again!"); break; case 14: //cco imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/cc o.jpg"))); spinResult = 2; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); break; case 15: //coa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/co a.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 16: //coc imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/co c.jpg"))); spinResult = 2; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 2 tokens. Nice win! Spin again!"); break; case 17: //coo imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/co o.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 18: //oaa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oa a.jpg"))); spinResult = 1; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 1 token. Nice win! Spin again!"); break; case 19: //oac imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oa c.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1;

Heriford - Page 6

spinlabel.setText("Won 0 tokens. Spin again!"); break; case 20: //oao imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oa o.jpg"))); spinResult = -1; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Ouch... Lost 1 token. Spin again!"); break; case 21: //oca imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oc a.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 22: //occ imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oc c.jpg"))); spinResult = 1; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 1 token. Nice! Spin again!"); break; case 23: //oco imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oc o.jpg"))); spinResult = -1; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Ouch... Lost 1 token! Spin again!"); break; case 24: //ooa imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oo a.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 25: //ooc imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oo c.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Won 0 tokens. Spin again!"); break; case 26: //ooo imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oo o.jpg"))); spinResult = -5; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Cmon!! Lost 5 tokens. Spin again!");

Heriford - Page 7

break; default: imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oo o.png"))); } slotsPanelLabel1.setText("Current Tokens:" + currentTokens);//returns new value of current tokens slotsPanelLabel2.setText("Result of Spin:" + spinResult);//returns the new result of spinning } } //***************************************************************** // Represents a listener for Spin Button (action) events. //***************************************************************** private class CashoutListener implements ActionListener { //-------------------------------------------------------------// Updates the Spin and label when the button is pushed. //-------------------------------------------------------------public void actionPerformed (ActionEvent event) { //Informing of pushed buttom spinlabel.setText("Spin to play again. Your cash out value is: "+ currentTokens); imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/mo ney.jpg"))); // currentTokens = 5; // resets the current token amount to 5. } } }

a. Four Screenshots of Slot Machine:  

Heriford - Page 8

After clicking “Spin” in order to play again, the ‘current tokens’ reverted back to 5 tokens. Part 2: Design and implement a driver class called CountFlips whose main method flips a coin 100 times and counts how many times each side comes up. Print the results. a. Driver: public class CountFlips { //----------------------------------------------------------------// Creates a Coin object, flips it, and prints the results. //----------------------------------------------------------------public static void main (String[] args) { final int numFlips = 100; int heads = 0; int tails = 0; Coin myCoin = new Coin(); for (int count = 1; count