0% found this document useful (0 votes)
41 views

Java Lab Manual (Bcs306a) - Cse (1) - 1

Uploaded by

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

Java Lab Manual (Bcs306a) - Cse (1) - 1

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BACHELOR’S IN ENGINEERING
3rd SEMESTER

OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY


(BCS306A)

LABORATORY MANUAL

PREPARED BY

Heena Kouser
Lab Supervisor
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


VISION

To be a pioneer and internationally recognized institute in Computer Science and


Engineering, leading the way in cutting-edge research, technological innovations,
and producing highly skilled professionals to address the challenges and
opportunities of the digital age.

MISSION

 Providing comprehensive and cutting-edge education in Computer Science and


Engineering, equipping students with the knowledge and skills needed to
succeed in the rapidly evolving tech industry
 Fostering collaborative and interdisciplinary culture within the department,
encouraging students and faculty to explore innovative solutions through cross-
disciplinary projects and partnerships
 Engaging in community outreach programs, leveraging technology to address
local societal needs and contribute positively to the well-being and development
of the broader community
 Instilling a strong sense of ethics and social responsibility among students,
emphasizing the importance of using technology responsibly and sustainably to
create a positive impact on society and the environment.

OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY


Course Code BCS306A CIE Marks 50
Teaching Hours/Week (L : T : 2:0:2 SEE Marks 50

OOP with Java Lab (BCS306A) Page


1 of 22
P : S)
28 Hrs - Theory
Total Hours of Pedagogy + Total Marks 100
20 Hrs - Practical
Credits 03 Exam Hours 03
Examination Type (SEE) Theory

Course Objectives:
 To learn primitive constructs JAVA programming language.
 To understand Object Oriented Programming Features of JAVA.
 To gain knowledge on: packages, multithreaded programing and exceptions.

Course outcome (Course Skill Set):


At the end of the course, the student will be able to:
1. Demonstrate proficiency in writing simple programs involving branching and
looping structures.
2. Design a class involving data members and methods for the given scenario.
3. Apply the concepts of inheritance and interfaces in solving real world problems.
4. Use the concept of packages and exception handling in solving complex problem.
5. Apply concepts of multithreading, autoboxing and enumerations in program
development
Sl.
Programming Experiments
No.
01 Develop a Java program to add two matrices of suitable order N (The value of N
should be read from command line arguments).
02 Develop a stack class to hold a maximum of 10 integers with suitable methods.
Develop a Java main method to illustrate Stack operations.
03 A class called Employee, which models an employee with an ID, name and
salary, is designed as shown in the following class diagram. The method
raiseSalary (percent) increases the salary by the given percentage. Develop the
Employee class and suitable main method for demonstration.
04 A class called MyPoint, which models a 2D point with x and y coordinates, is
designed as follows:
 Two instance variables x (int) and y (int).
 A default (or "no-arg") constructor that construct a point at the default
location of (0, 0).
 A overloaded constructor that constructs a point with the given x and y
coordinates.
 A method setXY() to set both x and y.
 A method getXY() which returns the x and y in a 2-element int array.
 A toString() method that returns a string description of the instance in the
format "(x, y)".
 A method called distance(int x, int y) that returns the distance from this point
to another point at the given (x, y) coordinates
 An overloaded distance(MyPoint another) that returns the distance from this
point to the given MyPoint instance (called another)
 Another overloaded distance() method that returns the distance from this
point to the origin (0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called
TestMyPoint) to test all the methods defined in the class.
05 Develop a JAVA program to create a class named shape. Create three sub
classes namely: circle, triangle and square, each class has two member
functions named draw () and erase (). Demonstrate polymorphism concepts by
OOP with Java Lab (BCS306A) Page
2 of 22
developing suitable methods, defining member data and main program.
06 Develop a JAVA program to create an abstract class Shape with abstract
methods calculateArea() and
calculatePerimeter(). Create subclasses Circle and Triangle that extend the
Shape class and implement the respective methods to calculate the area and
perimeter of each shape.
07 Develop a JAVA program to create an interface Resizable with methods
resizeWidth(int width) and
resizeHeight(int height) that allow an object to be resized. Create a class
Rectangle that implements the Resizable interface and implements the resize
methods
08 Develop a JAVA program to create an outer class with a function display. Create
another class inside the outer class named inner with a function called display
and call the two functions in the main class.
09 Develop a JAVA program to raise a custom exception (user defined exception)
for DivisionByZero using try, catch, throw and finally.
10 Develop a JAVA program to create a package named mypack and import &
implement it in a suitable class.
11 Write a program to illustrate creation of threads using runnable class. (start
method start each of the
newly created thread. Inside the run method there is sleep() for suspend the
thread for 500 milliseconds).
12 Develop a program to create a class MyThread in this class a constructor, call
the base class constructor, using super and start the thread. The run method of
the class starts after this. It can be observed that both main thread and created
child thread are executed concurrently.
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End
Exam (SEE) is 50%. The minimum passing mark for the CIE is 40% of the maximum
marks (20 marks out of 50) and for the SEE minimum passing mark is 35% of the
maximum marks (18 out of 50 marks). A student shall be deemed to have satisfied
the academic requirements and earned the credits allotted to each subject/ course if
the student secures a minimum of 40% (40 marks out of 100) in the sum total of the
CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken
together.

CIE for the theory component of the IPCC (maximum marks 50):
 IPCC means practical portion integrated with the theory of the course.
 CIE marks for the theory component are 25 marks and that for the practical
component is 25 marks.
 25 marks for the theory component are split into 15 marks for two Internal
Assessment Tests (Two Tests, each of 15 Marks with 01-hour duration, are to
be conducted) and 10 marks for other assessment methods mentioned in
22OB4.2. The first test at the end of 40-50% coverage of the syllabus and the
second test after covering 85-90% of the syllabus.
 Scaled-down marks of the sum of two tests and other assessment methods will
be CIE marks for the theory component of IPCC (that is for 25 marks).
 The student has to secure 40% of 25 marks to qualify in the CIE of the theory
component of IPCC.

CIE for the practical component of the IPCC:


 15 marks for the conduction of the experiment and preparation of laboratory
record, and 10 marks for the test to be conducted after the completion of all
the laboratory sessions.
OOP with Java Lab (BCS306A) Page
3 of 22
 On completion of every experiment/program in the laboratory, the students
shall be evaluated including viva-voce and marks shall be awarded on the
same day.
 The CIE marks awarded in the case of the Practical component shall be based
on the continuous evaluation of the laboratory report. Each experiment report
can be evaluated for 10 marks. Marks of all experiments’ write-ups are added
and scaled down to 15 marks.
 The laboratory test (duration 02/03 hours) after completion of all the
experiments shall be conducted for 50 marks and scaled down to 10 marks.
 Scaled-down marks of write-up evaluations and tests added will be CIE marks
for the laboratory component of IPCC for 25 marks.
 The student has to secure 40% of 25 marks to qualify in the CIE of the practical
component of the IPCC.

SEE for IPCC


 Theory SEE will be conducted by University as per the scheduled timetable,
with common question papers for the course (duration 03 hours)
Suggested Learning Resources:
Textbook:
1. Java: The Complete Reference, Twelfth Edition, by Herbert Schildt, November 2021,
McGraw-Hill, ISBN:
9781260463422
Reference Books:
1. Programming with Java, 6th Edition, by E Balagurusamy, Mar-2019, McGraw Hill
Education, ISBN:
9789353162337.
2. Thinking in Java, Fourth Edition, by Bruce Eckel, Prentice Hall, 2006
(https://sd.blackball.lv/library/thinking_in_java_4th_edition.pdf)

Web links and Video Lectures (e-Resources):


 Java Tutorial: https://www.geeksforgeeks.org/java/
 Introduction To Programming In Java (by Evan Jones, Adam Marcus and Eugene
Wu):
 https://ocw.mit.edu/courses/6-092-introduction-to-programming-in-java-
january-iap-2010/
 Java Tutorial: https://www.w3schools.com/java/
 Java Tutorial: https://www.javatpoint.com/java-tutorial
Activity Based Learning (Suggested Activities)/ Practical Based learning
1. Installation of Java
(Refer: https://www.java.com/en/download/help/index_installing.html)
2. Demonstration of online IDEs like geeksforgeeks, jdoodle or any other Tools
3. Demonstration of class diagrams for the class abstraction, type visibility,
composition and inheritance

Assessment Method
● Programming Assignment / Course Project

OOP with Java Lab (BCS306A) Page


4 of 22
LABORATORY PROGRAMS

1. Develop a Java program to add two matrices of suitable order N (The value of
N should be read from command line arguments).
import java.util.Scanner;

public class FirstPgm {


public static void main (String args[]){

int n = Integer.parseInt(args[0]);
int a[][]= new int[n][n];
int b[][]= new int[n][n];
int c[][]= new int[n][n];

Scanner sc = new Scanner(System.in);


System.out.println("Enter the Matrix A elements");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
a[i][j]=sc.nextInt();
}

System.out.print("\n");
}
System.out.println("Enter the Matrix B elements");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
b[i][j]=sc.nextInt();

}
System.out.print("\n");
}
System.out.println("Display the Matrix A elements");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print(a[i][j]+" ");
}

System.out.print("\n");
}

System.out.println("Display the Matrix B elements");


for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print(b[i][j]+" ");
}
System.out.print("\n");
}

System.out.println("Display the elements for Addition of A and B matrices");


for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
c[i][j]=a[i][j]+b[i][j];

OOP with Java Lab (BCS306A) Page


5 of 22
System.out.print(c[i][j]+" ");
}
System.out.print("\n");
}
}
}
OUTPUT:
f:\MITK\2023-24\3 SEM JAVA(2022 SCHEME)\LAB PROGRAMS>javac FirstPgm.java

f:\MITK\2023-24\3 SEM JAVA(2022 SCHEME)\LAB PROGRAMS>java FirstPgm 2

Enter the Matrix A elements


1
2

3
4

Enter the Matrix B elements


1
2

3
4

Display the Matrix A elements


12
34
Display the Matrix B elements
12
34
Display the elements for Addition of A and B matrices
24
68
2. Develop a stack class to hold a maximum of 10 integers with suitable methods.
Develop a Java main method to illustrate Stack operations.
class Stack {

private static final int MAX_SIZE = 10;


private int top = -1;
private int[] a = new int[MAX_SIZE];

public void push(int value) {


if (top == MAX_SIZE - 1) {
System.out.println("Stack is full");
} else {
System.out.println("Element pushed to the stack " +value);
a[++top] = value;

OOP with Java Lab (BCS306A) Page


6 of 22
}

public int pop() {


if (top == -1) {
System.out.println("Stack is empty");
return -1; // or throw an exception
} else {
return a[top--];
}
}

public int peek() {


if (top == -1) {
System.out.println("Stack is empty");
return -1; // or throw an exception
} else {
return a[top];
}
}

public void display() {


if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.println("Stack elements are:");
for (int i = 0; i <= top; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}

public static void main(String[] args) {


Stack stack = new Stack();

stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.push(60);
stack.push(70);
stack.push(80);
stack.push(90);
stack.push(100);
stack.push(110);
stack.display();

System.out.println("Peek: " + stack.peek());


System.out.println("Element poped from the stack" +stack.pop());

OOP with Java Lab (BCS306A) Page


7 of 22
System.out.println("Element poped from the stack" +stack.pop());

stack.display();
}
}

OUTPUT:
Element pushed to the stack 10
Element pushed to the stack 20
Element pushed to the stack 30
Element pushed to the stack 40
Element pushed to the stack 50
Element pushed to the stack 60
Element pushed to the stack 70
Element pushed to the stack 80
Element pushed to the stack 90
Element pushed to the stack 100
Stack is full
Stack elements are:
10 20 30 40 50 60 70 80 90 100
Peek: 100
Stack elements are:
10 20 30 40 50 60 70 80 90 100

3. A class called Employee, which models an employee with an ID, name and salary, is
designed as shown in the following class diagram. The method raiseSalary
(percent) increases the salary by the given percentage. Develop the Employee class
and suitable main method for demonstration.

import java.util.Scanner;

class EmployeeInfo {

String name;
int id;
float salary;

float raiseSalary(float percent){


return salary+(salary*(percent/100));
}
}

class Employee{
public static void main(String[] args) {
EmployeeInfo e = new EmployeeInfo();
Scanner sc = new Scanner(System.in);

OOP with Java Lab (BCS306A) Page


8 of 22
e.name = "Mintu";
e.id = 143;
e.salary=90000;

System.out.println("Enter the percentage of the increment");

int n = sc.nextInt();

System.out.println("Salary after incrememt: "+e.raiseSalary(n));


}

OUTPUT:
Enter the percentage of the increment
39
Salary after incrememt: 125100.0

4. A class called MyPoint, which models a 2D point with x and y coordinates, is


designed as follows:
 Two instance variables x (int) and y (int).
 A default (or "no-arg") constructor that construct a point at the default location
of (0, 0).
 A overloaded constructor that constructs a point with the given x and y
coordinates.
 A method setXY() to set both x and y.
 A method getXY() which returns the x and y in a 2-element int array.
 A toString() method that returns a string description of the instance in the
format "(x, y)".
 A method called distance(int x, int y) that returns the distance from this point
to another point at the given (x, y) coordinates
 An overloaded distance(MyPoint another) that returns the distance from this
point to the given MyPoint instance (called another)
 Another overloaded distance() method that returns the distance from this
point to the origin (0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called
TestMyPoint) to test all the methods defined in the class.

import java.util.Scanner;

class MyPoint {
int x, y;

MyPoint() {
x = 0;
y = 0;
}

MyPoint(int x1, int y1) {


x = x1;
OOP with Java Lab (BCS306A) Page
9 of 22
y = y1;
}

void setXY() {
Scanner s = new Scanner(System.in);
System.out.print("Enter x coordinate: ");
x = s.nextInt();
System.out.print("Enter y coordinate: ");
y = s.nextInt();
}

String pointToString() {
return String.format("(%d, %d)", x, y);
}

double calcDistance() {
return Math.sqrt(x * x + y * y); // Distance from origin (0,0)
}

double calcDistance(int x2, int y2) {


return Math.sqrt(Math.pow(x2 - x, 2) + Math.pow(y2 - y, 2));
}

double calcDistance(MyPoint p2) {


return Math.sqrt(Math.pow(p2.x - x, 2) + Math.pow(p2.y - y, 2));
}
}

class Program4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint();
MyPoint p3 = new MyPoint();

System.out.println("Enter the coordinates for P1:");


p1.setXY();
System.out.println("Enter the coordinates for P2:");
p2.setXY();
System.out.println("Enter the coordinates for P3:");
p3.setXY();

System.out.printf("Distance between P1 %s and O(0,0) is: %.2f


%n",p1.pointToString(), p1.calcDistance());
System.out.printf("Distance between P1 %s and P2 %s is: %.2f%n",
p1.pointToString(), p2.pointToString(), p1.calcDistance(p2));
System.out.printf("Distance between P1 %s and P3 %s is: %.2f%n",
p1.pointToString(), p3.pointToString(), p1.calcDistance(p3));

scanner.close(); // Close the Scanner resource

OOP with Java Lab (BCS306A) Page


10 of 22
}
}

OUTPUT:
Enter the coordinates for P1:
Enter x coordinate: 1
Enter y coordinate: 2
Enter the coordinates for P2:
Enter x coordinate: 3
Enter y coordinate: 4
Enter the coordinates for P3:
Enter x coordinate: -1
Enter y coordinate: -1
Distance between P1 (1, 2) and O(0,0) is: 2.24
Distance between P1 (1, 2) and P2 (3, 4) is: 2.83
Distance between P1 (1, 2) and P3 (-1, -1) is: 3.61

5. Develop a Java program to create a class named shape. Create three sub
classes namely: circle, triangle and square, each class has two member
functions named draw () and erase (). Demonstrate polymorphism concepts by
developing suitable methods, defining member data and main program.
class ThreeShapes{

public static void main(String args[]){


Circle c = new Circle();
Traingle t = new Traingle();
Square s = new Square();

c.draw();
c.erase();
t.draw();
t.erase();
s.draw();
s.erase();
}
}
class Shape {
void draw(){
System.out.println("Drawing the shape");
}

void erase(){
System.out.println("Erasing the shape");

class Circle extends Shape{


OOP with Java Lab (BCS306A) Page
11 of 22
void draw(){
System.out.println("Drawing the Circle");
}

void erase(){
System.out.println("Erasing the Circle");

}
}

class Traingle extends Shape{


void draw(){
System.out.println("Drawing the Traingle");
}

void erase(){
System.out.println("Erasing the Traingle");

}
}

class Square extends Shape{


void draw(){
System.out.println("Drawing the Square");
}

void erase(){
System.out.println("Erasing the Square");

}
}
OUTPUT:

Drawing the Circle


Erasing the Circle
Drawing the Traingle
Erasing the Traingle
Drawing the Square
Erasing the Square

6. Develop a Java program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle
that extend the Shape class and implement the respective methods to calculate
the area and perimeter of each shape.

// Abstract class Shape


OOP with Java Lab (BCS306A) Page
12 of 22
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}

class Circle extends Shape {


private double radius=5;

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}

@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}

class Triangle extends Shape {


private double a=3;
private double b=4;
private double c=5;

@Override
public double calculateArea() {
double s = (a+b+c)/2;
return Math.sqrt( s*(s-a)*(s-b)*(s-c));
}

@Override
public double calculatePerimeter() {
return a+b+c;
}
}

public class Program6 {


public static void main(String[] args) {

Circle c = new Circle();


System.out.println("Circle Area: "+ c.calculateArea());
System.out.println("Circle Perimeter: "+c.calculatePerimeter());
System.out.println("");

Triangle t = new Triangle();


System.out.println("Triangle Area: "+t.calculateArea());
System.out.println("Triangle Perimeter: " +t.calculatePerimeter());
}

OOP with Java Lab (BCS306A) Page


13 of 22
}

OUTPUT:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793

Triangle Area: 6.0


Triangle Perimeter: 12.0
7. Develop a Java program to create an interface Resizable with methods
resizeWidth(int width) and resizeHeight(int height) that allow an object to be
resized. Create a class Rectangle that implements the Resizable interface and
implements the resize methods

interface Resizable {

void resizeWidth(int width);


void resizeHeight(int height);

class Rectangle implements Resizable{


int width = 10, height = 20;

public void resizeWidth(int newWidth){


width=newWidth;
}
public void resizeHeight(int newHeight){
height=newHeight;
}

class Program7{
public static void main(String[] args) {
Rectangle r = new Rectangle();
System.out.println("Original Dimensions:");
System.out.println("Width= "+r.width);
System.out.println("Height= "+r.height);

r.resizeWidth(30);
r.resizeHeight(40);
System.out.println("Resized Dimensions:");
System.out.println("Width= "+r.width);
System.out.println("Height= "+r.height);
}
}

OUTPUT:
OOP with Java Lab (BCS306A) Page
14 of 22
Original Dimensions:
Width= 10
Height= 20
Resized Dimensions:
Width= 30
Height= 40

8. Develop a Java program to create an outer class with a function display. Create
another class inside the outer class named inner with a function called display
and call the two functions in the main class.

class Outer {
void display(){
System.out.println("Hey User, I am an Outer Class ");
}

class Inner{
void display(){
System.out.println("Hey User, I am an Inner Class");
}
}
}

class Program8 {
public static void main(String[] args) {
Outer oo = new Outer();
Outer.Inner io = oo.new Inner();

oo.display();
io.display();

}
}

OUTPUT:
Hey User, I am an Outer Class
Hey User, I am an Inner Class

9. Develop a Java program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.

import java.util.Scanner;

class A extends Exception{


A(String msg){
super(msg);

OOP with Java Lab (BCS306A) Page


15 of 22
}
}

class Program9 {

public static void main(String[] args) {

try{

Scanner sc = new Scanner(System.in);


int a , b, c;
System.out.println("Enter Value for Nominator a");
a = sc.nextInt();
System.out.println("Enter Value for Nominator b");
b=sc.nextInt();

if(b==0){
throw new A("Devide by Zero Its an Exception!");
}
c= a/b;
System.out.println("The Result is: "+c);

}
catch(Exception e){
System.out.println(e);

}
finally{
System.out.println("Finally block will be Executed.");

}
}

OUTPUT 1:
Enter Value for Nominator a
4
Enter Value for Nominator b
2
The Result is: 2
Finally block will be Executed.

OUTPUT 2:

Enter Value for Nominator a


1
OOP with Java Lab (BCS306A) Page
16 of 22
Enter Value for Nominator b
0
A: Devide by Zero Its an Exception!
Finally block will be Executed.

9. Dvelop a JAVA program to create a package named mypack and import &
implement it in a suitable class.

//create the main program in a different file outside the mypack folder:

// Main program outside the mypack folder


import mypack.MyPackageClass;
//import mypack.*;

public class PackageDemo {


public static void main(String[] args) {
// Creating an instance of MyPackageClass from the mypack package
MyPackageClass myPackageObject = new MyPackageClass();

// Calling the displayMessage method from MyPackageClass


myPackageObject.displayMessage();

// Using the utility method addNumbers from MyPackageClass


int result = MyPackageClass.addNumbers(5, 4);
System.out.println("Result of adding numbers: " + result);
}
}

// Inside a folder named 'mypack'


package mypack;

public class MyPackageClass {


public void displayMessage() {
System.out.println("Hello I am MyPackageClass from mypack package!");
}

// New utility method


public static int addNumbers(int a, int b) {
return a + b;
}
}

OUTPUT:
Hello I am MyPackageClass from mypack package!
Result of adding numbers: 9

11. Write a program to illustrate creation of threads using runnable class. (start method
start each of the newly created thread. Inside the run method there is sleep() for suspend
the thread for 500 milliseconds).

OOP with Java Lab (BCS306A) Page


17 of 22
class RunnableThread implements Runnable {
RunnableThread(String tn) {
Thread.currentThread().setName(tn);
}

public void start() {


run();
}

public void run() {


try {
Thread.sleep(1000);
//Thread.sleep(60000);
System.out.println(Thread.currentThread().getName() + " Created
and is Running");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class ThreadRunnable {
public static void main(String args[]) {
RunnableThread rt1 = new RunnableThread("T1");
rt1.start();
RunnableThread rt2 = new RunnableThread("T2");
rt2.start();
RunnableThread rt3 = new RunnableThread("T3");
rt3.start();
}
}

OUTPUT:
T1 Created and is Running
T2 Created and is Running
T3 Created and is Running

12. Develop a program to create a class MyThread in this class a constructor,


call the base class constructor, using super and start the thread. The run
method of the class starts after this. It can be observed that both main
thread and created child thread are executed concurrently.

class myThread extends Thread {


myThread(String tname) {
super(tname);
}

public void run() {


System.out.println(this.getName() + " Started...");
OOP with Java Lab (BCS306A) Page
18 of 22
try {
System.out.println(this.getName() + " Continues...");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName() + " Completed.");
}
}

public class Program12 {


public static void main(String args[]) {
System.out.println("Main Thread Started.");
myThread mt = new myThread("Child-Thread");
mt.start();
System.out.println("Main Thread Continues...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main Thread Completed.");
}
}
OUTPUT:
Main Thread Started.
Main Thread Continues...
Child-Thread Started...
Child-Thread Continues...
Main Thread Completed.
Child-Thread Completed.

OOP with Java Lab (BCS306A) Page


19 of 22
VIVA-VOCE QUESTIONS:

1. List the features of Java Programming language.


2. How does Java enable high performance?
3. What do you understand by Java Runtime Environment (JRE) and Java Virtual
4. Machine (JVM)?
5. What is the difference between JDK, JRE, and JVM?
6. What is JIT compiler?
7. What is the final variable?
8. What happens when the final keyword id used with the methods and classes?
9. What are the various access specifiers in Java?
10.Why do we use static methods and variables in Java.
11.What is an object?
12.What is a Class?
13.What is a Constructor and Why do we use Constructors in Java?
14.List different types of Constructors in Java.
15.What is this keyword in java?
16.What are the main uses of this keyword?
17.What is method overloading?
18.What is method overriding?
19.What is Inheritance?
20.Why is Inheritance used in Java?
21.Why is Multiple Inheritance not supported in java?
22.How do you achieve Multiple Inheritance in java?
23.What is the significance of ‘super’ in Java.
24.What are the differences between this and super keyword?
25.What do you mean by Abstraction?
26.What happens when you use the keyword ‘abstract’ with methods and classes?
27.What is a Package? What are the advantages of Packages in Java?

OOP with Java Lab (BCS306A) Page


20 of 22
28.What is an exception? How do you handle the exceptions in Java.
29.What is an ArrayList in Java?
30.How is ArrayList differed from Arrays in Java? Explain.
31.What is a Thread?
32.What is the difference between a Thread and a Process?
33.How do you create threads in Java?
34.Explain the Thread Life Cycle.
35.List out the various states of Threads.
36.What is Mult-Threading?
37.What is Mult-Threading?
38.What is an Interface?
39.What is the need of Interface in Java?
40.When do you use the keywords ‘extends’ and ‘implements’ in Java?
41.What is a Stream in Java?
42.What are the various operations which can be performed on files?
43.How do you create file and open file in Java?
44.What is an Applet in Java?
45.Explain the Lifecycle of Java Applet?
46.What is Abstract Windowing Toolkit in Java?
47.What is an event? How do you handle the events in Java?
48.What is Swings in Java?
49.What is the Difference between AWT and Swing?
50.What Java Foundation Classes (JFC)?

OOP with Java Lab (BCS306A) Page


21 of 22

You might also like