Java Lab Manual: Aurora'S PG College Moosarambagh Mca Department
Java Lab Manual: Aurora'S PG College Moosarambagh Mca Department
Java Lab Manual: Aurora'S PG College Moosarambagh Mca Department
MOOSARAMBAGH
MCA DEPARTMENT
JAVA LAB
MANUAL
Introduction to JAVA:
JAVA was developed by Sun Microsystems Inc in 1991, later acquired by
Oracle Corporation. It was developed by James Gosling and Patrick Naughton.
It is a simple programming language. Writing, compiling and debugging a
program is easy in java. It helps to create modular programs and reusable
code.
Java terminology
Before we start learning Java, lets get familiar with common java terms.
So, now that we understood that the primary function of JVM is to execute the
bytecode produced by compiler. Each operating system has different JVM,
however the output they produce after execution of bytecode is same
across all operating systems. That is why we call java as platform
independent language.
bytecode
As discussed above, javac compiler of JDK compiles the java source code into
bytecode so that it can be executed by JVM. The bytecode is saved in a .class
file by compiler.
These are the basic java terms that confuses beginners in java. For complete
java glossary refer this link:
https://docs.oracle.com/javase/tutorial/information/glossary.html
Main Features of JAVA
Java is a platform independent language
Compiler(javac) converts source code (.java file) to the byte code(.class file).
As mentioned above, JVM executes the bytecode produced by compiler. This
byte code can run on any platform such as Windows, Linux, Mac OS etc.
Which means a program that is compiled on windows can run on Linux and
vice-versa. Each operating system has different JVM, however the output they
produce after execution of bytecode is same across all operating systems. That
is why we call java as platform independent language.
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Simple
Java is considered as one of simple language because it does not have complex
features like Operator overloading, Multiple inheritance, pointers and Explicit
memory allocation.
Robust Language
Robust means reliable. Java programming language is developed in a way that
puts a lot of emphasis on early checking for possible errors, that’s why java
compiler is able to detect errors that are not easy to detect in other
programming languages. The main features of java that makes it robust are
garbage collection, Exception Handling and memory allocation.
Secure
We don’t have pointers and we cannot access out of bound arrays (you get
ArrayIndexOutOfBoundsException if you try to do so) in java. That’s why
several security flaws like stack corruption or buffer overflow is impossible to
exploit in Java.
Java is distributed
Using java programming language we can create distributed applications.
RMI(Remote Method Invocation) and EJB(Enterprise Java Beans) are used for
creating distributed applications in java. In simple words: The java programs
can be distributed on more than one systems that are connected to each other
using internet connection. Objects on one JVM (java virtual machine) can
execute procedures on a remote JVM.
Multithreading
Java supports multithreading. Multithreading is a Java feature that allows
concurrent execution of two or more parts of a program for maximum
utilisation of CPU.
Portable
As discussed above, java code that is written on one machine can run on
another machine. The platform independent byte code can be carried to any
platform for execution that makes java code portable.
PROGRAMS:
class Box
{
double l,w,h;
}
System.out.println("BOX b1 Details:");
System.out.println("Height" + b1.h);
System.out.println("Width" + b1.w);
System.out.println("Length" + b1.l);
System.out.println("BOX b2 Details");
System.out.println("Height" + b2.h);
System.out.println("Width" + b2.w);
System.out.println("Length" + b2.l);
}
}
Output:
BOX b1 Details:
Height 20.0
Width 10.0
Length 30.0
BOX b2 Details
Height 30.0
Width 40.0
Length 50.0
2. Program To Demostrate Class With Methods
class Rectangle
{
double h,w;
void setValues(double x, double y)
{
h=x;
w=y;
}
Output:
class Test
{
int x;
void increment(Test o)
{
o.x=x*10;
}
}
System.out.println("t.x=" + t.x);
System.out.println("t1.x=" + t1.x);
t.increment(t1);
System.out.println("After calling increment() t1.x=" + t1.x);
}
}
Output:
t.x=10
t1.x=20
After calling increment() t1.x=100
4. Program to demonstrate Constructor Overloading
class Student
{
int rollno;
String name;
double m1,m2,m3,avg;
Student()
{
rollno=1;
name="xyz";
m1=75;
m2=80;
m3=70;
avg=0;
}
double calculate()
{
avg = (m1+m2+m3)/3;
return(avg);
}
void details()
{
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollno);
System.out.println("Average: " + calculate());
}
}
Output:
Name: xyz
Roll No: 1
Average: 75.0
Name: xyz
Roll No: 1
Average: 75.0
5.Program to implement Stack operations using Interface
interface Stack
{
void push(int i);
int pop();
}
class Stack1
{
int stk[];
Stack1()
{
stk = new int[10];
}
}
Output:
9
8
7
6
5
4
3
2
1
Stack is empty
0
6. Program to demonstrate 'this' keyword
class ThisDemo
{
int x,y;
ThisDemo(int x,int y)
{
this.x=x;
this.y=y;
}
void display()
{
System.out.println("x=" + x);
System.out.println("y=" + y);
}
}
Output:
x=1
y=2
7.Program to demonstrate 'static' keyword
class StatDemo
{
static int x;
int y;
static
{
x=4;
System.out.println("x=" + x);
}
class StaticDemo
{
public static void main(String[] args)
{
StatDemo d1 = new StatDemo();
d1.y=10;
System.out.println("d1.y=" + d1.y);
System.out.println("x=" + StatDemo.x);
StatDemo.display();
}
}
OUTPUT:
x=4
d1.y=10
x=4
y=10
8. Program to demonstrate Command Line Arguments
class Demo5
{
public static void main(String[] args)
{
String str = args[0];
int i = Integer.parseInt(args[1]);
float f = Float.parseFloat(args[2]);
System.out.println("Adding " + i + " and " + f + " gives " + (f+i));
System.out.println("Multiplying " + i + " and " + f + " gives " + (f*i));
System.out.println("String " + str + " length is " + str.length());
System.out.println("For String " + str + " character at e is" + str.charAt(3));
}
}
Output:
class Outer
{
int ox;
Outer()
{
ox = 10;
}
class inner
{
int ix;
inner()
{
ox = 30;
ix = 40;
}
void display()
{
System.out.println();
System.out.println("Inner Class display");
System.out.println("ox=" + ox);
System.out.println("ix=" + ix);
}
}
void displayOuter()
{
System.out.println("Outer class display");
System.out.println("ox=" + ox);
inner k = new inner();
k.display();
}
class Inher1
{
int i;
Inher1()
{
i = 10;
}
Inher1(int i)
{
this.i = i;
}
void show()
{
System.out.println("In Superclass i = " + i);
}
}
Inher2(int i, int k)
{
this.i = i;
this.k = k;
}
void showK()
{
show();
System.out.println("k=" + k);
}
Output:
In Superclass i = 10
k=20
In Superclass i = 30
k=40
11.Program to demonstrate 'super' keyword
class Figure
{
int l,b;
Figure()
{
l=10;
b=5;
System.out.println("Super class constructor");
}
Figure(int i, int j)
{
System.out.println("Super class Constructor");
l=i;
b=j;
}
void area()
{
System.out.println("There is no area for the figure");
}
}
void area()
{
System.out.println("area=" + (l*b));
}
void volume()
{
System.out.println("volume=" + (l*b*h));
}
}
void perimeter()
{
System.out.println("Perimeter of rectangle" + (2*(l+b)));
}
SuperDemo(int l, int b)
{
super(l,b);
}
f.area();
c.area();
s.area();
c.volume();
s.perimeter();
f1=f;
f1.area();
f1=c;
f1.area();
f1=s;
f1.area();
}
}
Output:
void area()
{
System.out.println("Area of cube "+ (l*b*h));
}
void showlbh()
{
showlb();
System.out.println("h=" + h);
}
}
Output:
package mypack;
class Employee
{
int eid;
String name;
double hra,da,sal,basic;
Employee(int id,String n, double hr, double da)
{
eid=id;
name=n;
hra=hr;
basic=3000.50;
this.da=da;
sal=50;
}
void calculate()
{
sal=hra+basic+da;
System.out.println("empid: " + eid);
System.out.println("Name: " + name);
System.out.println("HRA: " + hra);
System.out.println("DA: " + da);
System.out.println("Basic: " + basic);
System.out.println("Sal: " + sal);
}
Output:
empid1
Nameabc
HRA: 30.1
DA: 3000.0
Basic: 3000.5
Sal: 6030.6
14. Program to demonstrate Interface
interface myintf
{
int x=10;
void call();
void callMe(int x);
}
Output:
class ExcepDemo
{
public static void main(String[] args)
{
int a=0;
int d=30;
try
{
System.out.println("a=" + a +" d=" + d);
d=d/a;
System.out.println("This will not be executed");
}
catch(ArithmeticException e)
{
System.out.println("Exception raised "+ e);
a=5;
d=d/a;
System.out.println("This will be executed");
}
Output:
a=0 d=30
Exception raised java.lang.ArithmeticException: / by zero
This will be executed
This also will be executed
16. Program to demonstrate MultiCatch
class Multicatch
{
public static void main(String[] args)
{
int a=args.length;
int d=40;
int c[]={1,2,3,4};
try
{
if(a==0)
d=d/a;
if(a==1)
d=d/(a-a);
if(a==2)
System.out.println("Value of c[7] = " + c[7]);
System.out.println("This will never be executed");
}
catch(ArithmeticException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array problem" + e);
}
catch(Exception e)
{
System.out.println("All kinds of exceptions " + e);
}
Output:
java.lang.ArithmeticException: / by zero
This will be executed
17. Program to demonstrate 'throws' clause and finally block
class ThrowDemo
{
void throwDemometh()throws Exception
{
try
{
System.out.println("This is within ThrowDemo");
throw new NullPointerException("From my code");
}
catch(Exception e)
{
System.out.println(e);
}
}
catch(Exception e)
{
System.out.println("Recaught" + e);
}
finally
{
System.out.println("In finally");
}
}
}
OUTPUT:
try
{
d.withdraw(500);
d.withdraw(2000);
}
catch(BankExcep e)
{
System.out.println(e);
}
}
}
Output:
class ArrDemo
{
public static void main(String[] args)
{
int a[][] = new int[3][3];
int i,j,k=0;
OUTPUT:
012
345
678
THREADS
/* Program to demonstrate Thread */
class ThreadDemo
{
public static void main(String[] args)
{
System.out.println("Within main thread");
try
{
Thread t = Thread.currentThread();
System.out.println(t);
t.setName("Demo Thread");
System.out.println(t);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Main existing");
}
}
}
Output:
In Main
Main0
A
B
Main existing
Main1
C
D
Main existing
E
Main2
F
Main existing
Main3
G
H
Main existing
Main4
I
J
Main existing
/* Program to demonstrate Thread using Runnable Interface */
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
Output:
Main: 0
child
♂child
Main: 1
♀child
child
Main: 2
♫child
☼child
Main: 3
►child
◄child
Main: 4
/* Program to demonstrate Join and isalive */
void world()
{
try
{
for(int i=0; i<5; i++)
{
System.out.println("world");
Thread.sleep(100);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
void hello()
{
try
{
for(int i=0; i<5; i++)
{
System.out.println("hello");
Thread.sleep(100);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
void hai()
{
try
{
for(int i=0; i<5; i++)
{
System.out.println("hai");
Thread.sleep(50);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
try
{
c1.t.join();
c2.t.join();
c3.t.join();
}
catch(Exception e)
{
System.out.println(e);
}
Output:
c1 is alive: true
c2 is alive: true
c3 is alive: true
world
hello
hai
hai
world
hello
hai
hai
world
hello
hai
world
hello
world
hello
Now c1 is alive: false
Now c2 is alive: false
Now c3 is alive: false
/* Program to demonstrate Synchronization */
class Shared
{
synchronized public void call(String str)
{
System.out.print("[" + str);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("]");
}
}
class SyncDemo
{
public static void main(String[] argS)
{
Shared s = new Shared();
Threads t1 = new Threads(s,"Hello");
Threads t2 = new Threads(s,"World");
try
{
t1.join();
t2.join();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
[Hello]
[World]
/* Program to demonstrate Interthread Communication */
class Q
{
int x;
boolean vs = false;
synchronized int get()
{
if(!vs)
{
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
System.out.println("got: " + x);
notify();
vs=false;
return x;
}
this.x=x;
System.out.println("put:x " + x);
notify();
vs=true;
}
}
class Consumer extends Thread
{
Q q;
Consumer(Q q)
{
this.q=q;
start();
}
class InterThread
{
public static void main(String[] args)
{
Q q = new Q();
Consumer c = new Consumer(q);
Producer p = new Producer(q);
try
{
c.join();
p.join();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
got: 527
put:x 528
got: 528
put:x 529
got: 529
put:x 530
got: 530
put:x 531
got: 531
put:x 532
got: 532
put:x 533
got: 533
put:x 534
got: 534
put:x 535
got: 535
put:x 536
got: 536
put:x 537
got: 537
put:x 538
got: 538
put:x 539
got: 539
APPLETS
/* Program to demonstrate Applet */
import java.awt.*;
import java.applet.*;
<HTML>
<applet code="FirstApp.class" height=400 width=400>
</applet>
</HTML>
/* Program to demonstrate Methods in Graphics class */
import java.applet.*;
import java.awt.*;
<HTML>
<applet code="GraphicsDemo.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
/* Program to demonstrate Threads using Applets */
import java.awt.*;
import java.net.*;
import java.applet.*;
try
{
Thread.sleep(5000);
ac.showDocument(new URL(getDocumentBase(),"FirstApp.html"));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
<HTML>
<applet code="AppThreadDemo.class" height=400 width=400>
</applet>
</HTML>
OUPUT:
/* Program to demonstrate Image within Applet*/
import java.awt.*;
import java.applet.*;
<HTML>
<applet code="DrawImageApp.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
/* Program to demonstrate Param Tag */
import java.awt.*;
import java.applet.*;
<HTML>
<applet code="ParamDemo.class" height=400 width=400>
<param name="fgcolor" value="red">
<param name="bgcolor" value="black">
</applet>
</HTML>
OUTPUT:
/* Program to demonstrate Font Class*/
import java.awt.*;
import java.applet.*;
<HTML>
<applet code="FontDemo.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
/* Program to demonstrate Digital Clock */
import java.awt.*;
import java.applet.*;
import java.util.*;
<HTML>
<applet code="TimeDemo.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
AWT AND EVENT
HANDLING
/* Program to create Frame*/
import java.awt.*;
OUTPUT:
/* Program to demonstrate Frame with Labels and Buttons */
import java.awt.*;
}
OUTPUT:
/* Program to demonstrate Frames using button , label, textfield.*/
import java.awt.*;
FrameDemo5()
{
u=new Label("Username ");
p=new Label("Password ");
d=new Label("Details");
o=new Button("OK");
c=new Button("Cancel");
tf1=new TextField(30);
tf2=new TextField(30);
ta=new TextArea("Enter your Address",4,5);
tf2.setEchoChar('*');
setLayout(new FlowLayout());
add(u);
add(tf1);
add(p);
add(tf2);
add(d);
add(ta);
add(o);
add(c);
setSize(400,400);
setVisible(true);
}
OUTPUT:
/* Program to demonstrate Frame with CheckBox and CheckBoxGroup */
import java.awt.*;
FrameDemo3()
{
super("Hello");
l1=new Label("QUALIFICATION");
l2=new Label("GENDER");
l3=new Label("SUBJECT");
cb1=new Checkbox("MCA");
cb2=new Checkbox("BSC");
cbg=new CheckboxGroup();
cb3=new Checkbox("M",true,cbg);
cb4=new Checkbox("F",false,cbg);
c=new Choice();
c.add("Java");
c.add("C++");
c.add("C");
c.add("Cobol");
setLayout(new FlowLayout());
add(l1);
add(cb1);
add(cb2);
add(l2);
add(cb3);
add(cb4);
add(l3);
add(c);
setSize(400,400);
setVisible(true);
}
OUTPUT:
/* Program to demonstrate Complex Frame( Checkboxes) */
import java.awt.*;
FrameDemo4()
{
super("Hello");
la=new Label("Skill Set");
o=new Checkbox("O.S");
s=new Checkbox("Software");
cbg1=new CheckboxGroup();
w=new Checkbox("Windows",true,cbg1);
d=new Checkbox("Dos",false,cbg1);
l=new Checkbox("Linux",false,cbg1);
p=new Checkbox("Unix",false,cbg1);
cbg2=new CheckboxGroup();
j=new Checkbox("Java",true,cbg2);
cp=new Checkbox("C++", false,cbg2);
c=new Checkbox("Cobol",false,cbg2);
setLayout(new FlowLayout());
add(la);
add(o);
add(w);
add(d);
add(l);
add(p);
add(s);
add(j);
add(cp);
add(c);
setSize(400,400);
setVisible(true);
}
OUTPUT:
/* Program to demonstrate DialogBox*/
import java.awt.*;
catch(Exception e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
import java.awt.*;
OUTPUT:
/* Program to demonstrate Border Layout */
import java.awt.*;
import java.applet.*;
<HTML>
<applet code="DemoApp.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
/* Program to demonstrate Grid Layout */
import java.awt.*;
setVisible(true);
setSize(300,300);
}
import java.awt.*;
OUPUT:
/* Program to demonstrate Implementation of Action Listener*/
import java.awt.*;
import java.awt.event.*;
if(ae.getSource()==b2)
{
l.setText("Two");
}
}
OUTPUT:
/* Program to graphs using Action Listener */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
if(e.getSource()==s)
{
k=2;
repaint();
}
if(e.getSource()==l)
{
k=3;
repaint();
}
}
OUTPUT:
/* Program to demonstrate implementation of ItemListener */
import java.awt.*;
import java.awt.event.*;
OUTPUT:
/* Program to show the use of Item Listener */
import java.awt.*;
import java.awt.event.*;
OUTPUT:
/* Program to demonstrate implementation of MouseListener */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
<HTML>
<applet code="MouseDemo.class" height=400 width=400>
</applet>
</HTML>
OUTPUT:
/* Program to show the use of MouseListener and increment the value of the
button.
import java.awt.*;
import java.awt.event.*;
OUTPUT:
/* Program to demonstrate FocusListener and WindowListener */
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
AdapterDemo()
{
u=new Label("Username");
p=new Label("Password");
tf1=new TextField(30);
tf2=new TextField(30);
o=new Button("Ok");
c=new Button("Reset");
tf2.setEchoChar('*');
setLayout(new FlowLayout());
add(u); add(tf1); add(p); add(tf2); add(o); add(c);
setVisible(true);
setSize(300,300);
o.addActionListener(this);
c.addActionListener(this);
addWindowListener(new MyAdapter());
}