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

Java Lab MS University

The document provides instructions for a lab manual for practical courses in MCA. It contains contents listing various exercises on topics like area of triangle and rectangle, comparing dates in Java, traffic light using applets, and animation using Java applets.

Uploaded by

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

Java Lab MS University

The document provides instructions for a lab manual for practical courses in MCA. It contains contents listing various exercises on topics like area of triangle and rectangle, comparing dates in Java, traffic light using applets, and animation using Java applets.

Uploaded by

Balaji Kannan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

LAB MANUAL FOR PRACTICAL COURSES

MCA

DIRECTORATE OF DISTANCE & CONTINUING EDUCATION


MANONMANIAM SUNDARANAR UNIVERSITY
TIRUNELVELI - 627 012

DECEMBER 2020 / MAY 2021


PRACTICAL EXAMINATION

1
DIRECTORATE OF DISTANCE & CONTINUING EDUCATION

MANONMANIAM SUNDARANAR UNIVERSITY


Reaccredited with ‘A’ Grade (CGPA 3.13 out of 4.0) by NAAC (3rd Cycle)

TIRUNELVELI – 627 012, Tamilnadu, India

Department of Computer Science

This is to certify that this is the bonafide record work of practical work done by

Mr./Ms. ________________________ Reg.No._________________________ of II MCA,

Java & Multimedia Lab, at the Directorate of Distance & Continuing Education, Manonmaniam

Sundaranar University, Tirunelveli during the academic year ________________.

Internal Examiner External Examiner

2
Java & Multimedia Lab - (DKAP3)
CONTENTS

Sl. Title of the Exercise Page No.


No.
1. Area of Triangle And Rectangle 04

2. Comparing Dates in Java 06

3. Traffic Light Using Applet In Java 09

4. Animation Using Java Applets 12

5. Open Dialog Box using Java Swing 15

6. Simple Calculator Using Java Swing 18

7. Create Menubar in Java Swing 24

8. Create a Java Bean to Draw Various 28


Graphical Shapes using Bdk Or
Without Bdk

3
EX.NO: 1 AREA OF TRIANGLE AND RECTANGLE

AIM
To Write a Java program to compute area of triangle and rectangle by using inheritance.

ALGORITHM

1. Create a super class called Figure that receives the dimensions of two dimensional
objects.
2. It also defines a method called area that computes the area of an object.
3. Derive two subclasses from Figure.
4. The first is Rectangle and second is Triangle.
5. Each of the sub class overridden area() so that it returns the area of a rectangle and a
triangle respectively.

PROGRAM :

import java.io.*;
class Figure
{
intl,w;
Figure(inta,int b)
{
l=a;
w=b;
}
void area()
{
System.out.println(“Area of the Figure”);
}
}
class Triangle extends Figure
{
Triangle(intg,int h)
{
super(g,h);
}
void area()
{
System.out.println(“Area is “+(l*w)/2);

4
}
}
class Rectangle extends Figure
{
Rectangle(intk,int l)
{
super(k,l);
}
void area()
{
System.out.println(“Area is “+l*w);
}
}
class A
{
public static void main(String ar[])
{
Triangle obj1=new Triangle(10,20);
Rectangle obj2=new Rectangle(20,20);
obj1.area();
obj2.area();
}
}

OUTPUT:

Area is 100

Area is 400

RESULT:
Thus the above program was executed successfully.

5
EX.NO: 2 COMPARING DATES IN JAVA
DATE:

AIM
To Write java program to compare dates.

ALGORITHM

1. Intially take the input interval of time in hours, minutes and seconds.
2. The system time can be read as using the object of the class GregorianCalendar .
3. This GregorianCalendar class contains a method by name get which returns a required
value from the hour, minutes and seconds of the system time.
4. Calendar class contains variables hour, minute,second which gives the system time
through the object of the class GregorianCalendar.
5. Read the time intervals (HH:MM) and to compare system time if the system time
between your time intervals print correct time and exit else try again to repute the same
thing. By using String Tokenizer class

PROGRAM :

import java.io.*;
import java.util.*;
import java.text.*;
public class Time
{
public static void main(String ar[])
{
int HOUR,SECOND,MINUTE;
Scanner s=new Scanner(System.in);
System.out.println(“Enter seconds : “);
int a=s.nextInt();
System.out.println(“Enter Minutes : “);
int b=s.nextInt();
System.out.println(“Enter Hours : “);
int c=s.nextInt();

6
GregorianCalendar date=new GregorianCalendar();
int second=date.get(Calendar.SECOND);
int minute=date.get(Calendar.MINUTE);
int hour=date.get(Calendar.HOUR);
if(second >a)
SECOND=second-a;
else
SECOND=a-second;
if(minute >b)
MINUTE=minute-b;
else
MINUTE=b-minute;
if(hour>c)
HOUR=hour-c;
else
HOUR=c-hour;
System.out.println(“Difference between two times is
“+HOUR+”:”+MINUTE+”:”+SECOND);
} }

7
OUTPUT:

RESULT:
Thus the above program was executed successfully.

8
EX.NO: 3 TRAFFIC LIGHT USING APPLET IN JAVA

DATE:

AIM
To design Traffic Light using Applet in Java

ALGORITHM

1. Use Applet implements ItemListener,


2. init() create the CheckboxGroup() and Checkbox() add it to addItemListener()
3. Use repaint() to call the paint method repeatedly until certain condition.
4. Paint method design signal using drawOval()
5. Alter the signal using setColor(Color.red/green/orange)

PROGRAM

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code=”Traffic” width=700 height=600>
</applet>*/
public class Traffic extends Applet implements Runnable
{
Thread t;
inti=0,a=0,j=0;
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
for(i=20;i>=0;i–)//countdown
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
if(i<=20 &&i>3)//red

9
{
a=1;
repaint();
}
else
if(i<=3 &&i>0)//yelloe
{
a=2;
repaint();
}
else
if(i==0)//green
{
for(j=0;j<20;j++)
{
a=3;
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
repaint();
}
if(j==20)//end of green(return to red)
{
run();
}
}
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(Color.black);//pole top
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
g.setColor(Color.black);//POLE UP
g.fillRect(150,150,50,150);
g.drawRect(150,150,50,150);
g.setColor(Color.black);//POLE DOWN
g.fillRect(165,300,20,155);
g.drawRect(165,300,20,155);
g.drawOval(150,150,50,50);//RED

10
g.drawOval(150,200,50,50);//YELLOW
g.drawOval(150,250,50,50);//GREEN
g.setColor(Color.red);//COUNTDOWN STOP
g.drawString(“”+i,50,50);
if(a==1)//REDSIGNAL
{
g.setColor(Color.red);
g.fillOval(150,150,50,50);
g.drawOval(150,150,50,50);
g.drawString(“STOP”,50,150);
}
if(a==2)//YELLOWSIGNAL
{
g.setColor(Color.yellow);
g.fillOval(150,200,50,50);
g.drawOval(150,200,50,50);
g.drawString(“READY”,50,200);
}
if(a==3)//GREENSIGNAL
{
g.setColor(Color.blue);//countdown
g.drawString(“”+j,150,50);
g.setColor(Color.green);
g.fillOval(150,250,50,50);
g.drawOval(150,250,50,50);
g.drawString(“GO”,50,250);
}
}
}

OUTPUT

11
EX.NO: 4 ANIMATION USING JAVA APPLETS

DATE:

AIM:
To write a program for animation using java applets.

ALGORITHM:

Step 1: Start the program


Step 2: Create a class by extending applet.Draw four car pictures to perform
animation
Step 3: Animate the pictures by varying X,Y position.
Step 4: Run the program in the command line using appletviewer.

PROGRAM:

APPLETFILE:
import java.awt.*;
import java.util.*;
import java.applet.*;
/* <APPLET CODE="Animation.class" WIDTH=400 HEIGHT=300>
</APPLET>*/
public class Animation extends Applet implements Runnable
{
Thread t;
//4 variables used to vary the car's positions.
int x1=0,x2=380,y1=50,y2=250;
public void start()
{
if(t==null)
{
t=new Thread(this,"New Thread");//New side
Thread created on start of applet.
t.start();
}
}
public void stop()
{
if(t!=null)

12
{
t=null;//On stop of applet the created thread is destroyed.
}
}
//Implementation of method run() of Runnable interface.
public void run()
{
Thread t1=Thread.currentThread();
while(t==t1)
{
repaint();
try

{
Thread.sleep(100);
}
catch(Exception e)
{ }
}
}
public void paint(Graphics g)
{
setBackground(Color.cyan);
g.setColor(Color.BLACK);
//Draw the roads using 2 filled rectangles using black color.

g.fillRect(0,130,400,40);
g.fillRect(180,0,40,305);
//Draw the white colored lines.
g.setColor(Color.white);
for(inti=0;i<20;i++)
{
if(i!=9 &&i!=10)
g.drawLine(i*20,150,i*20+10,150);
}
for(int j=0;j<15;j++)
{
if(j!=7 && j!=8)
g.drawLine(200,j*20,200,j*20+10);

13
}
//Draw 4 colored cars using filled round rectangles.
g.setColor(Color.red);
x1=(x1+16)%400;
x2=x2-16;
y1=(y1+12)%300;
y2=y2-12;
if(y2<0)
y2=288;
if(x2<0)

x2=384;
g.fillRoundRect(x2,152,20,8,2,2);
g.fillRoundRect(x1,140,20,8,2,2);
g.fillRoundRect(190,y1,8,20,2,2);
g.fillRoundRect(202,y2,8,20,2,2);
}
}

OUTPUT:

RESULT:
Thus the above program was executed successfully.

14
EX.NO:5 OPEN DIALOG BOX USING JAVA SWING

DATE:

AIM:
To write a program to show open dialog box using java swing.

ALGORITHM:
Step 1: Start the program
Step 2: Create the java program using swing controls.
Step 3: Use JFrame control to create frame.
Step 4:Use JFileChooser object to show open file dialog box.Save the program
with the extension .java
Step 5: Execute the program.

PROGRAM:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class OpenMenu extends JFrame implements ActionListener{
JMenuBarmb;
JMenu file;
JMenuItem open;
JTextArea ta;
OpenMenu(){
open=new JMenuItem("Open File");
open.addActionListener(this);

file=new JMenu("File");
file.add(open);

mb=new JMenuBar();
mb.setBounds(0,0,800,20);
mb.add(file);

ta=new JTextArea(800,800);
ta.setBounds(0,20,800,800);

15
add(mb);
add(ta);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==open){
openFile();
}
}
void openFile(){
JFileChooser fc=new JFileChooser();
inti=fc.showOpenDialog(this);

if(i==JFileChooser.APPROVE_OPTION){
File f=fc.getSelectedFile();
String filepath=f.getPath();
displayContent(filepath);
}
}
void displayContent(String fpath){
try{
BufferedReaderbr=new BufferedReader(new FileReader(fpath));
String s1="",s2="";

while((s1=br.readLine())!=null){
s2+=s1+"\n";
}
ta.setText(s2);
br.close();
}catch (Exception e) {e.printStackTrace(); }
}
public static void main(String[] args) {
OpenMenuom=new OpenMenu();
om.setSize(800,800);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

16
OUTPUT:

RESULT:
Thus the program was executed successfully.

17
EX.NO:6 SIMPLE CALCULATOR USING JAVA SWING

DATE:

AIM:

To write a calculator program using java swing.

ALGORITHM:

Step 1: Start the program


Step 2: Using the swing components design the buttons of the calculator.
Step 3: Use key events and key listener to listen the events of the calculator
Step 4: Do the necessary manipulations.
Step 5: Stop the program.

PROGRAM:

import javax.swing.*;
import java.awt.event.*;
class Calc implements ActionListener
{
JFrame f;
JTextField t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
static double a=0,b=0,result=0;
static int operator=0;
Calc()
{
f=new JFrame("Calculator");
t=new JTextField();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");

18
b8=new JButton("8");
b9=new JButton("9");
b0=new JButton("0");
bdiv=new JButton("/");
bmul=new JButton("*");
bsub=new JButton("-");
badd=new JButton("+");
bdec=new JButton(".");
beq=new JButton("=");
bdel=new JButton("Delete");
bclr=new JButton("Clear");

t.setBounds(30,40,280,30);
b7.setBounds(40,100,50,40);
b8.setBounds(110,100,50,40);

b9.setBounds(180,100,50,40);
bdiv.setBounds(250,100,50,40);
b4.setBounds(40,170,50,40);
b5.setBounds(110,170,50,40);
b6.setBounds(180,170,50,40);
bmul.setBounds(250,170,50,40);
b1.setBounds(40,240,50,40);
b2.setBounds(110,240,50,40);
b3.setBounds(180,240,50,40);
bsub.setBounds(250,240,50,40);

bdec.setBounds(40,310,50,40);
b0.setBounds(110,310,50,40);
beq.setBounds(180,310,50,40);
badd.setBounds(250,310,50,40);
bdel.setBounds(60,380,100,40);
bclr.setBounds(180,380,100,40);

f.add(t);
f.add(b7);
f.add(b8);
f.add(b9);

19
f.add(bdiv);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(bmul);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(bsub);
f.add(bdec);
f.add(b0);
f.add(beq);

f.add(badd);
f.add(bdel);
f.add(bclr);

f.setLayout(null);
f.setVisible(true);
f.setSize(350,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);

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);
b0.addActionListener(this);
badd.addActionListener(this);
bdiv.addActionListener(this);
bmul.addActionListener(this);

20
bsub.addActionListener(this);
bdec.addActionListener(this);
beq.addActionListener(this);
bdel.addActionListener(this);
bclr.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
t.setText(t.getText().concat("1"));
if(e.getSource()==b2)

t.setText(t.getText().concat("2"));
if(e.getSource()==b3)
t.setText(t.getText().concat("3"));
if(e.getSource()==b4)
t.setText(t.getText().concat("4"));
if(e.getSource()==b5)
t.setText(t.getText().concat("5"));
if(e.getSource()==b6)
t.setText(t.getText().concat("6"));
if(e.getSource()==b7)
t.setText(t.getText().concat("7"));
if(e.getSource()==b8)
t.setText(t.getText().concat("8"));
if(e.getSource()==b9)
t.setText(t.getText().concat("9"));
if(e.getSource()==b0)
t.setText(t.getText().concat("0"));
if(e.getSource()==bdec)
t.setText(t.getText().concat("."));
if(e.getSource()==badd)
{
a=Double.parseDouble(t.getText());
operator=1;
t.setText("");
}
if(e.getSource()==bsub)
{

21
a=Double.parseDouble(t.getText());
operator=2;
t.setText("");
}
if(e.getSource()==bmul)
{
a=Double.parseDouble(t.getText());

operator=3;
t.setText("");
}
if(e.getSource()==bdiv)
{
a=Double.parseDouble(t.getText());
operator=4;
t.setText("");
}
if(e.getSource()==beq)
{
b=Double.parseDouble(t.getText());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
t.setText(""+result);
}
if(e.getSource()==bclr)
t.setText("");
if(e.getSource()==bdel)

22
{
String s=t.getText();
t.setText("");
for(inti=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));
}
}

public static void main(String...s)


{
new Calc();
}
}

OUTPUT:

RESULT:
Thus the above program was executed successfully.

23
EX.NO: 7 CREATE MENUBAR IN JAVA SWING
DATE:

AIM:

To create menu bar in java swing and draw various shapes.

ALGORITHM:

Step 1: Start the program


Step 2: Create the java file to display the various shapes.
Step 3: Create the menuitem using the java swing control.
Step4: Save the program with the extension .java.
Step 5: Execute the program.

PROGRAM:
// TO create Menu Bar
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics2D;
class menu extends JFrame implements ActionListener
{
JMenuBarmb;
JMenu draw;
JMenuItemrect,line,oval,eMenuItem ;
Graphics2D g;
//Container c;
menu()
{
//c=getContentPane();
setLayout(null);
mb=new JMenuBar();

draw=new JMenu("SHAPES");
draw.setMnemonic('d');
mb.add(draw);

24
rect=new JMenuItem("Rectangle");
rect.addActionListener(this);
rect.setMnemonic('r');
draw.add(rect);

line=new JMenuItem("Line");
line.addActionListener(this);
line.setMnemonic('l');
draw.add(line);
oval=new JMenuItem("Oval");
oval.addActionListener(this);
oval.setMnemonic('o');
draw.add(oval);

eMenuItem = new JMenuItem("Exit");


eMenuItem.setMnemonic('e');
eMenuItem.addActionListener((ActionEvent event) -> {
System.exit(0);});
draw.add(eMenuItem);
setJMenuBar(mb);
}

public void actionPerformed(ActionEventae)


{
String str=ae.getActionCommand();
g=(Graphics2D) getGraphics();
if(str=="Rectangle")
{
clearScreen();
g.drawString("RECTANGLE",160,145);
g.drawRect(150,150,100,50);
}
if(str=="Line")
{
clearScreen();
g.drawString("LINE",160,145);
g.drawLine(150,150,400,350);
}

25
if(str=="Oval")
{
clearScreen();
g.drawString("OVAL",160,145);
g.drawOval(150,150,100,50);
}
}
public void clearScreen()
{
g.setPaint(Color.white);
// draw white on entire draw area to clear

g.fillRect(0, 0, getSize().width, getSize().height);


g.setPaint(Color.black);
}
public static void main(String args[])
{
menu f=new menu();
f.setTitle("my frame");
f.setSize(500,500);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

26
OUTPUT:

RESULT:
Thus the program was executed successfully.

27
EX.NO:8 CREATE A JAVA BEAN TO DRAW VARIOUS GRAPHICAL SHAPES
USING BDK OR WITHOUT BDK
DATE:

AIM:
To write a java bean program to draw various graphical shapes and display it without
using BDK.

ALGORITHM:
1. Start the Process.
2. Set the classpath for java as given below
C:\devi\bean>set path=%path%;c:\j2sdk1.4.1\bin;
3. Write the Source code as given below.
4. Save the above file as shapes. java
5. compile the file as
C:\devi\bean>javac shapes.java
C:\devi\bean>
6.If BDK is not used then execute the file as
C:\devi\bean>appletviewer shapes.java
7. Stop the process

PROGRAM:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="shapes" width=400 height=400></applet>*/
public class shapes extends Applet implements ActionListener
{
List list;
Label l1;
Font f;
public void init()
{
Panel p1=new Panel();
Color c1=new Color(255,100,230);
setForeground(c1);
f=new Font("Monospaced",Font.BOLD,20);
setFont(f);
l1=new Label("D R A W I N G V A R I O U S G R A P H I C A L S H A P E
S",Label.CENTER);

28
p1.add(l1);
add(p1,"NORTH");
Panel p2=new Panel();
list=new List(3,false);
list.add("Line");
list.add("Circle");
list.add("Ellipse");
list.add("Arc");
list.add("Polygon");
list.add("Rectangle");
list.add("Rounded Rectangle");
list.add("Filled Circle");
list.add("Filled Ellipse");
list.add("Filled Arc");
list.add("Filled Polygon");
list.add("Filled Rectangle");
list.add("Filled Rounded Rectangle");

p2.add(list);
add(p2,"CENTER");
list.addActionListener(this); }
public void actionPerformed(ActionEventae) {
repaint(); }
public void paint(Graphics g) {
inti;
Color c1=new Color(255,120,130);
Color c2=new Color(100,255,100);
Color c3=new Color(100,100,255);
Color c4=new Color(255,120,130);
Color c5=new Color(100,255,100);
Color c6=new Color(100,100,255);
if (list.getSelectedIndex()==0) {
g.setColor(c1);
g.drawLine(150,150,200,250); }
if (list.getSelectedIndex()==1) {
g.setColor(c2);
g.drawOval(150,150,190,190); }
if (list.getSelectedIndex()==2) {
g.setColor(c3);

29
g.drawOval(290,100,190,130); }
if (list.getSelectedIndex()==3) {
g.setColor(c4);
g.drawArc(100,140,170,170,0,120); }
if (list.getSelectedIndex()==4) {
g.setColor(c5);
int x[]={130,400,130,300,130};
int y[]={130,130,300,400,130};
g.drawPolygon(x,y,5); }
if (list.getSelectedIndex()==5) {
g.setColor(Color.cyan);
g.drawRect(100,100,160,150); }
if (list.getSelectedIndex()==6) {
g.setColor(Color.blue) g.drawRoundRect(190,110,160,150,85,85);

}
if (list.getSelectedIndex()==7)
{
g.setColor(c2);
g.fillOval(150,150,190,190);
}
if (list.getSelectedIndex()==8) {
g.setColor(c3);
g.fillOval(290,100,190,130); }
if (list.getSelectedIndex()==9) {
g.setColor(c4);
g.fillArc(100,140,170,170,0,120); }
if (list.getSelectedIndex()==10) {
g.setColor(c5);
int x[]={130,400,130,300,130};

int y[]={130,130,300,400,130};
g.fillPolygon(x,y,5); }
if (list.getSelectedIndex()==11)
{
g.setColor(Color.cyan);
g.fillRect(100,100,160,150);
}

30
if (list.getSelectedIndex()==12)
{
g.setColor(Color.blue);
g.fillRoundRect(190,110,160,150,85,85);
}}}

OUTPUT:

RESULT:
Thus the program was executed successfully.

31

You might also like