Java Lab Manual CS306_CSE-III Semester
Java Lab Manual CS306_CSE-III Semester
LAB MANUAL
List of Practical's
1. Installation of JDK
2. Write a program in JAVA to display a greeting message “Welcome to Java World” on
console screen.
3. Write a program in JAVA to show Scope of Variables
4. Write a program to show Concept of “class and object” in JAVA
5. Write a program in JAVA to show the concept of Type Casting.
6. Write a program to demonstrate Exception Handling in JAVA
7. Write a Program in JAVA to demonstrate the concept of Inheritance
8. Write a program to demonstrate the concept of Polymorphism in JAVA
9. Write a program to show the uses of Access Specifiers (Public, Private, Protected) in
JAVA
10. Write a program in JAVA to show the concept of CONTRUCTOR
11. Write a program in JAVA to show the concept of abstract class
12. Write a program to show Interfacing between two classes in JAVA
13. Write a program to add a class to a Package in JAVA
14. Write a program in JAVA to show Life Cycle of a Thread
15. Write a program in JAVA to demonstrate multithreading usingRunnable Interface
16. Write a program in JAVA to demonstrate multithreading using Thread Class.
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
Java Code:
*************START**************
class Welcome
{
public static void main(String args[])
{
System.out.println(“Welcome to Java World”);
}
}
*************THE END**************
Program 2: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class scope {
public static void main(String arg[]) {
int x; //known to all code within main function
x=10;
if(x==10) {
//Start new scope
int y=20; //known only to this block
//x and y both known here
System.out.print("x and y : "+x+" and "+y);
x=y*2;
}
//y=100; //error because y is not here
//x is still known here
System.out.print("\nx is "+x);
}
}
*************THE END**************
Program 3: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class box {
double width;
double height;
double depth;
}
class boxdemo {
public static void main (String arg[]) {
box mybox1 = new box();
double vol;
//assign values to mybox1's instance variables
mybox1.width=10;
mybox1.height=20;
mybox1.depth=15;
Program 4: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class typecasting
{
public static void main(String[] args)
{
double answer;
int integer1=3;
int integer2=2;
answer=1.5+ integer1/integer2;
System.out.println("Answer when Typecasting is not done: " + answer);
answer=1.5+(double) integer1/integer2;
System.out.println("Answer After using typecasting technique " + answer);
} // end of main function
}
*************THE END**************
Program 5: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
publicclassSimpleExceptionHandling{
publicstaticvoid main(Stringargs[]){
try{
BufferedReaderbr=newBufferedReader(newFileReader("girish"));
String s =br.readLine();
System.out.println(s);
}
catch(FileNotFoundExceptionfne)
{
System.out.println("File not found.");
}
}
}
*************THE END**************
Program 6: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
void getVolume() {
System.out.println("Volume is : " + width * height * depth);
}
}
class MatchBox extends Box {
double weight;
MatchBox(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}
public static void main(String args[])
{
MatchBox mb1 = new MatchBox(10, 10, 10, 10);
mb1.getVolume();
System.out.println("width of MatchBox 1 is " + mb1.width);
System.out.println("height of MatchBox 1 is " + mb1.height);
System.out.println("depth of MatchBox 1 is " + mb1.depth);
System.out.println("weight of MatchBox 1 is " + mb1.weight);
}
}
***********THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
Program 7: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class Flower {
void which() {
System.out.println("A Beautiful flower.");
}
}
Program 8: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class Test{
private int number;
public int number1;
private void Testmethod(){
System.out.println("I want to print the private variable within this class : " +number);
}
void noSpecifierMethod(){
System.out.println("I can access private method from a nonspecifier method too : ");
Testmethod();
}
}
class mainClass {
public static void main(String[] args){
Test t = new Test();
t.AccessPrivateMethod();
System.out.println("I can print the class variable : "+t.number1);
}
}
************THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
Program 9: OUTPUT
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
class Box {
double l;
double b;
double h;
Box() {
l=2.3;
b=3.4;
h=4.5;
}
void show() {
System.out.print("the volume of the box is : " +(l*b*h));
}
}
class volume {
public static void main(String args[]) {
Box o=new Box();
o.show();
}
}
************THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
abstract class Vehicle
{
Vehicle()
{
System.out.println("Vehicle class Constructor is call");
}
abstract void gearSystem();
}
}
class Car extends Vehicle
{
void gearSystem()
{
System.out.println("Bike have its own gear System");
}
}
class MainClass
{
public static void main(String args[])
{
System.out.println("***** Abstract Class Example *****");
Vehicle v1 = new Bike();
Vehicle v2 = new Bike();
v1.gearSystem();
v2.gearSystem();
}
}
************THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
interface I1 {
abstract void test(int i);
}
interface I2 {
abstract void test(String s);
}
************THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
package MyPack;
class balance {
string name;
double bal;
balance (string n, double b) {
name=n;
bal=b;
}
void show(){
if(bal<0)
System.out.print("-->");
System.out.println(name+ ": $" +bal);
}
}
class accountbalance {
public static void main (String arg[]) {
balance current[] = new balance[3];
current[0] = new balance ("K.K.Fielding", 123.123);
current[1] = new balance ("Will Tell", 157.02);
current[2] = new balance ("Tom Jackson", -12.33);
A java thread is an execution context or a lightweight process. It is a single sequential flow of control
within a program. Programmer may use java thread mechanism to execute multiple tasks at the same
time
Thread Scheduling
When we say that threads are running concurrently, in practice it may not be so. On a
computer with single CPU, threads actually run one at a time giving an illusion of
concurrency.
The execution of multiple threads on a single CPU based on some algorithm is called thread
scheduling.
Thread scheduler maintains a pool of all the ready-to-run threads. Based on fixed priority
algorithm, it allocates free CPU to one of these threads.
Thread States:
1. New state – After the creations of Thread instance the thread is in this state but before the start()
method invocation. At this point, the thread is considered not alive.
2. Runnable (Ready-to-run) state – A thread start its life from Runnable state. A thread first enters
runnable state after the invoking of start() method but a thread can return to this state after either
running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting
for a turn on the processor
3. Running state – A thread is in running state that means the thread is currently executing. There are
several ways to enter in Runnable state but there is only one way to enter in Running state: the
scheduler select a thread from runnable pool.
4. Dead state – A thread can be considered dead when its run() method completes. If any thread
comes on this state that means it cannot ever run again.
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
5. Blocked - A thread can enter in this state because of waiting the resources that are hold by another
thread.
*************START**************
// Create a new thread.
classNewThreadimplementsRunnable{
Thread t;
NewThread(){
// Create a new, second thread
t =newThread(this,"Demo Thread");
System.out.println("Child thread: "+ t);
t.start();// Start the thread
}
publicclassThreadDemo{
publicstaticvoid main(Stringargs[]){
newNewThread();// create a new thread
try{
for(inti=5;i>0;i--){
System.out.println("Main Thread: "+i);
Thread.sleep(100);
}
}catch(InterruptedException e){
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
************THE END************
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
*************START**************
classRunnableDemoimplementsRunnable{
privateThread t;
privateStringthreadName;
RunnableDemo(String name){
threadName= name;
System.out.println("Creating "+threadName);
}
publicvoid run(){
System.out.println("Running "+threadName);
try{
for(inti=4;i>0;i--){
System.out.println("Thread: "+threadName+", "+i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Thread "+threadName+" interrupted.");
}
System.out.println("Thread "+threadName+" exiting.");
}
publicvoid start ()
{
System.out.println("Starting "+threadName);
if(t ==null)
{
t =newThread(this,threadName);
t.start();
}
}
}
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
publicclassTestThread{
publicstaticvoid main(Stringargs[]){
RunnableDemo R1 =newRunnableDemo("Thread-1");
R1.start();
RunnableDemo R2 =newRunnableDemo("Thread-2");
R2.start();
}
}
************THE END************
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
classThreadDemoextendsThread{
privateThread t;
privateStringthreadName;
ThreadDemo(String name){
threadName= name;
System.out.println("Creating "+threadName);
}
publicvoid run(){
System.out.println("Running "+threadName);
try{
for(inti=4;i>0;i--){
System.out.println("Thread: "+threadName+", "+i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Thread "+threadName+" interrupted.");
}
System.out.println("Thread "+threadName+" exiting.");
}
publicvoid start ()
{
System.out.println("Starting "+threadName);
if(t ==null)
{
t =newThread(this,threadName);
t.start();
}
}
}
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033
publicclassTestThread{
publicstaticvoid main(Stringargs[]){
ThreadDemo T1 =newThreadDemo("Thread-1");
T1.start();
ThreadDemo T2 =newThreadDemo("Thread-2");
T2.start();
}
}
************THE END************
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.