100% found this document useful (1 vote)
63 views

Assignment Javac

This document describes a programming assignment to create a slot machine application in Java. It includes two parts: 1. A slot machine driver and panel class. The panel class sets up the GUI for the slot machine including spin and cash out buttons. It uses random images and can result in winning or losing tokens. 2. Code snippets from the panel class that initialize the GUI, handle button clicks by spinning the slots and updating tokens, and use a switch statement to randomly select images and calculate token wins/losses.

Uploaded by

Kumarecit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
63 views

Assignment Javac

This document describes a programming assignment to create a slot machine application in Java. It includes two parts: 1. A slot machine driver and panel class. The panel class sets up the GUI for the slot machine including spin and cash out buttons. It uses random images and can result in winning or losing tokens. 2. Code snippets from the panel class that initialize the GUI, handle button clicks by spinning the slots and updating tokens, and use a switch statement to randomly select images and calculate token wins/losses.

Uploaded by

Kumarecit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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 <=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);
}

Heriford - Page 9

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:

Heriford - Page 10

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
{

Heriford - Page 11

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

Heriford - Page 12

Fourth try

Fifth try

Lost

Heriford - Page 13

You might also like