programming assignment #2 - · pdf fileprogramming assignment #2 slot machine application...

13
Programming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming Professor Patti Ota

Upload: vodan

Post on 12-Mar-2018

245 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

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

By Alexander Joel Heriford

MIS 301 Introduction to

Java Programming Professor Patti Ota

Page 2: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 2

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 java.awt.*; import java.awt.event.*; import javax.swing.*; import 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 ();

Page 3: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 3

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! Good luck!"); spinlabel.setForeground (Color.white); //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/Casino-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. //--------------------------------------------------------------

Page 4: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 4

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/aaa.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/aac.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/aao.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/aca.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/acc.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/aco.jpg")));

Page 5: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 5

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/aoa.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/aoc.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/aoo.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/caa.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/cac.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/cao.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/cca.jpg"))); spinResult = 4;

Page 6: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 6

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/ccc.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/cco.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/coa.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/coc.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/coo.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/oaa.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/oac.jpg"))); spinResult = 0; currentTokens = currentTokens + spinResult - 1;

Page 7: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 7

spinlabel.setText("Won 0 tokens. Spin again!"); break; case 20: //oao imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/oao.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/oca.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/occ.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/oco.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/ooa.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/ooc.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/ooo.jpg"))); spinResult = -5; currentTokens = currentTokens + spinResult - 1; spinlabel.setText("Cmon!! Lost 5 tokens. Spin again!");

Page 8: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 8

break; default: imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/ooo.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/money.jpg"))); // currentTokens = 5; // resets the current token amount to 5. } } }

a. Four Screenshots of Slot Machine:

 

Page 9: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 9

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 <=numFlips; count++) { myCoin.flip(); if (myCoin.isHeads()) heads++; else tails++; } System.out.println ("The number flips: " + numFlips); System.out.println ("The number of heads: "+ heads); System.out.println ("The number of tails: "+ tails); }

Page 10: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 10

b. Coin Class (unmodified): public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; //----------------------------------------------------------------- // Sets up the coin by flipping it initially. //----------------------------------------------------------------- public Coin () { flip(); } //----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. //----------------------------------------------------------------- public void flip () { face = (int) (Math.random() * 2); } //----------------------------------------------------------------- // Returns true if the current face of the coin is heads. //----------------------------------------------------------------- public boolean isHeads () { return (face == HEADS); } //----------------------------------------------------------------- // Returns the current face of the coin as a string. //----------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; } }

c. Screenshot of program:

Page 11: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 11

Extra Credit: Design and implement an application that plays the Hi-Lo guessing game. a. Programming of HiLow game: //--------------------------------// // Hi-Low guessing game. // Extra Credit for Oct. 31 //--------------------------------// import java.util.Random; import javax.swing.JOptionPane; public class HiLow { public static void main (String[] args) { String numStr, correct; int lost; do { int guess; int answer; int guessNum = 0; //--System picks random number--// Random generator = new Random(); answer = generator.nextInt(100) + 1; numStr = JOptionPane.showInputDialog ("Enter an integer between 1 and 100: \n" + "(Warning! You get 5 guesses!)"); guess = Integer.parseInt(numStr); while (guessNum<4) { if (guess < answer) { numStr = JOptionPane.showInputDialog ("Your guess (" + guess + ") is low \n" + "Enter another integer: "); guess = Integer.parseInt(numStr); guessNum++; } else if (guess > answer) { numStr = JOptionPane.showInputDialog ("Your guess (" + guess + ") is high \n" + "Enter another integer: "); guess = Integer.parseInt(numStr); guessNum++; } else {

Page 12: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 12

correct = "You guessed Correctly!\nThe number was: " + answer; JOptionPane.showMessageDialog(null, correct); break; } } lost = JOptionPane.showConfirmDialog (null, "The correct answer is: " + answer + "\nPlay again?"); } while(lost==JOptionPane.YES_OPTION); } }

b. Screenshot of program: First try

Second try

Third try

Page 13: Programming Assignment #2 - · PDF fileProgramming Assignment #2 Slot Machine Application & CountFlips & Extra Credit By Alexander Joel Heriford MIS 301 Introduction to Java Programming

Heriford - Page 13

Fourth try

Fifth try

Lost