java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by...

40
1 Jawaharlal Nehru Engineering College Laboratory Manual Programming in JAVA For S.Y (M.C.A) Students 16, Jun 2010 – Rev 00 – MCA – ISO 9000 Tech Document Author JNEC, Aurangabad

Transcript of java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by...

Page 1: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

1

Jawaharlal Nehru Engineering College

Laboratory Manual

Programming in JAVA

For S.Y (M.C.A) Students

16, Jun 2010 – Rev 00 – MCA – ISO 9000 Tech Document

Author JNEC, Aurangabad

Page 2: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

2

FORWARD

It is my great pleasure to present this laboratory manual for T.Y (M.C.A) students for the subject of PECS. As a student, many of you may be wondering with some of the questions in your mind regarding the subject and exactly what has been tried is to answer through this manual. As you may be aware that MGM has already been awarded with ISO 9000 certification and it is our endure to technically equip our students taking the advantage of the procedural aspects of ISO 9000 Certification. Faculty members are also advised that covering these aspects in initial stage itself, will greatly relived them in future as much of the load will be taken care by the enthusiasm energies of the students once they are conceptually clear.

Prof. Dr.S.D.DESHMUKH Principal

Page 3: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

3

LABORATORY MANUAL CONTENTS

This manual is intended for the students T.Y (M.C.A) engineering for the subject of PECS. In the subject of this manual typically contains practical/Lab Sessions we have dealt with PECS. Students are advised to thoroughly go through this manual rather than only topics mentioned are the key to understanding and conceptual visualization of theoretical aspects covered in the books. Good Luck for your Enjoyable Laboratory Sessions

Prof. K. D. Zinjurde Assist. Prof. A. R.Tungar HOD MCA (Dept) Lecturer, MCA (Dept)

Page 4: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

4

DOs and DON’T DOs in Laboratory: 1. Do not handle any equipment before reading the instructions/Instruction manuals

2. Read carefully the power ratings of the equipment before it is switched on whether ratings 230

V/50

Hz or 115V/60 Hz. For Indian equipments, the power ratings are normally 230V/50Hz. If you have

equipment with 115/60 Hz ratings, do not insert power plug, as our normal supply is 230V/50 Hz, which

will damage the equipment.

3. Observe type of sockets of equipment power to avoid mechanical damage

4. Do not forcefully place connectors to avoid the damage

5. Strictly observe the instructions given by the teacher/Lab Instructor

Instruction for Laboratory Teachers::

1. Submission related to whatever lab work has been completed should be done during the next lab

session. The immediate arrangements for printouts related to submission on the day of practical

assignments.

2. Students should be taught for taking the printouts under the observation of lab teacher.

3. The promptness of submission should be encouraged by way of marking and evaluation patterns

that will benefit the sincere students.

Page 5: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

5

SUBJECT INDEX

1. Introduction JAVA Program’s.

2. To Demonstrate Method’s, Constructor’s, This Key Word.

3. To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference.

4. To Demonstrate Inheritance, Method Overriding.

5. To Demonstrate Packages.

6. To Demonstrate Interfaces, Exception Handling.

7. To Demonstrate Multithreading.

8. To Demonstrate Applets.

9. To Demonstrate JDBC.

10. To Demonstrate I/O Packages.

Page 6: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

6

(2 Hours):- 1Practical

1. Lab Exercises:

Introduction to JAVA Programs

First Program I. //Simple First Program for Java

class welcome

{

public static void main (String args [])

{

System.out.println ("Welcome To Java 2");

}

}

Out Put:- Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Admin>cd\

C:\>cd java

C:\Java>cd jdk*

C:\Java\jdk1.6.0>cd bin

C:\Java\jdk1.6.0\bin>javac welcome.java

C:\Java\jdk1.6.0\bin>java welcome

Welcome To Java 2 C:\Java\jdk1.6.0\bin>

Page 7: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

7

Second Program II. //Simple Program for Documentation Comment

/** Second- A test for JDK

@author-R.Krishnamoorthy and S.Prabhu

@version 1.0

*/

public class second

{

/** the main method is called first by the java interpreter*/

//Javadoc ignore this comment

public static void main (String args [])

{

int a, b, c; //variable declaration

a=5; //assign a to 5

b=10; //assign b to 10

c=a+b; //add

/*in the above statements we declare three variables of type integer

This is also ignore by the javadoc

*/

System.out.println ("\n A+B= "+c);

}

}

Out Put:- Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Admin>cd\

C:\>cd java

C:\Java>cd jdk*

C:\Java\jdk1.6.0>cd bin

C:\Java\jdk1.6.0\bin>javac second.java

C:\Java\jdk1.6.0\bin>java second

A+B= 15

C:\Java\jdk1.6.0\bin>

Page 8: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

8

Third Program

III. //Simple Program for Command Line Argument

class third

{

public static void main (String args[])

{

System.out.println ("hai");

System.out.println (args [0]);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac third.java

C:\Java\jdk1.6.0\bin>java third kavita Hai

Kavita

C:\Java\jdk1.6.0\bin>

Page 9: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

9

Fourth Program IV. //Simple Program for Command Line Argument using try and catch

class fourth

{

public static void main (String args [])

{

System.out.println ("Hai");

try

{

System.out.println (args [0]);

}

catch (Exception e)

{

System.out.println ("Enter the Parameters...");

}

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac fourth.java

C:\Java\jdk1.6.0\bin>java fourth

Hai

Enter parameter... C:\Java\jdk1.6.0\bin>javac fourth.java

C:\Java\jdk1.6.0\bin>java fourth Atul Avani Hai

Atul

Page 10: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

10

Fifth Program V. //Program to calculate the length of the command line argument

class fifth

{

public static void main (String args [])

{

int i=0;

for (i=0; i<args.length; i++)

{

System.out.println (i+"Hello"+args[i]);

}

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac fifth.java

C:\Java\jdk1.6.0\bin>java fifth JOHNE JONY JANARDHAN 0 Hello JOHNE

1 Hello JONY

2 Hello JANADHAN

Page 11: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

11

Sixth Program VI. //Program to calculate the Interactive input through command line argument

import java.io.*;

class interactiveinput

{

public static void main (String args [])

{

String s="";

DataInputStream d=new DataInputStream (System. in);

try

{

System.out.println ("Enter your Name ");

s=d.readLine ();

}

catch (Exception e)

{}

System.out.println (s+"\t Welcome to JAVA");

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac interactiveinput.java

Note: interactiveinput.java uses or overrides a deprecated API.

Note: Recompile with –Xlint: deprecation for details.

C:\Java\jdk1.6.0\bin>java interactiveinput

Enter your Name

KAVITA ATUL PATIL

KAVITA ATUL PATIL Welcome to JAVA C:\Java\jdk1.6.0\bin>

Page 12: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

12

(2 Hours):- 2Practical

2. Lab Exercises: -

To Demonstrate Method’s, Constructor’s, And This Key Word.

First Program

I. //Simple Program for Classes & Creation of Objects Only. class Triangle //class Definition

{

double base;

double height;

double area;

}

// The following class declare an object of the type triangle

class tritest

{

public static void main(String args [])

{

Triangle m=new Triangle();//object created

m.base=10.0;//assignment;

m.height=20.0;

m.area=0.5*m.base*m.height;

System.out.println ("The area of a Triangle is "+m.area);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac tritest.java

C:\Java\jdk1.6.0\bin>java tritest

The area of a Triangle is 100.0

C:\Java\jdk1.6.0\bin>

Page 13: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

13

Second Program

II. //Simple Program for Classes, Methods & Objects. class Rectangle

{

int length,width;

void getData(int x,int y)

{

length=x;

width=y;

}

int rectArea()

{

int area=length*width;

return (area);

}

}

class AreaRect

{

public static void main(String args[])

{

int area1,area2;

Rectangle rect1=new Rectangle ();

Rectangle rect2=new Rectangle ();

rect1.length=15;

rect1.width=10;

area1=rect1.length*rect1.width;

rect2.getData (20, 2);

area2=rect2.rectArea ();

System.out.println ("Area1 = "+area1);

System.out.println ("Area2 = "+area2);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac AreaRect.java

C:\Java\jdk1.6.0\bin>java AreaRect

Area1 = 150

Area2 = 40

Page 14: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

14

Third Program

III. //A Simple Program for Default Constructor for adding two

numbers

class Add

{

int a,b;

Add () //Default Constructor

{

System.out.println ("It's a Default Constructor");

a=10;

b=20;

}

void sum()

{

System.out.println ("a+b= "+ (a+b));

}

}

class addition

{

public static void main(String args [])

{

//invoke the default constructor

Add t=new Add ();

t.sum ();

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac addition.java

C:\Java\jdk1.6.0\bin>java addition

It's A Default Constructor

a+b= 30

C:\Java\jdk1.6.0\bin>

Page 15: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

15

Fourth Program

IV. //A Simple Program for Parameterized Constructor to initialize the

dimension of a triangle class Rectangle

{

int length,width;

Rectangle (int x, int y)//Parameterized constructor

{

length=x;

width=y;

}

int rectArea()

{

return (length*width);

}

}

class RectArea1

{

public static void main(String args[])

{

Rectangle rect1=new Rectangle (15, 10); //Parameter passing

Rectangle rect2=new Rectangle (5, 2);

int area1=rect1.rectArea();

System.out.println ("Area1 = "+area1);

int area2=rect2.rectArea();

System.out.println ("Area2 = "+area2);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac RectArea1.ja

C:\Java\jdk1.6.0\bin>java RectArea1

Area1 = 150

Area2 = 10

Page 16: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

16

Fifth Program

V. //A Simple Program for Parameterized Constructor for adding two

numbers //A Simple Program for Parameterized Constructor

class Add

{

int a,b;

Add(int i,int j) //Parameterized Constructor

{

System.out.println ("It's a Parameterized Constructor");

a=i;

b=j;

}

int sum()

{

return (a+b);

}

}

class addition

{

public static void main(String args [])

{

//invoke the Parameterized constructor

Add t=new Add (10,20);

System.out.println ("a+b = "+t.sum ());

}

}

Out Put:-

C:\Java\jdk1.6.0\bin>javac addition.java

C:\Java\jdk1.6.0\bin>java addition

It's a Parameterized Constructor

a+b = 30

C:\Java\jdk1.6.0\bin>

Page 17: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

17

Sixth Program

VI. //A Simple Program for Copy Constructor for adding two numbers class Copy

{

int p,q;

public Copy(int a,int b)

{

p=a;

q=b;

}

public Copy(Copy c)//Copy Constructor

{

p=c.p;

p=c.p;

}

void view()

{

System.out.println("P = "+p+"Q= "+q);

}

}

class CopyTest

{

public static void main(String args [])

{

Copy t=new Copy(10,15);

System.out.println("First Constructor");

t.view();

Copy t1=new Copy(t);//invoke copy constructor

System.out.println("Second Constructor");

t1.view();

}

}

Out Put:- C:\jdk1.6.0\bin>javac CopyTest.java

C:\jdk1.6.0\bin>java CopyTest

First Constructor

P = 10Q= 15

Second Constructor

P = 10Q= 0

C:\jdk1.6.0\bin>

Page 18: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

18

Seventh Program

VII. //A Simple Program for This Keyword. class thistest

{

String s="Instance Variable";

void view()

{

String s="Local Variable";

System.out.println ("S= "+s);

System.out.println ("This s= "+this.s);

}

public static void main(String args [])

{

thistest t=new thistest();

t.view();

}

}

Out Put:- C:\jdk1.6.0\bin>javac thistest.java

C:\jdk1.6.0\bin>java thistest

S= Local Variable

This s= Instance Variable

C:\jdk1.6.0\bin>

Page 19: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

19

Eighth Program

VIII. //A Second Simple Program for This Keyword.

class Rectangle

{

int length,breadth;

void show(int length, int breadth)

{

this.length=length;

this.breadth=breadth;

}

int calculate()

{

return (length*breadth);

}

}

public class thisoperator

{

public static void main(String args [])

{

Rectangle r=new Rectangle();

r.show(5,6);

int area=r.calculate();

System.out.println("The area of Rectangle ="+area);

}

}

Out Put:- C:\jdk1.6.0\bin>javac thisoperator.java

C:\jdk1.6.0\bin>java thisoperator

The area of Rectangle =30

C:\jdk1.6.0\bin>

Page 20: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

20

(2 Hours):- 3Practical

3. Lab Exercises: -

To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference

First Program

//Simple Program for Method Overloading class Arith

{

int a,b;

void addition()

{

a=10;

b=20;

int c=a+b;

System.out.println("Addition Of A and B ="+c);

}

void addition(int p)

{

a=p;

b=20;

int c=a+b;

System.out.println("Addition Of A and B ="+c);

}

void addition(int i,int j)

{

a=i;

b=j;

int c=a+b;

System.out.println("Addition Of A and B ="+c);

}

}

class MethodOverloading

{

public static void main(String args[])

{

Arith t=new Arith();

t.addition();

t.addition(2);

t.addition(1,1);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac MethodOverloading.java

C:\Java\jdk1.6.0\bin>java MethodOverloading

Addition Of A and B =30

Addition Of A and B =22

Addition Of A and B =2

Page 21: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

21

Second Program

//Simple Program for Constructor Overloading class Room

{

float length,breadth;

Room(float x,float y)

{

length=x;

breadth=y;

}

Room(float x)

{

length=breadth=x;

}

float area()

{

return(length*breadth);

}

}

class Area

{

public static void main(String args[])

{

Room t1=new Room(15,2);

System.out.println("Area of a Room1 = "+t1.area());

Room t2=new Room(2);

System.out.println("Area of a Room2 = "+t2.area());

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac Area.java

C:\Java\jdk1.6.0\bin>java Area

Area of a Room1 = 30.0

Area of a Room2 = 4.0

Page 22: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

22

Third Program

//Simple type are passed by value class Test

{

void meth(int i,int j)

{

i=i*2;

i=i/2;

}

}

class CallByValue

{

public static void main(String args[])

{

Test t=new Test();

int a=4,b=2;

System.out.println("a & b before call"+a+""+b);

t.meth(a,b);

System.out.println("a & b After call"+a+""+b);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac CallByValue.java

C:\Java\jdk1.6.0\bin>java CallByValue

a & b before call42

a & b After call42

Page 23: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

23

Fourth Program

//Simple type are passed by Reference class Test

{

int a,b;

void get(int i,int j)

{

a=i;

b=j;

}

void meth(Test t)

{

t.a=t.a*2;

t.b=t.b/2;

}

}

class CallBy

{

public static void main(String args[])

{

Test t1=new Test();

t1.get(10,4);

System.out.println("Value of a and b Before Call meth Function =" +t1.a+" "+t1.b);

t1.meth(t1);

System.out.println("Value of a and b After Call meth Function =" +t1.a+" "+t1.b);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac CallBy.java

C:\Java\jdk1.6.0\bin>java CallBy

Value of a and b Before Call meth Function =10 4

Value of a and b After Call meth Function =20 2

Page 24: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

24

Fifth Program

//Simple for Returning an Object class Test

{

int a;

Test(int i)

{

a=i;

}

Test incrByTen()

{

Test temp=new Test(a+10);

return temp;

}

}

class RetOb

{

public static void main(String args[])

{

Test t1=new Test(2);

Test t2;

t2=t1.incrByTen();

System.out.println("t1.a = "+t1.a);

System.out.println("t2.a = "+t2.a);

t2=t2.incrByTen();

System.out.println("t2.a After Second increament = "+t2.a);

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac RetOb.java

C:\Java\jdk1.6.0\bin>java RetOb

t1.a = 2

t2.a = 12

t2.a After Second increament = 22

Page 25: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

25

(2 Hours):- 4Practical

4. Lab Exercises

To Demonstrate Inheritance, Method Overriding.

First Program

//Simple program of Inheritance

class A

{ int i,j;

void showj()

{

System.out.println("i & j = "+i+" & "+j);

}}

class B extends A

{ int t;

void showt()

{ System.out.println("T = "+t); }

void sum()

{int m = i+j+t;

System.out.println("i+j+t="+m);

}}

class SimpleInheritance

{ public static void main(String args[])

{ A superb1=new A();

B subb2=new B();

superb1.i=10;

superb1.j=20;

System.out.println("Contents of superb1 = ");

superb1.showj();

System.out.println(" ");

subb2.i=7;

subb2.j=8;

subb2.t=9;

System.out.println("Contents of subb2 = ");

subb2.showj();

subb2.showt();

System.out.println(" ");

System.out.println("Sum of i,j & t in subb2 = ");

subb2.sum();

}}

Out Put:- C:\Java\jdk1.6.0\bin>javac SimpleInheritance.java

C:\Java\jdk1.6.0\bin>java SimpleInheritance

Contents of superb1 =

i & j = 10 & 20

Contents of subb2 =

i & j = 7 & 8

T = 9

Sum of i,j & t in subb2 =

i+j+t=24

Page 26: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

26

Second Program

//Simple program of Inheritance

class Alpha

{

int x,y;

void viewxy()

{ System.out.println("X = "+x+"Y = "+y);

}

void add()

{ System.out.println("X+Y = "+(x+y));

}

}

class Beta extends Alpha

{

int z;

void viewz()

{ System.out.println("Z = "+z);

}

void sum()

{ System.out.println("X+Y+Z = "+(x+y+z));

}

}

class inheritance

{

public static void main(String args[]) {

Alpha b=new Alpha();

Beta d=new Beta();

b.x=10;

b.y=20;

System.out.println("Base class values");

b.viewxy();

b.add();

System.out.println("Derived class values");

d.x=4;

d.y=5;

d.z=6;

d.viewxy();

d.viewz();

d.sum(); }

}

Out Put:- C:\Java\jdk1.6.0\bin>javac inheritance.java

C:\Java\jdk1.6.0\bin>java inheritance

Base class values

X = 10Y = 20

X+Y = 30

Derived class values

X = 4Y = 5

Z = 6

X+Y+Z = 15

Page 27: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

27

Third Program

//Simple program of Access Modifiers in Inheritance

class Access

{ int i;

public int j;

private int k;

protected int p;

void setValue(int i1,int j1,int k1,int p1)

{ i=i1;

j=j1;

k=k1;

p=p1;

}

void base_view()

{ System.out.println("\t\t Base class");

System.out.println("Default = "+i);

System.out.println("Public = "+j);

System.out.println("Private = "+k);

System.out.println("Protected = "+p);

}

}

class sub extends Access

{ void derived_view()

{ System.out.println("\t\t Derived Class");

System.out.println("Default = "+i);

System.out.println("Public = "+j);

//System.out.println("Private = "+k);

System.out.println("Protected = "+p);

}

}

class memberaccess

{ public static void main(String args[])

{ sub t=new sub();

t.setValue(1,2,3,4);

t.base_view();

t.derived_view();

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac memberaccess.java

C:\Java\jdk1.6.0\bin>java memberaccess

Base class

Default = 1

Public = 2

Private = 3

Protected = 4

Derived Class

Default = 1

Public = 2

Protected = 4

Page 28: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

28

Forth Program

//Simple program of Inheritance using super key word class trial

{

int a,b;

trial(int a1,int b1)

{

a=a1;

b=b1;

}

int geta()

{

return a;

}

int getb()

{

return b;

}

}

class demotrial extends trial

{

int c;

demotrial(int p,int q,int r)

{

super(p,q);

c=r;

}

int getc()

{

return c;

}

}

class DemoOfSuper

{

public static void main(String args[])

{

demotrial t=new demotrial(1,2,3);

System.out.println("A = "+t.geta());

System.out.println("B = "+t.getb());

System.out.println("C = "+t.getc());

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac DemoOfSuper.java

C:\Java\jdk1.6.0\bin>java DemoOfSuper

A = 1

B = 2

C = 3

Page 29: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

29

Sixth Program

//Simple program for use Second of super key word in Inheritance class first

{

int i;

}

class second extends first

{

int i;

second(int a,int b)

{

super.i=a;

i=b;

}

void show()

{

System.out.println("i in Super Class = "+super.i);

System.out.println("i in Sub Class = "+i);

}

}

class UseSuper

{

public static void main(String args[])

{

second t=new second(10,20);

t.show();

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac UseSuper.java

C:\Java\jdk1.6.0\bin>java UseSuper

i in Super Class = 10

i in Sub Class = 20

Page 30: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

30

Seventh Program

//Simple program for Method Ovrriding in Inheritance class xyz

{

int i,j;

xyz(int i,int j)

{

i=a;

j=b;

}

void show()

{

System.out.println("i & j = "+i+" "+j);

}

}

class pqr

{

int k;

pqr(int a,int b,int c)

{

super(a,b);

k=c;

}

void show()

{

super.show();

System.out.println("K = "+k);

}

}

class overrides

{

public static void main(String args[])

{

pqr t=new pqr(1,2,3);

t.show();

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac overrides.java

C:\Java\jdk1.6.0\bin>java overrides

i & j = 1 2

K = 3

Page 31: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

31

Seventh Program

//Simple program for Method Ovrriding using run time polymorphysm in

Inheritance class Figure

{

double dim1;

double dim2;

Figure(double a,double b)

{

dim1=a;

dim2=b;

}

double area()

{

System.out.println("Area for Figure is undefined");

return 0;

}

}

class Rectangle extends Figure

{

Rectangle(double a,double b)

{

super(a,b);

}

double area()

{

System.out.println("Inside Area for Rectangle is");

return (dim1*dim2);

}

}

class Triangle extends Figure

{

Triangle(double a,double b)

{

super(a,b);

}

double area()

{

System.out.println("Inside Area for Triangle is");

return (0.5*dim1*dim2);

}

}

class FindAreaDemo

{

public static void main(String args[])

{

Figure f=new Figure(10,10);

Rectangle r=new Rectangle(20,20);

Triangle t=new Triangle(30,30);

Page 32: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

32

Figure f1;

f1=r;

System.out.println("Area of Rectangle = "+f1.area());

f1=t;

System.out.println("Area of Triangle = "+f1.area());

f1=f;

System.out.println("Area of Figure = "+f1.area());

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac FindAreaDemo.java

C:\Java\jdk1.6.0\bin>java FindAreaDemo

Inside Area for Rectangle is

Area of Rectangle = 400.0

Inside Area for Triangle is

Area of Triangle = 450.0

Area for Figure is undefined

Area of Figure = 0.0

Page 33: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

33

Eithth Program

//Simple program of Abstract class abstract class A

{

abstract void cllme();

//Concrete methods are still allowed in abstract classes

void callmetoo();

{

System.out.println("This is a Concrete Mthod");

}

}

class B extends A

{

void cllme()

{

System.out.println("B's implementation of cllme");

}

}

class AbstractDemo

{

public static void main(String args [])

{

B.b=new B();

b.callme();

b.callmetoo();

}

}

Out Put:-

C:\Java\jdk1.6.0\bin>javac AbstractDemo.java

C:\Java\jdk1.6.0\bin>java AbstractDemo

B's implementation of cllme

This is a Concrete Mthod

Page 34: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

34

Nineth Program

//using abstract method and classes abstract class Figure

{ double dim1;

double dim2;

Figure(double a,double b)

{ dim1=a;

dim2=b; }

//area is now an abstract method

abstract double area();

}

class Rectangle extends Figure

{ Rectangle(double a,double b)

{ super(a,b);

}

//override area for rectangle

double area()

{ System.out.println("Inside Area for Rectangle");

return(dim1*dim2);

}

}

class Triangle extends Figure

{ Triangle(double a,double b)

{ super(a,b);

}

//override area for triangle

double area()

{ System.out.println("inside area of triangle");

return (dim1*dim2);

}

}

class AbstractArea

{ public static void main(String args [])

{ //Figure f= new Figure(10,10);//illegal now

Rectangle r=new Rectangle(9,5);

Triangle t= new Triangle(10,8);

Figure f;//This is ok, no object is created

f=r;

System.out.println("Area is " +f.area());

f=t;

System.out.println("Area is " +f.area());

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac AbstractArea.java

C:\Java\jdk1.6.0\bin>java AbstractArea

Inside Area for Rectangle

Area is 45.0

inside area of triangle

Area is 80.0

Page 35: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

35

Tenth Program

//Simple program of Final keyword in inheritance class T

{

final void meth()

{

System.out.println("This is a final method");

}

}

class P extends T

{

/*void meth()

{

System.out.println("Overriding of final method");

}*/

void meth123()

{

System.out.println("Use of concreat method");

}

}

class finaldemo

{

public static void main(String args[])

{

P x=new P();

x.meth();

x.meth123();

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac finaldemo.java

C:\Java\jdk1.6.0\bin>java finaldemo

This is a final method

Use of concreat method

Page 36: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

36

(2 Hours):- 5Practical

5. Lab Exercises:-

To Demonstrate Packages.

(2 Hours):- 6Practical

Lab Exercises VI Th:

- To Demonstrate Exception Handling.

1. class MultipleCatch

{

public static void main(String args[])

{

try

{

int a=args.length;

System.out.println("A = "+a);

int b=42/a;

int c[]={1};

c[42]=99;

}

catch(ArithmeticException e)

{

System.out.println("Divide by 0 ="+e);

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("Array index oob ="+e);

}

System.out.println("After try/catch block");

}

}

Out Put:- C:\Java\jdk1.6.0\bin>javac MultipleCatch.java

C:\Java\jdk1.6.0\bin>java MultipleCatch jjjjjj kkkkkk

Page 37: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

37

A = 2

Array index oob =java.lang.ArrayIndexOutOfBoundsException: 42

After try/catch block

(2 Hours):- 7Practical

Lab Exercises VII Th: -

To Demonstrate Multithreading.

(2 Hours):- 8Practical

Lab Exercises VIII Th: -

To Demonstrate Applets.

(2 Hours):- 9Practical

Lab Exercises IX Th: -

To Demonstrate JEBC.

(2 Hours):- 10Practical

Lab Exercises X Th: -

To Demonstrate I/O Packages.

4. Quiz on the subject:

Quiz should be conducted on tips in the laboratory, recent trends and subject knowledge of the

subject. The quiz questions should be formulated such that questions are normally are from the

scope outside of the books. However twisted questions and self formulated questions by the faculty

can be asked but correctness of it is necessarily to be thoroughly checked before the conduction of

the quiz.

5. Conduction of Viva-Voce Examinations:

Teacher should oral exams of the students with full preparation. Normally, the objective questions

with guess are to be avoided. To make it meaningful, the questions should be such that depth of the

students in the subject is tested Oral examinations are to be conducted in co-cordial environment

amongst the teachers taking the examination. Teachers taking such examinations should not have ill

thoughts about each other and courtesies should be offered to each other in case of difference of

opinion, which should be critically suppressed in front of the students.

6. Submission:

Document Standard:

A] Page Size A4 Size

B] Running text Justified text

C] Spacing 1 Line

D] Page Layout and Margins (Dimensions in cms)

Normal Page Horizontal

2.0

Page 38: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

38

2.5

2.0

2.5 2.0

2.0

0.7”

0.7”

2.0

2.0

Description Font B

o

l

d

n

e

s

s

I

t

a

l

i

c

s

Un

der

lin

e

Ca

pit

aliz

e

College Name Arial -

-

-

-

-

-

-

-

-

-

-

Ye

s

---

---

---

-

Document

Title

Tahoma -

-

-

-

-

-

-

-

-

-

-

---

---

---

---

---

---

-

Document

Subject

Century

Gothic

-

-

-

-

-

-

-

-

-

-

-

---

---

---

Ca

pit

al

Class Bookman

old Style

-

-

-

-

-

-

---

---

---

---

---

---

Page 39: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

39

-

-

-

-

-

-

Document No Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

---

---

---

---

---

---

-

Copy write

info

Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

---

---

---

---

---

---

-

Forward

heading

Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

Ye

s

Ca

pit

al

Forward

matter

Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

---

---

---

---

---

---

-

Lab man

Contents title

Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

Ye

s

Ca

pit

al

Index title Bookman

old Style

Y

e

s

-

-

-

-

-

-

Ye

s

Ca

pit

al

Index

contents

Bookman

old Style

-

-

-

-

-

-

-

-

-

-

-

---

---

---

---

---

---

-

Heading Tahoma Y

e

s

Y

e

s

Ye

s

---

---

---

-

Running

Matter

Comic Sans

MS

-

-

-

-

---

---

---

---

Page 40: java lab manual - jnec.org lab manual.pdf · To Demonstrate Overloading, Passing Objects Call by Value, Call by Reference. 4. To Demonstrate Inheritance, Method Overriding. 5. To

40

-

-

-

-

-

-

-

--- ---

-

7. Evaluation and marking system:

Basic honesty in the evaluation and marking system is absolutely essential and in the process

impartial nature of the evaluator is required in the examination system to become popular amongst

the students. It is a wrong approach or concept to award the students by way of easy marking to get

cheap popularity among the students to which they do not deserve. It is a primary responsibility of

the teacher that right students who are really putting up lot of hard work with right kind of

intelligence are correctly awarded.

The marking patterns should be justifiable to the students without any ambiguity and teacher should

see that students are faced with unjust circumstances.