0% found this document useful (0 votes)
3 views

java lab@msc

The document contains two Java programs: one simulates a traffic light with radio buttons displaying messages for red, yellow, and green lights, and the other creates a simple calculator for basic arithmetic operations. The traffic light program uses a JFrame and CheckboxGroup to handle user input and display corresponding messages in color. The calculator program utilizes buttons and text fields to perform addition, subtraction, multiplication, and division, displaying the result in a designated text field.

Uploaded by

latag16024
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java lab@msc

The document contains two Java programs: one simulates a traffic light with radio buttons displaying messages for red, yellow, and green lights, and the other creates a simple calculator for basic arithmetic operations. The traffic light program uses a JFrame and CheckboxGroup to handle user input and display corresponding messages in color. The calculator program utilizes buttons and text fields to perform addition, subtraction, multiplication, and division, displaying the result in a designated text field.

Uploaded by

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

8.Write a Java program that simulates a traffic light.

The program lets the user selects one of three


lights: red, yellow, or green with radio buttons. On selecting a button, an appropriate message with
Stop, Ready, or Go should appear above the buttons in the selected color.

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

class TrafficLightSimulator extends JFrame implements ItemListener {

JLabel lbl1, lbl2;

JPanel nPanel, cPanel;

CheckboxGroup cbg;

public TrafficLightSimulator() {

setTitle("Traffic Light Simulator");

setSize(600,400);

setLayout(new GridLayout(2, 1));

nPanel = new JPanel(new FlowLayout());

cPanel = new JPanel(new FlowLayout());

lbl1 = new JLabel();

Font font = new Font("Verdana", Font.BOLD, 70);

lbl1.setFont(font);

nPanel.add(lbl1);

add(nPanel);

Font fontR = new Font("Verdana", Font.BOLD, 20);

lbl2 = new JLabel("Select Lights");


lbl2.setFont(fontR);

cPanel.add(lbl2);

cbg = new CheckboxGroup();

Checkbox rbn1 = new Checkbox("Red Light", cbg, false);

rbn1.setBackground(Color.RED);

rbn1.setFont(fontR);

cPanel.add(rbn1);

rbn1.addItemListener(this);

Checkbox rbn2 = new Checkbox("Orange Light", cbg, false);

rbn2.setBackground(Color.ORANGE);

rbn2.setFont(fontR);

cPanel.add(rbn2);

rbn2.addItemListener(this);

Checkbox rbn3 = new Checkbox("Green Light", cbg, false);

rbn3.setBackground(Color.GREEN);

rbn3.setFont(fontR);

cPanel.add(rbn3);

rbn3.addItemListener(this);

add(cPanel);

setVisible(true);

// to close the main window

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// To read selected item

public void itemStateChanged(ItemEvent i) {


Checkbox chk = cbg.getSelectedCheckbox();

String str=chk.getLabel();

char choice=str.charAt(0);

switch (choice) {

case 'R':lbl1.setText("STOP");

lbl1.setForeground(Color.RED);

break;

case 'O':lbl1.setText("READY");

lbl1.setForeground(Color.ORANGE);

break;

case 'G':lbl1.setText("GO");

lbl1.setForeground(Color.GREEN);

break;

// main method

public static void main(String[] args) {

new TrafficLightSimulator();

}
9.Write a Java program that to create simple simple calculator. for the , -,*, % operations. Add a text
field to display the result.

/* Program to create a Simple Calculator */

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class GUICalculator extends JFrame {

private JButton button1;

private JTextField textField1;

private JButton button2;

private JButton button3;

private JButton button4;

private JTextField textField2;

private JTextField textField3;


public static void main(String[] args) {

GUICalculator cal = new GUICalculator();

cal.setTitle("GUI Calculator");

cal.setSize(600, 400);

cal.setVisible(true);

cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public GUICalculator() {

JPanel panel1 = new JPanel();

button1 = new JButton("+");

button2 = new JButton("-");

button3 = new JButton("*");

button4 = new JButton("/");

textField1 = new JTextField(50);

textField2 = new JTextField(50);

textField3 = new JTextField(50);


ListenForButton listenForButton = new ListenForButton();

button1.addActionListener(listenForButton);

button2.addActionListener(listenForButton);

button3.addActionListener(listenForButton);

button4.addActionListener(listenForButton);

panel1.add(textField1);

panel1.add(textField2);

panel1.add(textField3);

panel1.add(button1);

panel1.add(button2);

panel1.add(button3);

panel1.add(button4);

this.add(panel1);

private class ListenForButton implements ActionListener {

@Override

public void actionPerformed(ActionEvent e) {

//1
double number1 = 0;

double number2 = 0;

//2

try {

number1 = Double.parseDouble(textField1.getText());

number2 = Double.parseDouble(textField2.getText());

} catch (Exception error) {

error.printStackTrace();

//3

if (e.getSource() == button1) {

textField3.setText(number1 + number2 + "");

} else if (e.getSource() == button2) {

textField3.setText(number1 - number2 + "");

} else if (e.getSource() == button3) {

textField3.setText(number1 * number2 + "");

} else if (e.getSource() == button4) {

textField3.setText(number1 / number2 + "");

You might also like