Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help me to code that java program Program “School bus Lights” in java Ste

ID: 3594432 • Letter: P

Question

please help me to code that java program

Program “School bus Lights” in java

Step1.The user is suppose to select on of the available buttons in order to set a specific time delay. if the user fails to select a delay period then the program uses 1 second as a default.

Step2 the user clicks on start. The left shows red first and then it alternates with the right light. The “start” label has changed tp “pause”

Step 3 the user may click on pause at any time. After clicking on pause the label changes to “resume”

Step 4 the red lights flash alternating. In the order to not wear out the flash mechanism there is a counter that turns off both lights after 100 flashes.

Step 5 the user can change the speed of the flashing by clicking on pause, cicking on a new radio button, and then clicking on resume

Notice that the 3.0 second interval has been selected.

the two red lights are never both on the same time.

the light both have a diameter of 160 pixels but you are allowed to make the diameter larger if you want.

Explanation / Answer

import java.awt.Component;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.SwingConstants;

import javax.swing.ScrollPaneConstants;

public class Lab7 extends JFrame {

JCheckBox ck[];
JLabel lb1;
JRadioButton jradio[], jradio1[];

static String[] pizzaToppings = "Tomato,Green peppper,Black olives,Mushrooms,Extra cheese,Pepproni,Sausage"

.split(",");

static String[] pizzaSizes = "Small: $6.50, Medium: $8.50, Large: $10".split(",");
static String[] pizzatypes = "Thin Crust, Mediam Crust, Pan".split(",");
static final String CLEAR = "clear";
static final String TOTAL = "total";

final ButtonGroup groupSizes = new ButtonGroup();
final ButtonGroup groupSizes1 = new ButtonGroup();

JPanel jsizes, jtoppings, jtypes;

JTextArea jtextArea;

static final private int[] PRICE_SIZES = { 650, 850, 1000 };

public Lab7() {

super("Pizza Shop");
jradio = new JRadioButton[pizzaSizes.length];
jradio1 = new JRadioButton[pizzatypes.length];
ck= new JCheckBox[pizzaToppings.length];

JPanel options = makeOptionsPanel();

add(options);

setSize(800, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {

new Lab7();

}

private JPanel makeOptionsPanel() {
jsizes = addRadioBoxes(pizzaSizes, "Pizza Size", jradio, groupSizes);
jtoppings = addCkBoxes(pizzaToppings, ck);
jtypes = addRadioBoxes(pizzatypes, "Pizza Type", jradio1, groupSizes1);

JPanel size_type = new JPanel();

size_type.setLayout(new GridLayout(1, 2, 15, 10));
size_type.add(jsizes);
size_type.add(jtypes);

JPanel buttonControls = new JPanel();

buttonControls.setBorder(BorderFactory.createEmptyBorder(1, 10, 1, 10));
JButton clear = new JButton("Clear");

clear.setActionCommand(CLEAR);

clear.addActionListener(new MyButtonListener());

JButton total = new JButton("Process Selection");

total.setActionCommand(TOTAL);
total.addActionListener(new MyButtonListener());
buttonControls.add(clear);
buttonControls.add(total);

JPanel si_ty_cont = new JPanel();

si_ty_cont.setLayout(new GridLayout(2, 1, 15, 15));
si_ty_cont.add(size_type);
si_ty_cont.add(buttonControls);

JPanel p = new JPanel();

p.setLayout(new GridLayout(1, 2, 15, 15));
p.add(jtoppings);
p.add(si_ty_cont);
p.setAlignmentX(Component.LEFT_ALIGNMENT);

JPanel completePanel = new JPanel();
lb1.setAlignmentX(Component.CENTER_ALIGNMENT);
completePanel.setLayout(new BoxLayout(completePanel, BoxLayout.PAGE_AXIS));

JLabel jlb = new JLabel("Your order: ", SwingConstants.LEFT);
jtextArea = new JTextArea();
jtextArea.setRows(5);

JScrollPane sp = new JScrollPane(jtextArea);
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

lb1 = new JLabel("Welcome to Home Style Pizza Shop");
lb1.setFont(new Font("Courier New", Font.BOLD, 20));
lb1.setForeground(Color.RED);

completePanel.add(lb1);
completePanel.add(Box.createVerticalGlue());
completePanel.add(p);
completePanel.add(Box.createVerticalGlue());
completePanel.add(jlb);
completePanel.add(sp);
  
return completePanel;

}

private void calcPrice() {
int total = 0, subTotal = 0;
int priceIndex = 0, priceIndex1 = 0;
for (int i = 0; i < jradio.length; i++)

{

if (jradio[i].isSelected()) {
priceIndex = i;
break;
}
}

for (int i = 0; i < jradio1.length; i++) {
if (jradio1[i].isSelected()) {
priceIndex1 = i;
break;
}
}

jtextArea.setText("");
StringBuilder sb = new StringBuilder();
  
sb.append(String.format("Pizza type: %s%n", pizzatypes[priceIndex1]));
sb.append(String.format("Pizza size: %s%n", pizzaSizes[priceIndex]));
sb.append("Toppings : ");
total += PRICE_SIZES[priceIndex];

String accumTops = "";

for (int i = 0; i < ck.length; i++) {
if (ck[i].isSelected()) {
accumTops += "" + i + ",";
subTotal += 150;
}
}

if (!accumTops.isEmpty()) {
String[] orderedTops = accumTops.split(",");
for (int i = 0; i < orderedTops.length; i++) {
sb.append(String.format("%s, ", pizzaToppings[Integer.parseInt(orderedTops[i])]));
}

}

else
{
sb.append(String.format("%s%n", "<no extras"));
}

total += subTotal;
sb.append(String.format(" Amount Due: $ %d.%2d%n", total / 100, total % 100));
jtextArea.setText(sb.toString());

}

private JPanel addRadioBoxes(String[] opts, String title, JRadioButton jrb[], ButtonGroup bg) {
int rows = opts.length;
JPanel p = new JPanel();
addBorder(p);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

lb1 = new JLabel(title);
lb1.setFont(new Font("Courier New", Font.ROMAN_BASELINE, 18));
lb1.setForeground(Color.RED);
p.add(lb1);

JPanel p1 = new JPanel();

p1.setLayout(new GridLayout(rows, 1));
for (int i = 0; i < rows; i++) {

jrb[i] = new JRadioButton(opts[i]);
p1.add(jrb[i]);
bg.add(jrb[i]);
}

p.add(p1);
return p;
}

private void addBorder(JPanel p) {

p.setBorder(BorderFactory.createLineBorder(Color.RED));
}

private JPanel addCkBoxes(String[] opts, JCheckBox checkbox[]) {

int rows = opts.length;
JPanel p = new JPanel();
addBorder(p);

p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
lb1 = new JLabel("Each Topping at $1.50", SwingConstants.LEFT);
lb1.setFont(new Font("Courier New", Font.ROMAN_BASELINE, 18));
lb1.setForeground(Color.RED);
p.add(lb1);

JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(rows, 1));
for (int i = 0; i < rows; i++) {
checkbox[i] = new JCheckBox(opts[i]);
p1.add(checkbox[i]);
}

p.add(p1);
return p;
}

private void clearButtons() {

groupSizes.clearSelection();
groupSizes1.clearSelection();
for (int i = 0; i < ck.length; i++) {

ck[i].setSelected(false);
}

jtextArea.setText("");
}

class MyButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand().equals(CLEAR)) {
clearButtons();
}

else if (e.getActionCommand().equals(TOTAL)) {
calcPrice();
}
}
}
}