Java Lab Manual BCS306A
Java Lab Manual BCS306A
LABORATORY MANUAL
Prepared by,
Dr. Sharanabasavana Gowda, Dr. Sreenatha M, Mr. Vikhyath K B
Bengaluru - 560060
2023-2024
JSS ACADEMY OF TECHNICAL EDUCATION
(Approved by AICTE, New Delhi, Affiliated to Visvesvaraya Technological University, Belagavi, Karnataka, INDIA)
Vision
“To be among the finest Institutions providing Engineering and Management Education
empowered with research, innovation and entrepreneurship”
Mission
Vision
Mission
3. Provide a conducive environment for faculty and students to carry out research
and innovation in collaboration with reputed research institutes and industry.
4. Inculcate human values and professional ethics among students to enable them
to become good citizens and serve the society.
JSS ACADEMY OF TECHNICAL EDUCATION
(Approved by AICTE, New Delhi, Affiliated to Visvesvaraya Technological University, Belagavi, Karnataka, INDIA)
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
andmodern 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 environments.
a. PSO1: Apply the principles of Basic Engineering Science to acquire the hardware and
software aspects of Computer Science.
b. PSO2: Solve the real-world problems using modelling for a specific Computer system
and architecture.
c. PSO3: Ability to design and develop applications using various software and hardware
tools.
d. PSO4. Exhibit the practical competence using broad range of programming languages.
List of Experiments
Sl. No. PART A – List of problems for which student should develop program and execute in the
Laboratory
Program: Develop a JAVA program to add TWO matrices of suitable order N (The value of N
1 should be read from command line arguments).
Program: Develop a stack class to hold a maximum of 10 integers with suitable methods.
2 Develop a JAVA main method to illustrate Stack Operations.
Program: A class called Employee, which models an employee with an ID, name and salary, is
3 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.
Program: A class called MyPoint which models a 2D point with X and Y coordinates, is designed as
follows:
4 • 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 construct 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 that 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 diatance 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: Develop a JAVA program to create a class named shape. Create three sub classes namely:
5 circle, triangle and square, each class has two member functions named draw() and erase().
Demonstrate polymorphism concepts by developing suitable methods, defining members data and
main program.
Program: Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeters(). Create subclasses Circle and Triangle that extend the
6 Shape class and implement the respective methods to calculate the area and perimeters of each
shape.
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
7 implement the Resizable interface and implements the resize methods.
Program: 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
8 functions in the main class.
Program: Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
9
Program: Develop a JAVA program to create a package named mypack and import & implement it
10 in a suitable class.
Program: 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
11 500 milliseconds).
Program: 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
12 be observed that both main thread and created child thread are executed concurrently.
CO 1. Apply the fundamental concepts of object-oriented programming to solve the real-world problems.
CO 2. Apply Java language constructs to implement application programs.
CO 3. Develop applications using inheritance, packages and exception handling features of Java.
CO 4. Apply concepts of multi-threading, auto boxing and enumerations in program development.
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). The minimum passing
mark for the SEE is 35% of the maximum marks (18 marks out of 50). 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 Test ( Two Test,
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.
• 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 student 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
ecperiment’s 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 evaluation 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.
Theory SEE will be conducted by University as per the scheduled timetable, with common question papers for the
course (duration 03 hours)
1. Java: The Complete Reference, Twelfth Edition, by Herbert Schildt, November 2021, McGraw-Hill,
ISBN:9781260463422
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)
OOP with Java /BCS306A
PROGRAM :1
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:
// Initialize matrices
int[][] matrix1 = new int[N][N];
int[][] matrix2 = new int[N][N];
int[][] result = new int[N][N];
{
System.out.print("Enter element at position (" + i + ", " + j + "): ");
matrix[i][j] = scanner.nextInt();
}
}
}
PROGRAM :2
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:
// Constructor
public Stack()
{
stackArray = new int[MAX_SIZE];
top = -1;
}
myStack.pop();
myStack.display();
myStack.push(15);
myStack.push(25);
myStack.push(30);
myStack.display();
myStack.pop();
myStack.pop();
myStack.display();
}
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.
PROGRAM :
// Constructor
public Employee(int id, String name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
// Getter methods
public int getId()
{
return id;
}
6
OOP with Java /BCS306A
PROGRAM :4
4 A class called MyPoint which models a 2D point with X and Y coordinates, is designed as follows:
Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint) to
test all the methods defined in the class.
PROGRAM:
// Default constructor
public MyPoint()
{
this.x = 0;
this.y = 0;
}
// Overloaded constructor
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
// Display points
System.out.println("Point 1: " + point1.toString());
System.out.println("Point 2: " + point2.toString());
PROGRAM :5
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(). Demonstarte
polymorphism concepts by developing suitable methods, defining members data and main
program.
PROGRAM:
class Shape
{
// Member functions
public void draw()
{
System.out.println("Drawing a shape");
}
// Demonstrate polymorphism
for (Shape shape : shapes)
{
shape.draw();
shape.erase();
System.out.println(); // Add a newline for clarity
}
}
PROGRAM :6
6. Develop a JAVA program to create an abstract class Shape with abstract methods calculateArea()
and calculatePerimeters(). Create subclasses Circle and Triangle that extend the Shape class and
implement the respective methods to calculate the area and perimeters of each shape.
PROGRAM:
// Constructor
public Circle(double radius)
{
this.radius = radius;
}
@Override
public double calculatePerimeter()
{
return 2 * Math.PI * radius;
}
}
// Constructor
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
PROGRAM :7
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
implement the Resizable inteeface and implements the resize methods.
PROGRAM:
// Constructor
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
PROGRAM :8
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
{
// Outer class display method
public void display()
{
System.out.println("Outer class display method");
}
// Inner class
class Inner
{
// Inner class display method
public void display()
{
System.out.println("Inner class display method");
}
}
// Create an instance of the inner class using the outer class instance
Outer.Inner inner = outer.new Inner();
9. Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero using try,
catch, throw and finally.
PROGRAM:
17
OOP with Java /BCS306A
18
OOP with Java /BCS306A
PROGRAM :10
10. Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
PROGRAM:
// mypack/MyClass.java
package mypack;
// MainClass.java
import mypack.MyClass;
Ensure that both files are in separate directories, and the MainClass.java file is not inside the mypack
directory.
javac MainClass.java
java MainClass
PROGRAM :11
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:
@Override
public void run()
{
try
{
for (int i = 1; i <= 5; i++)
{
System.out.println(threadName + ": Count - " + i);
// Sleep for 500 milliseconds
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(threadName + " thread interrupted.");
}
System.out.println(threadName + " thread exiting.");
}
}
20
OOP with Java /BCS306A
// Start the threads
thread1.start();
thread2.start();
// Main thread
for (int i = 1; i <= 5; i++) {
System.out.println("Main Thread: Count - " + i);
try
{
// Sleep for 500 milliseconds
Thread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
}
21
OOP with Java /BCS306A
PROGRAM :12
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
// Main thread
try
{
for (int i = 1; i <= 5; i++)
{
System.out.println(Thread.currentThread().getName() + ": Count - " + i);
// Sleep for 500 milliseconds
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " thread interrupted.");
}