BCS6L3-Programming in Java Lab
BCS6L3-Programming in Java Lab
PROGRAMMING IN JAVA
LABORATORY
(SEMESTER-VI of B.Tech)
PREPARED BY
Mr.A V Allin Geo
1|Page
SCHOOL OF COMPUTING
LAB MANUAL
Regualtion 2015
(2015-2016)
2|Page
BCS6L3 PROGRAMMING IN JAVA LABORATORY L T P C
Total Contact Hours - 30 0 0 3 2
Prerequisite –Fundamentals of Computing and Programming, Object Oriented
Programming Using C++
Lab Manual Designed by – Dept. of Computer Science and Engineering.
OBJECTIVES
The Main objective of this Lab manual is Develop CUI and GUI application using Java Programming.
COURSE OUTCOMES (COs)
CO1 Identify classes, objects, members of a class and the relationships among them for a
Specific problem.
CO2 Develop programs using appropriate packages for Inter –thread Communication and
Synchronization.
CO3 Develop GUI applications to handle events.
CO4 Develop client server based applications.
CO5 Design, develop, test and debug Java programs using object-oriented principles in
Conjunction with development tools including integrated development environments.
CO6 Develop Applets Programs.
3|Page
PROGRAMMING IN JAVA LABORATORY- BCS6L3
LIST OF EXPERIMENTS
3 Methods Overloading
4 Inheritance
5 Interface
6 Multithreading
7 Package
4|Page
CONTENT
3 Methods Overloading 12
4 Inheritance 15
5 Interface 18
6 Multithreading 20
7 Package 23
5|Page
EX.No.1 CLASSES AND OBJECTS
AIM:
Write a Program in Java to implement the Classes and Objects.
ALGORITHM:
SOURCE CODE:
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
6|Page
}
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
7|Page
Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
OUTPUT:
101 Sonoo
102 Amit
111 Karan
222 Aryan
55
45
9|Page
RESULT:
Thus the Java program to implement classes and objects was written, executed and the output
was verified successfully.
10 | P a g e
EX.No.2 CONSTRUCTORS & DESTRUCTORS
AIM:
To write a program in java with Constructors and destructors
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the input variables
STEP 3: Create the Constructors
STEP 4: Create various methods for Subclass
STEP 5: In derived class extend the previous class
STEP 6: In main class specify the values and create the object
STEP 7: Stop
SOURCE CODE:
// import java.io.*;
class Geek
int num;
String name;
Geek()
System.out.println("Constructor
called");
11 | P a g e
}
class GFG
System.out.println(geek1.name);
System.out.println(geek1.num);
}
OUTPUT:
Constructor called
null
Param
RESULT:
Thus the program in java with Constructors and destructors is executed successfully and the
output is verified
12 | P a g e
EX.No.3 METHOD OVERLOADING
AIM:
STEP 7: In main function create the object and call the methods
SOURCE CODE:
OUTPUT:
30
60
31.0
#Adding Numbers
return x + y;
return x + y;
14 | P a g e
OUTPUT:
int: 13
double: 10.559999999999999
RESULT:
Thus the Java program to implement method overloading and the output was verified
successfully.
15 | P a g e
EX. No.4 INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
16 | P a g e
public String toString()
{
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
17 | P a g e
Output
No of gears are 3
Speed of bicycle is 100
Seat height is 25
Result:
Thus the program in java to implement Inheritance is executed successfully and the output is verified.
18 | P a g e
EX.No.5 INTERFACE
AIM:
ALGORITHM:
STEP 1: Start the Program
STEP 6: STOP
SOURCE CODE
import java.io.*;
// A simple interface
interface In1
{
// public, static and final
final int a = 10;
// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
RESULT:
Thus the program in java to implement Interface is executed successfully and the output is verified
20 | P a g e
EX. No. 6 MULTI THREADING
AIM:
To write a Program in Java to implement Multi Thread.
ALGORITHM:
STEP 8: Stop
SOURCE CODE:
try {
System.out.println(
+ " is running");
catch (Exception e) {
// Throwing an exception
21 | P a g e
System.out.println("Exception is caught");
// Main Class
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
OUTPUT:
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
22 | P a g e
// Java code for thread creation by implementing
// the Runnable Interface
class MultithreadingDemo implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Thread object
= new Thread (new Multithreading Demo());
object.start ();
}
}}
OUTPUT:
Thread 13 is running
Thread 11 is running
Thread 12 is running
Thread 15 is running
Thread 14 is running
Thread 18 is running
Thread 17 is running
Thread 16 is running
RESULT:
Thus the Java program using Thread class and runnable interface for implementing Thread was
written, executed and the output was verified successfully.
23 | P a g e
EX. No. 7 PACKAGES
AIM:
To write a program in Java to implement Packages
ALGORITHM:
STEP 1. START
STEP 2. Import package
STEP 3. Create Class A
STEP 4. Create Class B
STEP 5. Get output.
SOURCE CODE:
package pack;
public class A {
public void msg() {
System.out.println("Hello");
}
}
//save by B.java
package mypack;
class B {
public static void main(String args[]) {
pack.A obj = new pack.A(); //using fully qualified name
obj.msg();
}
}
Output
Hello
Result:
Thus the program in java to implement Packages is executed successfully and the output is verified
24 | P a g e
EX. No. 8 CREATING JAVA APPLETS
AIM:
To write a program in java to create Java Applets
ALGORITHM:
STEP 5: STOP
SOURCE CODE:
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="HelloWorld" width=200 height=60>
</applet>
*/
// HelloWorld class extends Applet
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
Output
appletviewer
HelloWorld
25 | P a g e
RESULT:
Thus the program in Java to create Java Applets is executed successfully and the output is verified
26 | P a g e