Java Lab Manual
Java Lab Manual
K S INSTITUTE OF TECHNOLOGY
#14 Raghuvanahalli Kanakapura road Bangalore – 560109
LAB MANUAL
Prepared By
Dr. Kusuma T
Associate Professor
Dept of CSD
KSIT, Bangalore
Object Oriented Programming with JAVA- Manual
K S INSTITUTE OF TECHNOLOGY
VISION
“To impart quality technical education with ethical values, employable
skills and research to achieve excellence”
MISSION
VISION
MISSION
To groom the students with the needed practical skills to cater to the
needs of society through projects.
Object Oriented Programming with JAVA- Manual
PEOs
interdisciplinary field.
2. Exhibit creative thinking and continue learning through higher studies and
research.
3. Proficient in solving real-life problems related to innovation through
teamwork and ethics.
PSOs
PROGRAM OUTCOMES
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.
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.
Object Oriented Programming with JAVA- Manual
List of Experiments
Object Oriented Programming with JAVA- Manual
Object Oriented Programming with JAVA- Manual
COURSE OUTCOMES
Applying
Demonstrate proficiency in writing simple programs involving branching
CO1
and looping structures (K3)
Applying
Design a class involving data members and methods for the given
CO2
scenario. (K3)
Applying
Apply the concepts of inheritance and interfaces in solving real world
CO3
problems. (K3)
Applying
Use the concept of packages and exception handling in solving complex
CO4
problem. (K3)
Apply concepts of multithreading, autoboxing and enumerations in Applying
CO5
program development (K3)
Object Oriented Programming with JAVA- Manual
PROGRAMMING EXPERIMENTS
1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be
read from command line arguments).
b[i][j] = sc.nextInt();
}
}
System.out.println("");
//Print the first matrix
System.out.println("First Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
//Print the second matrix
System.out.println("Second Matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
//Loop to add matrix elements
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < q; k++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
}
//Print the resultant matrix
System.out.println("Matrix after addition:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
Object Oriented Programming with JAVA- Manual
}
}
else
{
System.out.println("Addition not possible");
System.out.println("Try Again");
}
}
}
OUTPUT
Enter number of rows in first matrix:2
Enter number of columns in first matrix :2
Enter number of rows in second matrix:2
Enter number of columns in second matrix:2
Enter all the elements of first matrix:
23
12
Enter all the elements of second matrix:
33
33
First Matrix:
23
12
Second Matrix:
33
33
Matrix after addition:
56
45
2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
JAVA main method to illustrate Stack operations.
import java.io.*;
import java.util.*;
class Test
{
Object Oriented Programming with JAVA- Manual
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: " + pos);
}
Object Oriented Programming with JAVA- Manual
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
OUTPUT
Pop Operation:
10,9,8,7,6,5,4,3,2,1,0
Element on stack top: 10
Element is found at position: 1
Element not found
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.
} else {
System.out.println("Invalid percentage. Salary remains unchanged.");
}
}
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.
public class MyPoint {
private int x;
private int y;
public MyPoint() {
this(0, 0); // Default constructor sets the point at (0, 0)
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
Output
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 Shape {
public void draw() {
System.out.println("Drawing a generic shape.");
}
@Override
public void erase() {
System.out.println("Erasing a circle.");
}
}
@Override
public void draw() {
System.out.println("Drawing a triangle.");
}
@Override
public void erase() {
System.out.println("Erasing a triangle.");
}
}
@Override
public void erase() {
System.out.println("Erasing a square.");
}
}
OUTPUT
$ java ShapeDemo
Drawing a circle with radius 5.0
Erasing a circle with radius 5.0
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.
// Shape.java
// Abstract class Shape
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
// Circle.java
// Subclass Circle
class Circle extends Shape {
private double radius;
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
@Override
double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
// Triangle.java
// Subclass Triangle
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
@Override
double calculateArea() {
double s = (side1 + side2 + side3) / 2; // Semi-perimeter
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
// Main.java
// Subclass Main
//Resizable.java
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
//Rectangle.java
class Rectangle implements Resizable {
private int width;
private int height;
rectangle.resizeWidth(150);
rectangle.resizeHeight(200);
rectangle.printSize();
}
}
OUTPUT
Object Oriented Programming with JAVA- Manual
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("Outer class display()");
}
class Inner {
void display() {
System.out.println("Inner class display()");
}
}
}
9.Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally
// Custom exception class for DivisionByZero
if (denominator == 0) {
throw new DivisionByZeroException();
}
1. Create the mypack directory and the MyClass.java file inside it.
Directory Structure:
- mypack
- MyClass.java
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.
@Override
public void run() {
System.out.println("Child thread is running.");
}
OUTPUT
Main thread is running concurrently with the child thread.
Child thread is running.
Object Oriented Programming with JAVA- Manual
VIAV -QUESTIONS