Question & Answer: Objectives The objective is to help students understand how to create Applets Steps In this lab, you are going to provide code that builds the Applet sho…..

Lab 31: Applets Objectives The objective is to help students understand how to create Applets Steps In this lab, you are going to provide code that builds the Applet shown below. Please follow the instructions below Create a new Java Class Library Project named Lab31 In the Project pane, right click on Lab31 and choose New-> JApplet and name the Applet, SodaMachineApplet and provide a package name of lab31 In the init0 method, add code that builds the components depicted in thepicture below. bolow ° Add an inner class named SodaActionListener that add functionality to the button:s ron the ap To run the applet, right click on the SodaMachineApplet.java file and choose Run File ise your hand and demonstrate the running project to the i nstructor Lab Questions Ca n you create an Applet without Swing class components? ° What goes in the html file that supports an Applet? 四Applet Viewer: CokeMachine.class Applet TOTAL: Price: .60 COIN SLOT: COKE GINGER-Ale ROOT BEER empty empty COIN RETURN CAN SLOT: Coin Return: 0 Applet started

Objectives The objective is to help students understand how to create Applets Steps In this lab, you are going to provide code that builds the Applet shown below. Please follow the instructions below: Create a new Java Class Library Project named “Lab31”. In the Project pane, right click on Lab31 and choose New rightarrow JApplet and name the Applet, “SodaMachineApplet and provide a package name of lab31. In the init() method, add code that builds the components depicted in the picture below. Add an inner class named SodaActionListener that add functionality to the buttons. To run the applet, right click on the SodaMachineApplet.java file and choose “Run File” Raise your hand and demonstrate the running project to the instructor Lab Questions Can you create an Applet without Swing class components? What goes in the html file that supports an Applet?

Expert Answer

 

Code to copy

//SodaMachineApplet.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.text.*;

//Class
public class SodaMachineApplet extends Applet implements ActionListener
{
//Needed text field variables
private TextField priceText;
private TextField coinText;
private TextField returnText;
private TextField canDropText;
private TextField totalDepositText;

//Needed button variables
private Button[] buttonsForSoda;
private Button buttonsForCoinReturn;

//Array for soda
private String[] sodaArray = {“COKE”,”GINGER-Ale”,”ROOT BEER”,”empty”,”empty”};

//Needed variables
private int sselection=1;
private boolean isValid=false;
static int coinTotal=0;
static int coin=0;
static int price=60;
static String stringForCoin;

//Method init()
public void init()
{
//Set layout
setLayout(new BorderLayout());

//Create a panel
Panel ourPanel1 = new Panel();

//Set layout for panel
ourPanel1.setLayout(new FlowLayout());

//Initialize
totalDepositText = new TextField(15);
totalDepositText.setEditable(false);
totalDepositText.setText(“Price .”+price);

//Add to panel
ourPanel1.add(new Label(“TOTAL:”));
ourPanel1.add(totalDepositText);

//Initialize
coinText = new TextField(5);
coinText.setEditable(true);
coinText.addActionListener(this);

//Add to panel
ourPanel1.add(new Label(“COIN SLOT:”));
ourPanel1.add(coinText);
add(“North”, ourPanel1);

//Create panel
Panel ourPanel2 = new Panel();

//Set layout for panel
ourPanel2.setLayout(new GridLayout(3,2));

//Create
buttonsForSoda = new Button[sodaArray.length];

//Loop
for (int i=0; i < sodaArray.length; i++)
{
//Initialize
buttonsForSoda[i] = new Button(sodaArray[i]);
buttonsForSoda[i].addActionListener(this);
ourPanel2.add(buttonsForSoda[i]);
}

//Initialize
buttonsForCoinReturn = new Button(“COIN RETURN”);
buttonsForCoinReturn.addActionListener(this);

//Add
ourPanel2.add(buttonsForCoinReturn);
add (“Center”, ourPanel2);

//Create panel
Panel ourPanel3 = new Panel();

//Set layout for panel
ourPanel3.setLayout(new FlowLayout());

//Create
canDropText = new TextField(15);
canDropText.setEditable(false);

//Add
ourPanel3.add(new Label(“CAN SLOT:”));
ourPanel3.add(canDropText);

//Initialize
returnText = new TextField(5);
returnText.setEditable(false);

//Add
ourPanel3.add(new Label(“Coin Return:”));
ourPanel3.add(returnText);
add (“South”,ourPanel3);
}

//Method actionPerformed()
public void actionPerformed(ActionEvent e)
{
//Create
Object src = e.getSource();

//Check condition
if (src == coinText)
{
//Display
System.out.println(“at coinText”);

//Function call
resetSale();

//Function call
validateIt();

//Check condition
if (isValid)

//Function call
calculateCoin();
}

//Check condition
else if (src == buttonsForCoinReturn)

//Function call
moneyReturn();

//Otherwise
else

//Function call
sodeSelection(e.getActionCommand());
}

//Method resetSale()
public void resetSale()
{
//Display
System.out.println(“Reset”);

//Reset
canDropText.setText(“”);

//Reset
returnText.setText(“”);
}

//Method postSale()
public void postSale()
{
//Display
System.out.println(“post Sale”);

//Reset
coinText.setText(“”);

//Display
totalDepositText.setText(“Price: .”+price);

//Update
coinTotal = 0;
coin = 0;
isValid=false;
}

//Method validateIt()
public void validateIt()
{
//Initialize
stringForCoin = coinText.getText();

//Display
System.out.println(“Validate”);

//Check condition
if(! stringForCoin.equals(“”))
{
//Update
coin = new Integer(stringForCoin.trim()).intValue();

//Check condition
if(coin==5 | coin==10 | coin==25 | coin==100)

//Update
isValid=true;

//Otherwise
else
{
//Update
isValid=false;
canDropText.setText(“Invalid Coin”);
returnText.setText(“”+coin);
coinText.setText(“”);
}
}
}

//Method calculateCoin()
public void calculateCoin()
{
//Initialize
coinTotal = coinTotal + coin;

//Check condition
if (coinTotal==100)

//Update
totalDepositText.setText(“1.00”);

//Check condition
else if (coinTotal==5)

//Update
totalDepositText.setText(“.05”);

//Otherwise
else

//Update
totalDepositText.setText(“.”+(coinTotal));
coinText.setText(“”);

//Display
System.out.println(“Post Calculation”);

//Check condition
if(coinTotal >= price)
{
//Update
isValid = true;
totalDepositText.setText(“MAKE SELECTION”);
}
}

//Method moneyReturn()
public void moneyReturn()
{
//Display
System.out.println(“Coin return1”);

//Check condition
if (coinTotal==100)

//Update
returnText.setText(“1.00”);

//Check condition
else if (coinTotal==5)

//Update
returnText.setText(“.05”);

//Otherwise
else

//Update
returnText.setText(“.”+(coinTotal));

//Display
System.out.println(“Coin return2”);

//Function call
postSale();
}

//Method sodeSelection()
public void sodeSelection(String pop)
{
//Initialize
sselection = 0;

//Loop
while(!pop.equals(sodaArray[sselection]))

//Increment
sselection++;

//Check condition
if (! sodaArray[sselection].equals(“empty”))
{
//Check condition
if (coinTotal>=price)
{
//Set
canDropText.setText(sodaArray[sselection]);

//Check condition
if (coinTotal >price)
{
//Check condition
if ((coinTotal-price)==5)

//Return
returnText.setText(“.05”);

//Otherwise
else

//Return
returnText.setText(“.”+(coinTotal-price));
}

//Function call
postSale();
}
}
}
}

<!– SodaMachineApplet.html –>
<!– Html tag –>
<html>

<!– Applet code –>
<applet code=”SodaMachineApplet.class” height=300 width=500></applet>

<!– End of html tag –>
</html>

Still stressed from student homework?
Get quality assistance from academic writers!