Assignment Javac
Assignment Javac
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);
}
}
java.awt.*;
java.awt.event.*;
javax.swing.*;
java.util.Random;
Heriford - Page 2
Good luck!");
Heriford - Page 3
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
Heriford - Page 6
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.
}
}
}
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
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
b. Screenshot of program:
First try
Second try
Third try
Heriford - Page 12
Fourth try
Fifth try
Lost
Heriford - Page 13