BCS306A OOPS With Java Lab Manual
BCS306A OOPS With Java Lab Manual
COURSE MATERIAL
Prepared by :
Vision, Mission and PEO’s of the Artificial Intelligence & Data Science Programme
To empower women engineers with cutting-edge knowledge and skills in Artificial Intelligence
and Data Science, fostering innovation, leadership, and ethical practices to drive transformative
changes in society and industry.
Mission 1: Provide a rigorous and comprehensive education in Artificial Intelligence and Data
Science.
Mission 2: Foster a culture of research and innovation among students and faculty
Mission 3: Equip students with the skills and confidence to take on leadership roles in their careers
Mission 4: Promote ethical considerations and the responsible use of AI and Data Science to benefit
society.
PEO 2: To engage in innovative practices and research, contributing to the development of new
technologies and solutions by advancing the frontiers of knowledge and technology
PEO 3: To exhibit leadership qualities and the ability to work effectively in multidisciplinary
teams, demonstrating strong communication, project management, and teamwork skills to drive
successful project outcomes.
Program Outcomes
PO2: Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
PO5: Modern tool usage: Create, select and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
PO8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO12: Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
PSO1: Enable students to design system and system architecture, inculcating software,
computing and analytical ability.
Course objectives:
● To learn primitive constructs JAVA programming language.
● To understand Object Oriented Programming Features of JAVA.
● To gain knowledge on: packages, multithreaded programming 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, auto boxing and enumerations in program development.
Programming Experiments(Suggested and are not limited to)
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read
from command line arguments).
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a JAVA
main method to illustrate Stack operations.
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 ford Demonstration.
4. A class called MyPoint,whichmodelsa2D 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 a2-element int array.
● A toString()methodthatreturnsastringdescriptionoftheinstanceintheformat"(x,y)".
● A method called distance(int x,inty)that returns the distance from
this point to another point at the given(x,y)coordinates
● An overloaded distance(MyPointanother)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
them methods defined in the class.
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.
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.
7. Develop a JAVA program to create an interface Resizable with methods resize Width(int width) and
resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that implements the
Resizableinterface and implements the resize methods
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.
9. Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZerousingtry,
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
[Link]()forsuspendthethreadfor500milliseconds).
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 weight age 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.
● 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.
● 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 15marks.
● The laboratory test (duration 02/03 hours) after completion of all the experiments shall be conducted for 50
marks and scaled down to 10marks.
● Scaled-down marks of write- up evaluations and tests added will be CIE marks for the
laboratory component of IPCC for25 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 time table, with common question papers for the
course (duration03 hours)
The theory portion of the IPCC shall be for both CIE and SEE, whereas the practical portion will have a CIE
component only. Questions mentioned in the SEE paper may include questions from the practical component.
Textbook
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. Thinking in Java, Fourth Edition, by Bruce Eckel, Prentice Hall, 2006
([Link]
Eclipse:
Installing Eclipse
1. Unzip this file that you just downloaded. On most Windows machines, you can
Right-click the file.
Move to the WinZip command.
Click Extract to here
It creates a folder named eclipse. You can leave this folder here or move it elsewhere on your disk
drive.
5. Click OK
6. If you want easy access to the workspace, create a shortcut to it (see the instructions above),
putting the shortcut on your desktop.
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).
package pgm1;
import [Link];
[Link]();
}
}
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop
a JAVA main method to illustrate Stack operations.
package pgm2;
//[Link]
public class Stack
{
final int MAX = 10;
int s[] = new int[MAX];
int top = -1;
import [Link];
//[Link]
public class StackDemo
{
public static void main(String args[])
{
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.
//[Link]
package pgm3;
public class Employee {
private int id;
private String name;
private int salary;
//[Link]
import [Link];
}
}
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
package pgm4;
// [Link]
public class MyPoint {
int x;
int y;
public MyPoint()
{
this.x = 0;
this.y = 0;
}
return dist;
}
import [Link];
while (true)
{
[Link]("1. Distance to origin");
[Link]("2. Distance from point (x,y)");
[Link]("3. Distance from Point object");
[Link]("0. Exit");
[Link]("Enter your choice : ");
choice = [Link]();
switch ( choice )
{
case 1 :
distance = [Link]();
break;
case 2 :
[Link]("Enter coordinates of other point");
x = [Link]();
y = [Link]();
distance = [Link](x,y);
break;
case 3:
[Link]("Enter coordinates of other point");
x = [Link]();
y = [Link]();
MyPoint point1 = new MyPoint(x,y);
distance = [Link](point1);
break;
case 0:
return;
}
[Link]("Distance : " + distance);
}
}
}
import [Link];
while (true)
{
[Link]("1. Distance to origin");
[Link]("2. Distance from point (x,y)");
[Link]("3. Distance from Point object");
[Link]("0. Exit");
[Link]("Enter your choice : ");
choice = [Link]();
switch ( choice )
{
case 1 :
distance = [Link]();
break;
case 2 :
[Link]("Enter coordinates of other point");
x = [Link]();
y = [Link]();
distance = [Link](x,y);
break;
case 3:
[Link]("Enter coordinates of other point");
x = [Link]();
y = [Link]();
MyPoint point1 = new MyPoint(x,y);
distance = [Link](point1);
break;
case 0:
return;
}
[Link]("Distance : " + distance);
}
}
}
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.
package pgm5;
//
public class Circle implements Shape {
public void draw()
{
[Link]("[Link]() ...");
}
public void erase()
{
[Link]("[Link]() ...");
}
}
public interface Shape {
public void draw();
public void erase();
}
{
[Link]("[Link]() ...");
}
}
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.
package pgm6;
public class Circle extends Shape {
@Override
public float calculatePerimeter() {
return (float) (2 * [Link] * radius);
}
@Override
public float calculateArea() {
return (float) ([Link] * radius * radius );
}
}
public abstract class Shape {
public abstract float calculatePerimeter();
public abstract float calculateArea();
import [Link];
float perimeter;
float area;
[Link]("1. Circle");
[Link]("2. Triangle");
[Link]("Enter your option : ");
option = [Link]();
Shape shape;
switch ( option )
{
case 1 :
[Link]("Enter radius : ");
radius = [Link]();
shape = new Circle(radius);
perimeter = [Link]();
area = [Link]();
[Link]("Perimeter : " + perimeter);
[Link]("Area : " + area);
break;
case 2 :
[Link]("Enter 3 sides of triang;e : ");
a = [Link]();
b = [Link]();
c = [Link]();
shape = new Triangle(a,b,c);
perimeter = [Link]();
area = [Link]();
[Link]("Perimeter : " + perimeter);
[Link]("Area : " + area);
break;
}
}
private int a;
private int b;
private int c;
@Override
public float calculatePerimeter() {
return (float) (a + b + c);
}
@Override
public float calculateArea() {
float s = (float)((a+b+c) / 2.0);
float area = (float)[Link](s * (s-a) * (s-b) * (s-c));
return area;
}
}
package pgm7;
public class Rectangle implements Resizable {
public Rectangle()
{
@Override
public void resizeHeight(int height) {
[Link] = height;
}
@Override
public void resizeWidth(int width) {
[Link] = width;
}
import [Link];
int height = 0;
int width = 0;
[Link](resizable);
}
}
8. Develop a JAVA program to create an outer class with a function display. Create
another class inside theouter class named inner with a function called display and call
the two functions in the main class.
package pgm8;
public class Outer {
public void display()
{
[Link]("Inside [Link]()");
}
class Inner
{
public void display()
{
[Link]("Inside [Link]()");
}
}
}
[Link] = [Link]();
[Link]();
}
}
9. Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
package pgm9;
import [Link];
try
{
int quotient = 22 / n;
[Link]("quotient : " + quotient);
}
catch(ArithmeticException ae)
{
try
{
throw new MyException();
}
catch(MyException me)
{
}
}
}
}
10. Develop a JAVA program to create a package named mypack and import &
implement it in asuitable class.
package [Link];
import [Link];
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).
package pgm11;
[Link]("JIT1");
[Link]("JIT2");
[Link]("JIT3");
[Link]();
[Link]();
[Link]();
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.
package pgm12;
@Override
public void run()
{
for ( inti = 0; i< 50; i++ )
[Link]("run() : " + [Link]().getName());
try {
[Link](5000);
} catch (InterruptedException e) {
[Link](e);
}
}
}
}