0% found this document useful (0 votes)
8 views34 pages

BCS306A OOPS With Java Lab Manual

The document outlines the course material for the Object Oriented Programming with JAVA Laboratory (BCS306A) for the III Semester of the 2025-2026 academic year, prepared by Ms. Likhitha. It includes the department's vision, mission, program educational objectives, outcomes, and specific outcomes, along with a detailed lesson plan and assessment criteria. The course aims to equip students with programming skills in JAVA, focusing on object-oriented concepts, multithreading, and exception handling.
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)
8 views34 pages

BCS306A OOPS With Java Lab Manual

The document outlines the course material for the Object Oriented Programming with JAVA Laboratory (BCS306A) for the III Semester of the 2025-2026 academic year, prepared by Ms. Likhitha. It includes the department's vision, mission, program educational objectives, outcomes, and specific outcomes, along with a detailed lesson plan and assessment criteria. The course aims to equip students with programming skills in JAVA, focusing on object-oriented concepts, multithreading, and exception handling.
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

DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

COURSE MATERIAL

Object Oriented Programming with JAVA Laboratory


[BCS306A]
III Semester, Odd Semester 2025-2026

Prepared by :

Ms. Likhitha V B.E, [Link]


Assistant Professor
Dept of AI&DS
GSSSIETW, Mysuru
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

Vision, Mission and PEO’s of the Artificial Intelligence & Data Science Programme

Vision of the Department

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 of the Department

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.

Program Educational Objectives

PEO 1: To demonstrate technical proficiency and a deep understanding of fundamental and


advanced concepts in Artificial Intelligence and Data Science in solving complex engineering
problems.

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.

PEO 4: To uphold ethical standards and demonstrate a commitment to social responsibility,


ensuring that their work positively impacts society and adheres to legal and ethical guidelines.
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

Program Outcomes

Engineering Graduates will be able to:

PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.

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.

PO3: Design/development of solutions: Design solutions for complex engineering


problems and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.

PO4: Conduct investigations of complex problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

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.

PO7: Environment and sustainability: Understand the impact of the professional


engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

PO8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.

PO9: Individual and team-work: Function effectively as an individual, and as a member


or leader in diverse teams, and in multidisciplinary settings.

PO10: 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.

PO11: Project management and finance: Demonstrate knowledge and understanding of


the 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 multidisciplinary environments.

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.

Program Specific Outcomes

PSO1: Enable students to design system and system architecture, inculcating software,
computing and analytical ability.

PSO2: Enhance skills to be successful in National, International level competition like


GATE, GRE, GMAT.
Year of Study: 2025-26
LESSON PLAN

Semester & Year: 3rd Semester, 2025-26 Faculty Name: Likhitha V


Subject with Code: Object Oriented Programming with JAVA Laboratory [BCS306A]

COURSE OUTCOME: On completion of this subject, students will be expected to:

CO1: Demonstrate proficiency in writing simple programs involving branching and


looping structures.
CO2: Design a class involving data members and methods for the given scenario.
CO3: Apply the concepts of inheritance and interfaces in solving real world problems.
CO4: Use the concept of packages and exception handling in solving complex problem

CO5: Apply concepts of multithreading, auto-boxing and enumerations in program development

Week Program Experiment CO’s


No.
Use Eclipse/Net-Beans IDE to design, develop, debug Java Projects.
 Students should be familiarized about java installation and
setting the java environment. CO1
01  Usage of IDE’s like Eclipse/NetBeans should be introduced.
Develop a JAVA program to add TWO matrices of suitable order N
CO1
02 01 (The value of N should be read from command line arguments).
Develop a stack class to hold a maximum of 10 integers with
03 02 suitable methods. Develop a JAVA main method to illustrate Stack CO1
operations.
A class called Employee, which models an employee with an ID,
04 03 name and salary, is designed as shown in the following class CO1
diagram. The method raiseSalary (percent) increases the salary by
the given percentage. Develop the Employee class and suitable
main method for demonstration.
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).
05 04 ● A default (or "no-arg") constructor that construct a point at the CO2
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
Develop a JAVA program to create a class named shape. Create
06 05 three sub classes namely: circle, triangle and square, each class has CO2
two member functions named draw () and erase (). Demonstrate
polymorphism concepts by developing suitable methods, defining
member data and main program.
07 06 Develop a Java program to create an abstract class Shape with CO3
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.
08 07 Develop a JAVA program to create an interface Resizable with CO3
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
09 08 Develop a JAVA program to create an outer class with a function CO3
display. Create another class inside the outer class named inner with
a function called display and call the two functions in the main
class.
10 09 Develop a JAVA program to raise a custom exception (user defined CO3
exception) for DivisionByZero using try, catch, throw and finally.

11 10 Develop a JAVA program to create a package named mypack and CO4


import & implement it in a suitable class.

12 11 Write a program to illustrate creation of threads using runnable CO5


class. (start method start each of the newly created thread. Inside the
run method there is sleep() for suspend the thread for 500
milliseconds).
13 12 Develop a program to create a class MyThread in this class a CO5
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.

Faculty PAC HoD, AI &DS


Object Oriented Programming with JAVA Semester 3
Course Code BCS306A CIE Marks 50
Teaching Hours/Week(L:T:P:S) [Link] SEE Marks 50
Total Hours of Pedagogy 28Hours of Theory+20Hours of Practical 10
Total Marks 0
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 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.

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

Suggested Learning Resources:

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]

Web links and Video Lectures(e-Resources):


● Java Tutorial: [Link]
● Introduction To Programming In Java (by Evan Jones, Adam Marcus and Eugene
Wu):[Link]
● Java Tutorial: [Link]
● Java Tutorial: [Link]
Activity Based Learning(Suggested Activities)/Practical Based learning
1. Installation of Java (Refer: [Link]
2. Demonstration of online IDEs like geeks for geeks, 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
Object Oriented Programming with Java [BCS306A]

Eclipse:

Eclipse (@ [Link]) is a free and open-source Java Integrated Development Environment


(IDE), originated from IBM inspired by VisualAge (in 2001), and now maintained by Eclipse
Foundation. Eclipse is a desktop app written mostly in Java. However, it uses its own Java graphics
library known as SWT (Standard Widget Toolkit), instead of Java's Swing/AWT.
Eclipse is popular for Java application development (Java SE and Java EE) and Android apps.
Eclipse is cross- Platform and runs under Windows, Linux and macOS.

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.

2. Create a shortcut on your desktop to the [Link] file.


 On most Windows machines, you can
 Right-press the file [Link]
 Drag it to the desktop.
 Release the right button.
 Click Create shortcut here

Now you are ready to perform a one-time only setup of Eclipse.


3. Double-click the shortcut to Eclipse that you just created. In the Workspace Launcher
window, in the box following Workspace:, should appear something like C:\Documents and
Settings\username\workspace (where username is your login on the machine). If you want,
you can type in (or browse) another location for the workspace file to be created, but I advise
accepting the default.
4. Uncheck the box labeled Use this as the default and do not ask again. Aside: you will be
using one workspace during the semester, checking projects in and out of this workspace. If
you ever want to re-enable the display of this window, once Eclipse starts, you can
 Select Window | Preferences.
 Click the + in front of General or double-click General (after the +)
 Click Startup and Shutdown
 Check the box labeled Prompt for workspace on startup.

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.

7. Terminate (X) the Welcome window


You can always get it back by selecting Help|Welcome

8. Terminate (X) the Eclipse window.

Dept. of AI&DS, GSSSIETW, Mysore


3rd Sem–Object Oriented Programming with Java [BCS306A]

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

public class MatrixAddition {


public static void main(String[] args)
{
int m,n;
int a[][], b[][], c[][];
Scanner scanner = new Scanner([Link]);
[Link]("Enter the size of matrix(row,col) : ");
m = [Link]();
n = [Link]();
[Link]("Enter the elements of first matrix");
a = readMatrix(m,n);
[Link]("Enter the elements of second matrix");
b = readMatrix(m,n);
c = addMatrix(a,b);
[Link]("Result");
printMatrix(c);
}

public static int[][] readMatrix(int m, int n)


{
Scanner scanner = new Scanner([Link]);
int a[][] = new int[m][n];
for ( inti = 0; i< m; i++ )
for ( int j = 0; j < n; j++ )
a[i][j] = [Link]();
return a;
}

public static void printMatrix(int a[][])


{
for ( inti = 0; i<[Link]; i++ )
{
for ( int j = 0; j < a[0].length; j++ )
[Link](a[i][j] + "");

R Dept. of AI&DS, GSSSIETW, Mysore


1
3rd Sem–Object Oriented Programming with Java [BCS306A]

[Link]();
}
}

public static int[][] addMatrix(int a[][], int b[][])


{
int c[][] = new int[[Link]][a[0].length];
for ( inti = 0; i<[Link]; i++ )
for ( int j = 0; j < a[0].length; j++ )
c[i][j] = a[i][j] + b[i][j];
return c;
}
}

R Dept. of AI&DS, GSSSIETW, Mysore


2
3rd Sem–Object Oriented Programming with Java [BCS306A]

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;

void push(int element)


{
if ( top>= MAX-1 )
[Link]("Stack overflow");
else
s[++top] = element;
}
int pop()
{
int element = 0;
if ( top == -1 )
[Link]("Stack underflow");
else
element = s[top--];
return element;
}
void display()
{
if ( top == -1 )
[Link]("Stack empty");
else
{
for ( inti = top; i> -1; i--)
[Link](s[i]+"");
}
}
}

import [Link];
//[Link]
public class StackDemo
{
public static void main(String args[])
{

R Dept. of AI&DS, GSSSIETW, Mysore


3
3rd Sem–Object Oriented Programming with Java [BCS306A]

Stack stack = new Stack();


[Link]("Program to perform stack operations");

Scanner scanner = new Scanner([Link]);


while( true )
{
[Link]("1. push [Link] 3. display [Link]");
[Link]("Enter your choice");
int choice = [Link]();
int element = 0;
switch (choice)
{
case 1:
[Link]("enter the element to be pushed");
element = [Link]();
[Link](element);
break;
case 2:
element = [Link]();
[Link]("the poped element is" + element);
break;
case 3:
[Link]("elements in the stack are");
[Link]();
break;
case 4:
return;
}
}
}
}

R Dept. of AI&DS, GSSSIETW, Mysore


4
3rd Sem–Object Oriented Programming with Java [BCS306A]

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;

public Employee(int id, String name, int salary) {


[Link] = id;
[Link] = name;
[Link] = salary;
}

public int raiseSalary(int percent)


{
return [Link] + [Link] * percent / 100;
}
}

//[Link]
import [Link];

public class EmployeeDemo {


public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
int id;
String name;
int salary;

[Link]("Enter employee id, name, salary");


id = [Link]();
name = [Link]();
salary = [Link]();

Employee employee = new Employee(id,name,salary);

[Link]("Enter percent raise");


int percent = [Link]();
int raisedSalary = [Link](percent);

R Dept. of AI&DS, GSSSIETW, Mysore


5
3rd Sem–Object Oriented Programming with Java [BCS306A]

[Link]("Raised salary : " + raisedSalary)

}
}

R Dept. of AI&DS, GSSSIETW, Mysore


6
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

public MyPoint(int x, int y)


{
this.x = x;
this.y = y;
}

public void setX(int x)


{
this.x = x;
}

public void setY(int y)


{
this.y = y;

Dept. of AI&DS, GSSSIETW, Mysore


7
3rd Sem–Object Oriented Programming with Java [BCS306A]

public int getX()


{
return this.x;
}

public int getY()


{
return this.y;
}

public double distance()


{
double dist = 0.0;
dist= [Link](this.x * this.x + this.y * this.y);
return dist;
}

public double distance(int x, int y)


{
double dist = 0.0;

double xdiff = this.x - x;


double ydiff = this.y - y;

dist = [Link](xdiff * xdiff + ydiff * ydiff);


return dist;
}

public double distance(MyPoint point)


{
double dist = 0.0;

double xdiff = this.x - [Link]();


double ydiff = this.y - [Link]();

dist = [Link](xdiff * xdiff + ydiff * ydiff);

return dist;
}

public String toString()


{
return "(" + this.x + "," + this.x + ")";
}

Dept. of AI&DS, GSSSIETW, Mysore


8
3rd Sem–Object Oriented Programming with Java [BCS306A]

import [Link];

public class PointDemo {


public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
int choice = 0;
int x, y;
double distance = 0;

[Link]("Enter the co-ords of Point under consideration");


x = [Link]();
y = [Link]();
MyPoint point = new MyPoint(x,y);

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

Dept. of AI&DS, GSSSIETW, Mysore


9
3rd Sem–Object Oriented Programming with Java [BCS306A]

break;

case 0:
return;
}
[Link]("Distance : " + distance);
}
}
}

import [Link];

public class PointDemo {


public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
int choice = 0;
int x, y;
double distance = 0;

[Link]("Enter the co-ords of Point under consideration");


x = [Link]();
y = [Link]();
MyPoint point = new MyPoint(x,y);

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

Dept. of AI&DS, GSSSIETW, Mysore


10
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

Dept. of AI&DS, GSSSIETW, Mysore


11
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

public class ShapeDemo {


public static void main(String[] args)
{
Shape shape;

shape = new Circle();


[Link]();
[Link]();

shape = new Triangle();


[Link]();
[Link]();

shape = new Square();


[Link]();
[Link]();
}
}
public class Square implements Shape {
public void draw()
{
[Link]("[Link]() ...");
}
public void erase()

Dept. of AI&DS, GSSSIETW, Mysore


12
3rd Sem–Object Oriented Programming with Java [BCS306A]

{
[Link]("[Link]() ...");
}
}

public class Triangle implements Shape {


public void draw()
{
[Link]("[Link]() ...");
}
public void erase()
{
[Link]("[Link]() ...");
}

Dept. of AI&DS, GSSSIETW, Mysore


13
3rd Sem–Object Oriented Programming with Java [BCS306A]

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 {

private int radius;

public Circle(int radius) {


[Link] = radius;
}

@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];

public class ShapeDemo {


public static void main(String[] args)
{
int option = 0;
int radius;
int a;
int b;
int c;

float perimeter;
float area;

Scanner scanner = new Scanner([Link]);

Dept. of AI&DS, GSSSIETW, Mysore


14
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

}
}

public class Triangle extends Shape {

private int a;
private int b;
private int c;

public Triangle(int a, int b, int c) {


this.a = a;
this.b = b;
this.c = c;

Dept. of AI&DS, GSSSIETW, Mysore


15
3rd Sem–Object Oriented Programming with Java [BCS306A]

@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;
}
}

Dept. of AI&DS, GSSSIETW, Mysore


16
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

package pgm7;
public class Rectangle implements Resizable {

private int height;


private int width;

public Rectangle()
{

public Rectangle(int height, int width) {


[Link] = height;
[Link] = width;
}

@Override
public void resizeHeight(int height) {
[Link] = height;
}

@Override
public void resizeWidth(int width) {
[Link] = width;
}

public String toString()


{
return "Height : " + [Link] + " Width : " + [Link];
}
}

import [Link];

public class RectangleDemo {


public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);

Dept. of AI&DS, GSSSIETW, Mysore


17
3rd Sem–Object Oriented Programming with Java [BCS306A]

int height = 0;
int width = 0;

[Link]("Enter the height and width of rectangle");


height = [Link]();
width = [Link]();

Rectangle rectangle = new Rectangle(height, width);


[Link](rectangle);

[Link]("Enter the new height and width");


int newHeight = [Link]();
int newWidth = [Link]();

Resizable resizable = new Rectangle();


[Link](newHeight);
[Link](newWidth);

[Link](resizable);

}
}

public interface Resizable {


public void resizeHeight(int height);
public void resizeWidth(int width);

Dept. of AI&DS, GSSSIETW, Mysore


18
3rd Sem–Object Oriented Programming with Java [BCS306A]

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]()");
}
}
}

public class OuterInnerDemo {


public static void main(String[] args)
{
Outer o = new Outer();
[Link]();

[Link] = [Link]();
[Link]();
}
}

Dept. of AI&DS, GSSSIETW, Mysore


19
3rd Sem–Object Oriented Programming with Java [BCS306A]

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

public class ExceptionDemo {


public static void main(String[] args)
{
int n = 0;
Scanner scanner = new Scanner([Link]);
[Link]("Enter the divisor : ");
n = [Link]();

try
{
int quotient = 22 / n;
[Link]("quotient : " + quotient);
}
catch(ArithmeticException ae)
{
try
{
throw new MyException();
}
catch(MyException me)
{

}
}
}
}

public class MyException extends Exception {


public MyException()
{
[Link]("[Link]() ...");
}
}

Dept. of AI&DS, GSSSIETW, Mysore


20
3rd Sem–Object Oriented Programming with Java [BCS306A]

10. Develop a JAVA program to create a package named mypack and import &
implement it in asuitable class.

package [Link];

public class MyClass {


public void display()
{
[Link]("Inside [Link]() method ...");
}
}

import [Link];

public class PackageDemo {


public static void main(String[] args)
{
MyClassmyClass = new MyClass();
[Link]();
}
}

Dept. of AI&DS, GSSSIETW, Mysore


21
3rd Sem–Object Oriented Programming with Java [BCS306A]

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;

public class MyThread implements Runnable {


@Override
public void run()
{
for ( inti = 1; i<= 10; i++ )
[Link]("run() : " + [Link]().getName());
try
{
[Link](2000);
}
catch(InterruptedExceptionie)
{
[Link](ie);
}
}
}

public class ThreadDemo {


public static void main(String[] args)
{
Thread t1,t2,t3;

t1 = new Thread(new MyThread());


t2 = new Thread(new MyThread());
t3 = new Thread(new MyThread());

[Link]("JIT1");
[Link]("JIT2");
[Link]("JIT3");

[Link]();
[Link]();
[Link]();

for ( inti = 0; i< 10; i++ )


[Link]("run() : " + [Link]().getName());
}
}

Dept. of AI&DS, GSSSIETW, Mysore


22
3rd Sem–Object Oriented Programming with Java [BCS306A]

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;

public class MyThread extends Thread {


public MyThread()
{
setName("JIT");
}

@Override
public void run()
{
for ( inti = 0; i< 50; i++ )
[Link]("run() : " + [Link]().getName());

try {
[Link](5000);
} catch (InterruptedException e) {
[Link](e);
}
}

public static void main(String[] args)


{
new MyThread().start();
for ( inti = 0; i< 50; i++ )
[Link]([Link]().getName());

}
}

Dept. of AI&DS, GSSSIETW, Mysore


23

You might also like