Ooc Mannual
Ooc Mannual
Ooc Mannual
2023-2024
III Semester CBCS
2022 scheme
OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY
Subject code: BCS306A
COMPILED BY:
Mrs. Shilpa V
Ms. Jahnavi S
Mr. Sumanth Reddy
DAYANANDA SAGAR ACADEMY OF TECHNOLOGY AND MANAGEMENT
(Affiliated to Visvesvaraya Technological University, Belagavi & Approved By AICTE, New Delhi)
Opp. Art of Living, Udayapura, Kanakapura Road, Bangalore-560082
“To strive at creating the institution a center of highest caliber of learning, so as to create an
overall intellectual atmosphere with each deriving strength from the other to be the best of the
engineers, scientists with management & design skills.”
Mission
• To serve its region, state, the nation and globally by preparing students to make
meaningful contributions in an increasing global society challenges.
• To encourage, reflection on and evaluation of emerging needs and priorities with the state
of art infrastructure at institution.
• To support research and services establishing enhancements in technical, economic,
human and cultural development.
• To establish inter disciplinary center of excellence, supporting/ promoting students
implementation.
• To increase the number of Doctorate holders to promote research culture in the campus.
• To increase IPC, IPR, EDC, innovation cells with functional MOU’s supporting students
quality growth.
“Epitomize CSE graduate to crave a niche globally in the field of computer science to excel in
the world of information technology and automation by imparting knowledge to sustain skills for
the changing trends in the society and industry.”
Mission
Program Outcomes
a. Engineering Knowledge: Apply knowledge of mathematics, science, engineering
fundamentals and an engineering specialization to the solution of complex engineering
problems
b. Problem Analysis: Identify, formulate, research literature and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural
sciences and engineering sciences
c. Design/ Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet specified needs with appropriate
consideration for public health and safety, cultural, societal and environmental considerations.
d. Conduct investigations of complex problems: Using research-based knowledge and
research methods including design of experiments, analysis and interpretation of data and
synthesis of information to provide valid conclusions.
e. 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 under- standing of the limitations.
f. The Engineer and Society: Apply reasoning informed by contextual knowledge to assess
societal, health, safety, legal and cultural issues and the Consequent responsibilities relevant
to professional engineering practice
g. Environment and Sustainability: Understand the impact of professional Engineering
solutions in societal and environmental contexts and demonstrate knowledge of and need for
sustainable development
h. Ethics: Apply ethical principles and commit to professional ethics and Responsibilities and
norms of engineering practice
i. Individual and Team Work: Function effectively as an individual, and as a member or leader
in diverse teams and in multi-disciplinary settings.
j. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as being able to comprehend and write
effective reports and design documentation, make effective presentations and give and receive
clear instructions.
k. 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
l. Project Management and Finance: Demonstrate knowledge and understanding of engineering
and management principles and apply these to one’s own work, as a member and leader in a
team, to manage projects and in multi-disciplinary environment
Program Specific Outcomes
m. PSO1: Adapt, Contribute Innovate ideas in the field of Artificial Intelligence and Machine
Learning
n. PSO2: Enrich the abilities to qualify for Employment, Higher studies and Research in various
domains of Artificial Intelligence and Machine Learning such as Data Science, Computer
Vision, Natural Language Processing with ethical values
o. PSO3: Acquire practical proficiency with niche technologies and open-source platforms and
become Entrepreneur in the domain of Artificial Intelligence and Machine Learning
List of
Experiments
PART A – List of problems for which student should develop program and execute in the Laboratory
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 for demonstration.
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.
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 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
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 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.
Course Outcome (Course Skill Set)
At the end of the course the student will be able to:
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.
● 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.
Assessment Method
● Programming Assignment / Course Project
OOP with Java / BCS306A
OOP with Java Lab /21CSL35
PROGRAM :1
Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read
from command line arguments).
PROGRAM :
public class MatrixAddition {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide the order N as a command line argument.");
return;
}
int N = Integer.parseInt(args[0]);
if (N <= 0) {
System.out.println("N should be a positive integer.");
return;
}
System.out.println("Matrix B:");
displayMatrix(matrixB, N);
// Display a matrix
private static void displayMatrix(int[][] matrix, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
OUT PUT:
Provide Command Line Arguments:
In the "Console" view at the bottom, you should see your program running.
In the "Console," you can provide the value of N as a command line argument by going to the
"Run" menu (in the console) and selecting Run Configurations.
In the "Arguments" tab, you can add your desired value for N.
Click "Run": Once you've provided the command line argument, click the "Run" button.
The program will execute, and you'll see the output in the Console view within Eclipse.
Please replace <N> in the command line argument with the actual value of N you want to use when
running the program in Eclipse. The output will vary depending on the value of N and the random
sample values used to initialize the matrices in the code.
These steps should help you run the Java program successfully in Eclipse.
Matrix A:
123
456
789
Matrix B:
987
654
321
PROGRAM :2
Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a JAVA
main method to illustrate Stack operations.
PROGRAM :
class Stack {
private int[] stackArray;
private int top;
private int capacity;
stack.pop();
stack.pop();
stack.push(20);
stack.push(25);
stack.peek();
while (!stack.isEmpty()) {
stack.pop();
}
}
}
OUTPUT:
Pushed: 5
Pushed: 10
Pushed: 15
Popped: 15
Popped: 10
Pushed: 20
Pushed: 25
Peeked: 25
Popped: 25
Popped: 20
Popped: 5
PROGRAM :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
class Employee {
private int id;
private String name;
private double salary;
OUTPUT:
PROGRAM :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
PROGRAM :
class MyPoint {
private int x;
private int y;
public MyPoint() {
this.x = 0;
this.y = 0;
}
point1.setXY(1, 2);
System.out.println("Point 1 after setXY(1, 2): " + point1);
}
}
OUTPUT
Point 1: (0, 0)
Point 2: (3, 4)
PROGRAM :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
PROGRAM
class Shape
{
void draw()
{
System.out.println("Shape draw()");
}
void erase()
{
System.out.println("Shape erase()");
}
}
s = new Circle();
s.draw();
s.erase();
s = new Triangle();
s.draw();
s.erase();
s = new Square();
s.draw();
s.erase();
}
}
Output
Shape draw()
Shape erase()
Circle draw()
Circle erase()
Triangle draw()
Triangle erase()
Square draw
Square erase)
PROGRAM :6
PROGRAM
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
public Circle(double r) {
this.radius = r;
}
double calculateArea() {
return Math.PI * radius * radius;
}
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
this.side3 = c;
}
double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
public class AbstractShape {
public static void main(String[] args) {
double r = 4.0;
Circle circle = new Circle(r);
Output
Area of the Circle: 50.26548245743669
Perimeter of the Circle: 25.132741228718345
Area of the Triangle: 6.0
Perimeter of the Triangle: 12.0
PROGRAM :7
PROGRAM
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 {
}
}
public class InterfaceRectangle {
public static void main(String[] args) {
rectangle.printSize();
rectangle.resizeWidth(150);
rectangle.resizeHeight(200);
rectangle.printSize();
}
}
Output
Width: 10, Height: 15
Width: 150, Height: 200
PROGRAM :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.
PROGRAM
class Outer
{
String msgOuter = "This is Outer Class";
void display()
{
System.out.println(msgOuter);
}
void test(){
Inner inner = new Inner();
inner.display();
}
class Inner
{
String msgInner ="This is inner Class";
void display()
{
System.out.println(msgInner);
}
}
}
class InnerClassDeml
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.display();
outer.test();
}
}
Output
This is Outer Class
This is inner Class
PROGRAM :9
Develop a JAVA program to raise a custom exception (user defined exception) for Division By
Zero using try, catch, throw and finally.
PROGRAM
import java.util.*;
public class TryP
{
int c;
void div(int a,int b)
{
try
{
c=a/b;
System.out.println("Result="+c);
}
catch(ArithmeticException e)
{
System.out.println("Cannot divide by zero");
}
}
public static void main(String args[])
{
TryP obj=new TryP();
Scanner in=new Scanner(System.in);
System.out.println("Enter the values of a and b");
int no1=in.nextInt();
int no2=in.nextInt();
obj.div(no1,no2); }}
Output:
20
PROGRAM :10
Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
Aim: Develop a java application to implement currency converter (Dollar to INR, EURO to
INR, Yen to INR and vice versa), distance converter (meter to KM, miles to KM and vice
versa), time converter (hours to minutes, seconds and vice versa) using packages.
PROGRAM
CurrencyC.java
package cc;
import java.util.*;
public class CurrencyC
{
double inr,usd;
double euro,yen;
Scanner in=new Scanner(System.in);
public void dollartorupee()
{
System.out.println("Enter dollars to
convert into Rupees:");
usd=in.nextInt();
inr=usd*81.83;
System.out.println("Dollar ="+usd+" equal to INR="+inr);
System.out.println("\n");
}
public void rupeetodollar()
{
System.out.println("Enter Rupee to convert into Dollars:");
inr=in.nextInt();
usd=inr/81.83;
System.out.println("Rupee ="+inr+"equal to Dollars="+usd);
}
DistaceC.Java
package dc;
import java.util.*;
public class DistanceC
{
double km,m,miles;
Scanner in=new Scanner(System.in);
public void mtokm()
{
System.out.println("Enter the distance in meter");
m=in.nextDouble();
km=(m/1000);
System.out.println(m+"m" +" is equal to +km+"km");
System.out.println("\n");
}
public void kmtom()
{
System.out.println("Enter the distance in Kilometer");
km=in.nextDouble();
m=km*1000;
System.out.println(km+"km" +" is equal to "+m+"m");
System.out.println("\n");
}
public void milestokm()
{
System.out.println("Enter the distance in miles");
miles=in.nextDouble();
km=(miles*1.60934);
System.out.println(miles+"miles" +" is equal to "+km+"km");
System.out.println("\n"); }
TimeC.java
package tc;
import java.util.*;
public class TimeC
{
int hours,seconds,minutes;
Scanner in = new Scanner(System.in);
Main Class
import cc.*;
import dc.*;
import tc.*;
public class Main
{
public static void main(String args[])
{
CurrencyC obj=new CurrencyC();
DistanceC obj1=new DistanceC();
TimeC obj2=new TimeC();
obj.dollartorupee();
obj.rupeetodollar();
obj.eurotorupee();
obj.rupeetoeuro();
obj.yentoruppe();
obj.ruppetoyen();
obj1.mtokm();
obj1.kmtom();
obj1.milestokm();
obj1.kmtomiles();
obj2.hourstominutes();
obj2.minutestohours();
obj2.hourstoseconds();
obj2.secondstohours();
}
}
OUTPUT:
Enter dollars to convert into Rupees:1
Dollar =1.0 equal to INR=81.83
Enter Rupee to convert into Dollars: 80
Rupee =80.0equal to Dollars=0.977636563607479
Enter the distance in meter :1000
1000.0m is equal to 1.0km
Enter the distance in Kilometre :1
1.0km is equal to 1000.0m
Enter the no of Hours to convert into minutes: 1
Minutes: 60
Enter the no of Minutes to convert into Hours: 60
Hours: 1
PROGRAM :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).
PROGRAM
import java.util.Random;
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube extends Thread
{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}
class Rnumber extends Thread
{
public void run()
{
Random random = new Random();
for(int i =0; i<5; i++)
{
int randomInteger = random.nextInt(10);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
}
public class ThreadP
{
public static void main(String[] args)
{
Rnumber n = new Rnumber();
n.start();
}
}
Output:
PROGRAM 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.
PROGRAM
@Override
public void run() {
// Code to be executed by the child thread
for (int i = 1; i <= 5; i++) {
System.out.println("Child Thread: " + i);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
Main Thread: 1
Child Thread: 1
Main Thread: 2
Child Thread: 2
Main Thread: 3
Child Thread: 3
Main Thread: 4
Child Thread: 4
Main Thread: 5
Child Thread: 5
VIVA QUESTIONS