Java Lab Manual
Java Lab Manual
net/publication/328969152
CITATIONS READS
0 8,675
1 author:
Asif Munir
BQE Software Inc
6 PUBLICATIONS 15 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Asif Munir on 15 November 2018.
1
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
INDEX
Sr. No. Experiment Number Page Number
1 Experiment No 1 3 -4
2 Experiment No 2 5-6
3 Experiment No 3 7-9
4 Experiment No 4 10-13
5 Experiment No 5 14-17
6 Experiment No 6 18-21
7 Experiment No 7 22-24
8 Experiment No 8 25-30
9 Experiment No 9 31-33
10 Experiment No 10 34-37
2
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 1
An Armstrong of three digits is an integer such that the sum of the cubes of its
digits is equal to the number itself. The various examples of Armstrong numbers
are 153,370,371,407.
Source Code
/* Java Program to check whether a three digit number is Armstrong or not*/
import java.util.Scanner;
class armstrong
{
public static void main(String args[])
{
int number=0,sum=0,rem=0,cube=0,temp=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter a number: ");
number=scan.nextInt();
System.out.println("The number entered is:"+" " +number );
temp=number;
while(number!=0)
{
rem=number%10;
cube=(int)Math.pow(rem,3);
sum=sum+cube;
number=number/10;
}
if(sum==temp)
System.out.println(temp+" "+"is an armstrong number");
else
System.out.println(temp+" "+"is not armstrong number");
}
}
3
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
OUTPUT:
CONCLUSIONS:
So after doing the above study we have understood how to create a class and a
Viva questions:
1. What does static keyword mean?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
4
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 2
Objective:- Write a program to show the concept of Constructors.
Java supports a special type of method called constructor that enables an object
to initialize itself when it is created. Constructors have the same name as the
class itself. Constructors do not specify a return type, not even void. This is
Source Code
/* Java Program computing the area of rectangle illustrating constructors */
class rectangle
{
int length, width;
rectangle(int x, int y)
{
length=x;
width=y;
}
void getArea()
{
int area=length*width;
System.out.println("The area of rectangle is: "+ area + " units");
}
}
class constructorExample
{
public static void main(String args[])
{
rectangle r1=new rectangle(10,20);
r1.getArea();
}
}
OUTPUT:
5
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
CONCLUSION:
constructors, how to create constructor and how parameters are passed to the
constructor.
Viva questions:
1. What is a constructor?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
6
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 3
In java it is possible to create methods that have same name but different
Method overloading is used when objects are required to perform similar tasks
but using different input parameters. When we call a method in an object, Java
matches up the method name first, and then the number and type of parameters
polymorphism.
Source Code
/* Java Program computing the area of room illustrating the concept of method overloading*/
class Room
{
int length;
int breadth;
Room (int x, int y)
{
length=x;
breadth=y;
}
Room(int x)
{
length=x;
breadth=x;
}
void area()
{
int x=length*breadth;
7
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
class overloadingExample
{
public static void main(String args[])
{
Room r1=new Room(10,20);
r1.area();
OUTPUT:
CONCLUSION:
So after doing the above study we have understood the concept of method
overloading
Viva questions:
1. What is method overloading?
2. What is polymorphism?
8
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
9
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 4
Reusability is another aspect of OOP paradigm. Java supports this concept. Java
classes can be reused in several ways. This is basically done by creating new
new class from an old one is called inheritance. The old class is known as base
class or super class or parent class and the new one is called the subclass or
derived class or child class. The “extends” keyword is used for inheritance.
The inheritance allows the subclasses to inherit all the variables and methods of
Java does not directly implement multiple inheritances; however this concept is
10
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Source Code
/* Java Program computing the area of room illustrating the concept of single Inheritance*/
class Room
{
int length;
int breadth;
Room(int x, int y)
{
length=x;
breadth=y;
}
void area()
{
int z=length*breadth;
System.out.println("The area is: "+z);
}
}
11
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
super(a,b); //passes the value to superclass
height=c;
}
void volume()
{
int v=length*breadth*height;
System.out.println("The volume is : "+v);
}
}
class inheritenceExample
{
public static void main(String args[])
{
BedRoom br=new BedRoom(10,20,30);
br.area();
br.volume();
}
}
OUTPUT:
CONCLUSION:
So after doing the above study we have understood the concept of Inheritance.
Viva questions:
1. What is an Inheritance?
12
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
13
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 5
Strings represent a sequence of characters. In Java strings are class objects and
implemented using two classes namely String and StringBuffer. A Java string is
an instantiated object of the String class. Java strings are more reliable and
Source Code
/* Java Program to show various string operations*/
class stringExample
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Object Oriented Language");
System.out.println("Original String: "+str);
14
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
//Modifying Charecters
str.setCharAt(6,'-');
System.out.println("updated string is: "+str);
OUTPUT:
Source Code
/* Java Program for Alphabetical Ordering of Strings*/
class stringOrdering
15
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
static String name[]={"Madras","Delhi","Bombay","Hyderabad", "Ahmedabad"};
int size=name.length;
String temp=null;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if (name[j].compareTo(name[i])<0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
OUTPUT:
16
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
CONCLUSION:
So after doing the above study we have understood the concept and various
operations on strings.
Viva questions:
1. What is a StringBuffer?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
17
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 6
methods and variables but with major difference. The difference is that interface
defines only abstract methods and final fields. This means that interface do not
specify any code to implement these methods and data fields contain only
interface <interfaceName>
{
Variables declaration;
Methods declaration;
}
18
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Source Code
class student
{
int rollNo;
void getNumber(int n)
{
rollNo=n;
}
void putNumber()
{
System.out.println("Roll No is: "+ rollNo);
}
}
interface sports
{
int sportsWt=5;
void putWt();
}
19
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
total=part1+part2+sportsWt;
putNumber();
putMarks();
putWt();
System.out.println("Total score = "+ total);
}
}
class interfaceExample2
{
public static void main(String args[])
{
results r1=new results();
r1.getNumber(10);
r1.getMarks(50,60);
r1.display();
}
}
OUTPUT:
CONCLUSION:
20
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Viva questions:
1. What is a multiple inheritance?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
21
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 7
Packages are the Java’s way of grouping a variety of classes and/or interfaces
packages from accessing classes that are meant for internal use only.
package <packageName>;
Define the class that is to be put in the package and declare it public.
Create a subdirectory under the directory where the main source files are
22
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Source Code
package package1;
package package2;
import package1.classA;
import package2.classB;
class packageExample
{
public static void main(String args[])
{
classA a=new classA();
a.displayA();
classB b=new classB();
b.displayB();
OUTPUT:
23
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
CONCLUSION:
So after doing the above study we have understood the concept of packages,
Viva questions:
1. What is the use of package in Java?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
24
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 8
implemented at the same time in parallel. Since the threads in java are
subprograms of main application program and share same memory space, they
Creating threads in java is simple. The run() method is the heart and soul of any
thread. It makes up the entire body of a thread and is the only method in which
thread’s behavior can be implemented. The run method would appear as:
We can make our class runnable by extending the class java.lang.Thread. The
follows
25
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
ThreadName.setPriority(intNumber);
The intNumber is an integer value to which the thread’s priority is set. The
MIN_PRIORITY=1
NORM_PRIORITY=5
MAX_PRIORITY=10
Source Code
26
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
System.out.println("Thread B: j= "+j);
}
System.out.println("Exit from B");
}
}
class threadExample1
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
OUTPUT:
27
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Source Code
System.out.println("Thread A: i= "+i);
yield();
}
System.out.println("Exit from A");
}
}
}
System.out.println("Exit from B");
}
}
}
System.out.println("Exit from C");
}
}
class threadExample2
{
public static void main(String args[])
28
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadA.setPriority(Thread.MIN_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.start();
threadB.start();
threadC.start();
}
}
OUTPUT:
CONCLUSION:
So after doing the above study we have understood the concept of creating
29
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Viva questions:
1. What is lightweight process?
Reference:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
30
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 9
exception object and throws it (Informs us that error has occurred). If the
exception is not caught and handled properly, the interpreter will display an error
message.
In Java we can use more than one catch statement for handling error. Java
supports another statement known as finally statement that can be used to handle
an exception that is not caught by any of the previous catch statement. The
syntax is as:
try
{
……………
}
catch(….)
{
………
}
catch(….)
{
……….
}
…
…
finally
31
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
{
…………
}
Source Code
class exceptionExample
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[1]/0;
}
catch(ArithmeticException e)
{
System.out.println("Division by zero Error");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("Wrong data type");
}
finally
{
System.out.println("I am always here");
}
}
}
32
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
OUTPUT:
CONCLUSION:
So after doing the above study we have understood the concept of exception
of finally statement.
Viva questions:
1. What is the use of finally statement?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
33
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
Experiment No. 10
Applets are small java programs that are primarily used in internet computing.
They can be transported over internet from one computer to another and run
using applet Viewer. Applets are not full featured application programs. They
are usually written to accomplish small task or a component of task. Since they
are usually designed for use on internet, they impose certain limitations and
They cannot run independently. They are run from inside a web page.
3. Designing a web page using HTML tags. Webpage must be saved with
34
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
appletviewer <filename.html>
Applet code uses the services of two classes namely Applet and Graphics from
java class library. Java automatically calls a series of Applet class methods for
starting, running and stopping the applet code. The applet class therefore
maintains the life cycle of an Applet. The paint() method actually displays the
Source Code
35
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
/* Applet code for drawing line, rectangle, circle and setting color */
import java.awt.*;
import java.applet.*;
public class appletExample2 extends Applet
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
g.fillRect(60,10,30,80);
g.drawOval(70,70,200,200);
g.setColor(Color.green);
g.fillOval(90,80,150,150);
}
}
HTML CODE:
<HTML>
<HEAD>
<H1>WELCOME TO APPLETS</H1>
</HEAD>
<BODY>
<APPLET
CODE=appletExample2.class
width=400
height=300>
</APPLET>
</BODY>
</HTML>
OUTPUT:
CONCLUSION:
36
Lab File: Java Programming lab (6CS7A), Dept. of CSE 2017-2018
So after doing the above study we have understood the concept of creating
applets
Viva questions:
1. What is an Applet?
References:
1. Java : The complete Reference by Herbertz Schildt, Oracle Press
37