You are on page 1of 19

Rajasthan Technical University, Kota

Department of Computer Science Engineering


Advance Java Lab
( 2017-18 )
Lab File
for
th
6 Semester

Enrollment No.: 15/470

Name: Vinod Saini

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 program that create a user inteface to perform integer division


.The user enter two numbers in textfield Num1 and Num2.The
2 division of Num1 and Num2 is displayed in the result textfield6
when the button named "compute" is clicked.
Display the exception in a massage if exception are occur .

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.*;

class CalculatorDemo extends JFrame implements ActionListener


{
Double a,b,c;
int temp;

JTextArea t1=new JTextArea();


JTextArea ta=new JTextArea();
JButton b1=new JButton("+");
JButton b2=new JButton("-");
JButton b3=new JButton("X");
JButton b4=new JButton("/");
JButton b5=new JButton("1");
JButton b6=new JButton("2");
JButton b7=new JButton("3");
JButton b8=new JButton("4");
JButton b9=new JButton("5");
JButton b10=new JButton("6");
JButton b11=new JButton("7");
JButton b12=new JButton("8");
JButton b13=new JButton("9");
JButton b14=new JButton("0");
JButton b15=new JButton("CLR");
JButton b16=new JButton("DEL");
JButton b17=new JButton("=");
JButton b18=new JButton(".");
JButton b19=new JButton(" % ");

JPanel j1=new JPanel();


JPanel j2=new JPanel();
JPanel j3=new JPanel();
JPanel j4=new JPanel();
JPanel j5=new JPanel();
JPanel j6=new JPanel();
GridLayout layout = new GridLayout(6,4);

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);

public void actionPerformed(ActionEvent e)


{
String st="";
try{
if(e.getSource()== b1)
{
a=Double.parseDouble(ta.getText());
temp=1;
st=ta.getText();
st=st+" + ";
t1.setText(st);
ta.setText("");

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:

Compile Program: No Error

Run Program: No Error


VIVA QUES. & ANS.:

1. java.util.regex consists of which classes?

Ans: java.util.regex consists of three classes − Pattern class, Matcher class and
PatternSyntaxException class.

2. When parseInt() method can be used?


Ans: This method is used to get the primitive data type of a certain String.

3. When a byte datatype is used?


Ans: This data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
PROGRAM NO: - 2

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.*;

class Division extends JFrame implements ActionListener


{
JLabel l1,l2;
JTextField t1, t2, t3;
JButton b1;

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);
}

public void actionPerformed(ActionEvent e)


{
Float a,b,c;
String s="";
String s1="";
String e3="";
s=t1.getText();
s1=t2.getText();

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);
}

}}

public static void main(String arg[])


{
Division s=new Division();
s.setSize(800,400);
s.setVisible(true);
s.setTitle(" YOGESH ARORA");
}
}

OUTPUT SET:

Compile Program: No Error

Run Program: No Error


Awnser :

VIVA QUES. & ANS.:

1. java.util.regex consists of which classes?

Ans: java.util.regex consists of three classes − Pattern class, Matcher class and
PatternSyntaxException class.

2. When parseInt() method can be used?


Ans: This method is used to get the primitive data type of a certain String.

3. What do you mean by Access Modifier?


Ans: Java provides access modifiers to set access levels for classes, variables, methods and
constructors. A member has package or default accessibility when no accessibility modifier is
specified.

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.*;

class TrafficLightSwing extends JFrame implements ActionListener


{
JRadioButton b1, b2, b3;
JTextField t1,t2,t3;

Signal green =new Signal(Color.green);


Signal yellow =new Signal(Color.yellow);
Signal red =new Signal(Color.red);

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);
}

public void actionPerformed(ActionEvent e)


{
if (e.getSource() == b1)
{
t1.setText("STOP");
t2.setText(" ");
t3.setText(" ");
red.turnOn(true);
green.turnOn(false);
yellow.turnOn(false);

}
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);
}
}

public static void main(String[] args)


{

TrafficLightSwing f =new TrafficLightSwing();


f.setSize(new Dimension(500,600));
f.setVisible(true);
}

class Signal extends JPanel


{
Color on;
int r = 40;
int br = 10;
boolean flag;

Signal(Color color)
{

on = color;
flag = true;
}

public void turnOn(boolean a)


{
flag = a;
repaint();
}

public Dimension getPreferredSize()


{
int size = (r+br)*2;
return new Dimension( size, size );
}

public void paintComponent(Graphics g)


{
g.setColor( Color.black );
g.fillRect(0,0,getWidth(),getHeight());

if (flag)
g.setColor( on );
else
g.setColor( on.darker().darker().darker() );

g.fillOval( br,br,2*r,2*r );
}
}

OUTPUT SET:

Compile Program: No Error

Run Program: No Error


VIVA QUES. & ANS.:

1. An applet extend which class?


Ans: An applet extends java.applet.Applet class.

2. What is a Class Variable?


Ans: These are variables declared with in a class, outside any method, with the static
keyword.

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.

You might also like