Professional Documents
Culture Documents
Course: B.tech
Section: CS-4
Assignment-4
Date of Submission:-12/04/2018
Page
Sr. No. Lab Assignment-4 Remarks
No.
Write a java program that works as a simple calculator using grid
1 layout to arrange button for the digits and 3
for the +, - , / ,*,% operation.
Write a java program that simulates a traffic light .The program lets the
user select one of three lights :red, yellow,or green with radio
3 button .on selecting a button an 63-69-6 appropriate message with9
stop or ready or Go should appear above the buttons in selected
color .Initially there is no message shown.
PROGRAM NO: - 1
PROBLEM DEFINITION:
Write a java program that works as a simple calculator using grid layout to arrange button for the digits and for the +, -
, / ,*,% operation.
IMPLEMENTATION:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
CalculatorDemo()
{
this.setLayout(layout);
layout.setHgap(0);
layout.setVgap(0);
add(j1);
j1.setLayout(new GridLayout(2,1));
j1.add(ta);
j1.add(t1);
add(j2);
j2.setLayout(new GridLayout(1,4));
j2.add(b11);
j2.add(b12);
j2.add(b13);
j2.add(b4);
add(j3);
j3.setLayout(new GridLayout(1,4));
j3.add(b8);
j3.add(b9);
j3.add(b10);
j3.add(b3);
add(j4);
j4.setLayout(new GridLayout(1,4));
j4.add(b5);
j4.add(b6);
j4.add(b7);
j4.add(b2);
add(j5);
j5.setLayout(new GridLayout(1,4));
j5.add(b18);
j5.add(b14);
j5.add(b17);
j5.add(b1);
add(j6);
j6.setLayout(new GridLayout(1,3));
j6.add(b15);
j6.add(b16);
j6.add(b19);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if(e.getSource() == b2)
{
a=Double.parseDouble(ta.getText());
temp=2;
st=ta.getText();
st=st+" - ";
t1.setText(st);
ta.setText("");
}
if(e.getSource() == b3)
{
a=Double.parseDouble(ta.getText());
temp=3;
st=ta.getText();
st=st+" * ";
t1.setText(st);
ta.setText("");
}
if(e.getSource() == b4)
{
a=Double.parseDouble(ta.getText());
temp=4;
st=ta.getText();
st=st+" / ";
t1.setText(st);
ta.setText("");
}
if(e.getSource()== b19)
{
a=Double.parseDouble(ta.getText());
temp=5;
st=ta.getText();
st=st+" % ";
t1.setText(st);
ta.setText("");
if(e.getSource() == b5)
ta.setText(ta.getText().concat("1"));
if(e.getSource() == b6)
ta.setText(ta.getText().concat("2"));
if(e.getSource() == b7)
ta.setText(ta.getText().concat("3"));
if(e.getSource() == b8)
ta.setText(ta.getText().concat("4"));
if(e.getSource() == b9)
ta.setText(ta.getText().concat("5"));
if(e.getSource() == b10)
ta.setText(ta.getText().concat("6"));
if(e.getSource() == b11)
ta.setText(ta.getText().concat("7"));
if(e.getSource() == b12)
ta.setText(ta.getText().concat("8"));
if(e.getSource() == b13)
ta.setText(ta.getText().concat("9"));
if(e.getSource() == b14)
ta.setText(ta.getText().concat("0"));
if(e.getSource() == b18)
ta.setText(ta.getText().concat("."));
if(e.getSource() == b15)
{
ta.setText("");
t1.setText("");
}
if(e.getSource() == b16)
{
String s1=ta.getText();
ta.setText("");
t1.setText("");
for (int i=0 ; i<s1.length()-1 ; i++)
{
ta.setText(ta.getText()+s1.charAt(i));
}
if(e.getSource() == b17)
{
b=Double.parseDouble(ta.getText());
switch(temp)
{
case 1 :
c=a+b;
st=t1.getText();
t1.setText(st+ta.getText());
ta.setText("");
break;
case 2 :
c=a-b;
st=t1.getText();
t1.setText(st+ta.getText());
ta.setText("");
break;
case 3 :
c=a*b;
st=t1.getText();
t1.setText(st+ta.getText());
ta.setText("");
break;
case 4:
c=a/b;
st=t1.getText();
t1.setText(st+ta.getText());
ta.setText("");
break;
case 5:
c=a%b;
st=t1.getText();
t1.setText(st+ta.getText());
ta.setText("");
break;
}
ta.setText(""+c);
}
}
catch(NumberFormatException e4)
{
ta.setText(" Syntax Error ");
}
}
public static void main(String args[])
{
CalculatorDemo f1 = new CalculatorDemo();
f1.setSize(500,400);
f1.setVisible(true);
f1.setTitle("Calculator");
OUTPUT SET:
Ans: java.util.regex consists of three classes − Pattern class, Matcher class and
PatternSyntaxException class.
PROBLEM DEFINITION:
Write a program that create a user inteface to perform integer division .The user enter two numbers in textfield Num1 and
Num2.The division of Num1 and Num2 is displayed in the result textfield when the button named "compute" is clicked.
Display the exception in a massage if exception are occur .
IMPLEMENTATION:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Division()
{
setLayout(null);
b1=new JButton("division");
l1=new JLabel("num1");
l2=new JLabel("num2");
t1=new JTextField(15);
t2=new JTextField(15);
t3=new JTextField(15);
b1.setSize(80,50);
b1.setLocation(50,300);
l1.setSize(70,50);
l1.setLocation(50,50);
t1.setSize(200,30);
t1.setLocation(130,50);
l2.setSize(70,50);
l2.setLocation(50,120);
t2.setSize(200,30);
t2.setLocation(130,120);
t3.setSize(200,30);
t3.setLocation(300,300);
add(l1);
add(l2);
add(t1);
add(t2);
add(t3);
add(b1);
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
if(b1.getActionCommand().equals("division"))
{
try{
c=0f;
a=Float.parseFloat(s);
b=Float.parseFloat(s1);
c=a/b;
e3=""+c;
t3.setText(e3);
}
catch(ArithmeticException e2)
{
e3="Arithmetic Exception occur";
t3.setText(e3);
}
catch(NumberFormatException e1)
{
e3="number format error";
t3.setText(e3);
}
}}
OUTPUT SET:
Ans: java.util.regex consists of three classes − Pattern class, Matcher class and
PatternSyntaxException class.
PROGRAM NO: - 3
PROBLEM DEFINITION:
Write a java program that simulates a traffic light .The program lets the user select one of three lights :red, yellow,or green with
radio button .on selecting a button an 63-69-6 appropriate message with stop or ready
or Go should appear above the buttons in selected color .Initially there is no message shown.
IMPLEMENTATION:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public TrafficLightSwing()
{
getContentPane().setLayout(new GridLayout(2, 1));
b1 =new JRadioButton("Red");
b2 =new JRadioButton("Yellow");
b3 =new JRadioButton("Green");
JRadioButton rb=new JRadioButton();
ButtonGroup bg=new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
t1=new JTextField();
t2 =new JTextField();
t3 =new JTextField();
b1.setForeground(Color.RED);
b2.setForeground(Color.YELLOW);
b3.setForeground(Color.GREEN);
green.turnOn(false);
yellow.turnOn(false);
red.turnOn(false);
JPanel p1 =new JPanel(new GridLayout(3,2));
p1.add(red);
p1.add(t1);
p1.add(yellow);
p1.add(t2);
p1.add(green);
p1.add(t3);
JPanel p2 =new JPanel(new FlowLayout());
p2.add(b1);
p2.add(b2);
p2.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
getContentPane().add(p1);
getContentPane().add(p2);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
if (e.getSource() == b2)
{
t2.setText("READY");
t1.setText(" ");
t3.setText(" ");
yellow.turnOn(true);
green.turnOn(false);
red.turnOn(false);
}
if (e.getSource() == b3)
{
t3.setText("GO");
t1.setText(" ");
t2.setText(" ");
red.turnOn(false);
yellow.turnOn(false);
green.turnOn(true);
}
}
Signal(Color color)
{
on = color;
flag = true;
}
if (flag)
g.setColor( on );
else
g.setColor( on.darker().darker().darker() );
g.fillOval( br,br,2*r,2*r );
}
}
OUTPUT SET:
3. What is an applet?
Ans: An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.