Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views29 pages

Java Lab Manual CS306_CSE-III Semester

Gdkskgixyixg, if, ifxkf, gkgk ocg

Uploaded by

pt1735596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Java Lab Manual CS306_CSE-III Semester

Gdkskgixyixg, if, ifxkf, gkgk ocg

Uploaded by

pt1735596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Department of Computer Science & Engineering

University Institute of Technology


Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033

LAB MANUAL

Programming Lab –I (Java Technologies)


Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033

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

Program#1: Installation of JDK


Step1:DownloadJDK
1. GotoJavaSEdownloadsite.http://www.oracle.com/technetwork/java/javase/dow
nloads/index.html
2. Clickthe "Download"button under"JDK"of"JavaSE21".
3. Choose your operating platform, e.g., Windows x86 for 32-bit Windows OS or
Windows x64 for 64-bit Windows OS. You can check whether your Windows OS is
32-bit or 64-bit via "Control Panel" ⇒ System ⇒ Under the "System Type".
Step 2:Install JDK and JRE
Run the downloaded installer (e.g., "jdk-21uxx-windows-i586.exe"), which installs both
the JDK (Java Development Kit) and JRE (Java Runtime). By default, the JDK will be
installed indirectory "C:\Program Files\Java\jdk21.0.1_{xx}", where {xx} denotes the
latest upgradenumber; and JRE in "C:\Program Files\Java\jre21".For novices, accept
the defaults. Simply click "next"..."next"... to install JDK in "C:\Program Files\Java\
jdk21.0.1_{xx}" and JRE in "C:\Program Files\Java\jre21". Take note of you JDK
installed directory. I shall refer to the JDK installed directory as <JAVA_HOME>,
hereafter.(For Advanced Users Only) The default JDK/JRE directories work but I
recommend avoiding "ProgramFiles"directorybecauseofthatblank
characterinthedirectory name. Youmaychange theinstalleddirectories forJDKandJRE
duringinstallation.Ipersonally installedJDK and allmy programming tools in "d:\bin"
(instead of "C:\Program Files") for ease of maintenance.

Step 3:Include JDK's "bin" Directory inthePATH


Windows OS searches the current directory and the directories listed in the PATH
environment variable for executable programs. JDK's programs (such as Java compiler
javac.exe and Java runtime java.exe) reside in directory "<JAVA_HOME>\bin"
(where <JAVA_HOME>denotes the JDK installed directory, e.g., C:\Program
Files\Java\ jdk21.0.1_{xx}). You need to include the "<JAVA_HOME>\bin"
directory in the PATH.

Toedit thePATHenvironmentvariableinWindows 2000/XP/Vista/7/8:


1. Click "Start" button ⇒ "Control Panel" ⇒ "System" ⇒ (Vista/7/8 only) "Advanced
system settings".
2. Switchto "Advanced" tab⇒"Environment Variables..."
3. In"SystemVariables" box, scrolldowntoselect "PATH" ⇒"Edit..."
4. (CAUTION: Read this paragraph 3 times before doing this step! There is no
UNDO) In "Variable value" field, INSERT "c:\Program Files\Java\
jdk21.0.1_{xx}\bin" (VERIFY that this is your JDK's binary directory) IN FRONT of
all the existing directories, followed by a semi- colon (;) which separates the JDK's
binary directory from the rest of the existing directories.
5. Variablename: PATH
Variablevalue:c:\ProgramFiles\Java\ jdk21.0.1_{xx}\bin;[exitingentries]
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#2: Write a program in JAVA to display a greeting message “Welcome to


Java World” on console screen.

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

Program#3: Write a program in JAVA to show Scope of Variables

*************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

Program#4: Write a program to show Concept of class and object in JAVA

*************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;

vol=mybox1.width * mybox1.height * mybox1.depth;


System.out.print("Volume : "+vol);
}
}
*************THE END**************

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

Program#5: Write a program in JAVA to show the concept of Type Casting.

*************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

Program#6: Write a program to demonstrate Exception Handling in JAVA


*************START**************
importjava.io.BufferedReader;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;

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

Program#7: Write a Program in JAVA to demonstratethe concept of Inheritance

*************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

Program#8: Write a program to demonstrate the concept of Polymorphism in


JAVA

*************START**************
class Flower {
void which() {
System.out.println("A Beautiful flower.");
}
}

class Rose extends Flower {


void which() {
System.out.println("Rose");
}
}

class Lotus extends Flower {


void which() {
System.out.println("Lotus.");
}
}
class Test {
public static void main(String[] args) {
Flower ref1 = new Flower();
Flower ref2 = new Rose();
Flower ref3 = new Lotus();
ref1.which();
ref2.which();
ref3.which();
}
}
************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 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

Program#9: Write a program to show the uses of Access Specifiers (Public,


Private, Protected) in JAVA

*************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);
}

public void AccessPrivateMethod()


{
System.out.println("We can access private method declared in this class here : ");
Testmethod();
}

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

Program#10: Write a program in JAVA to show the concept of CONTRUCTOR

*************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

Program 10: 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

Program#11: Write a program in JAVA to show the concept of abstract class

*************START**************
abstract class Vehicle
{
Vehicle()
{
System.out.println("Vehicle class Constructor is call");
}
abstract void gearSystem();
}

class Bike extends Vehicle


{
void gearSystem()
{
System.out.println("Bike have its own gear System");
}

}
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

Program 11: 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

Program#12: Write a program to show Interfacing between two classes in JAVA

*************START**************
interface I1 {
abstract void test(int i);
}
interface I2 {
abstract void test(String s);
}

class MultiInterfaces implements I1, I2 {


public void test(int i) {
System.out.println("In MultiInterfaces.I1.test");
}
public void test(String s) {
System.out.println("In MultiInterfaces.I2.test");
}
public static void main(String[] a) {
MultiInterfaces t = new MultiInterfaces();
t.test(42);
t.test("Hello");
}
}

************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 12: 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

Program#13: Write a program to add a class to a Package in JAVA

*************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);

for (int i=0; i<3; i++)


current[i].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

Program 13: 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

Program#14: Write a program in JAVA to show Life Cycle of a Thread

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
}

// This is the entry point for the second thread.


publicvoid run(){
try{
for(inti=5;i>0;i--){
System.out.println("Child Thread: "+i);
Department of Computer Science & Engineering
University Institute of Technology
Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal
(State Technological University of Madhya Pradesh)
Bhopal (M.P.)-462033

// Let the thread sleep for a while.


Thread.sleep(50);
}
}catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child 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

Program#15: Write a program in JAVA to demonstrate multithreading using


Runnable Interface

Note: (Create Thread by Implementing Runnable Interface)

*************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************

Program 15: OUTPUT

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

Program#16: Write a program in JAVA to demonstrate multithreading using


Thread Class.

(Note: Create Thread by Extending Thread Class)

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************

Program 16: OUTPUT

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.

You might also like