KEMBAR78
Java Lab (AL 406) Lab Manual June 2025 For ALS1 ALS2 | PDF | Method (Computer Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
51 views39 pages

Java Lab (AL 406) Lab Manual June 2025 For ALS1 ALS2

The document outlines the Java Lab (AL-406) course at Acropolis Institute of Technology and Research, detailing its objectives, syllabus, and laboratory instructions for students. It emphasizes hands-on experience in Java programming, covering key concepts like object-oriented principles and GUI development, while providing guidelines for lab conduct. Additionally, it includes evaluation records for various programming assignments and projects aimed at reinforcing students' programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views39 pages

Java Lab (AL 406) Lab Manual June 2025 For ALS1 ALS2

The document outlines the Java Lab (AL-406) course at Acropolis Institute of Technology and Research, detailing its objectives, syllabus, and laboratory instructions for students. It emphasizes hands-on experience in Java programming, covering key concepts like object-oriented principles and GUI development, while providing guidelines for lab conduct. Additionally, it includes evaluation records for various programming assignments and projects aimed at reinforcing students' programming skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 39

2024-25

Acropolis Institute of
Technology and
Research, Indore
Department of CSE
Submitted To:
(Artificial Intelligence & Machine
Mr. Dharmendra Singh Chouhan
Learning)

Java Lab (AL-406)

Submitted By:
Name of Student
Enrollment No. : 0827
Class/Year/Sem : ALS-/2nd / 4th

[LAB ASSIGNMENT JAVA LAB (AL-406)]

The Objective of this laboratory work is to strengthen the ability of the students to identify
and apply the suitable Java Programming for the given real world problem. It enables them to
gain knowledge in practical applications of Java Programming.
ACROPOLIS INSTITUTE OF TECHNOLOGY & RESEARCH,
INDORE

Department of CSE (Artificial Intelligence & Machine Learning)

CERTIFICATE

This is to certify that the experimental work entered in this journal as per

the B. TECH. II year syllabus prescribed by the RGPV was done by Mr./

Ms. …………………………………B.TECH II year III semester in the

Java Lab (AL-406) Laboratory of this institute during the academic year

2024- 2025.

Signature of the Faculty


About the Laboratory

The Java Programming Lab is designed to provide students with a strong foundation in object-
oriented programming using Java. Through practical exercises and real-time coding sessions,
students will develop the skills needed to design, implement, and debug Java programs.

This lab complements the core Java programming course by offering a hands-on learning
environment where students apply theoretical concepts to solve real-world problems. Students will
explore object-oriented principles such as encapsulation, inheritance, polymorphism, and
abstraction, and implement these concepts in various Java applications.

Key features of the lab include:

 Writing and executing Java programs to solve computational and logical problems.

 Understanding and implementing object-oriented programming concepts.

 Working with packages, interfaces, exception handling, multithreading, file I/O, and GUI
development using AWT.

 Developing mini-projects and modular applications using best coding practices.

 Utilizing modern IDEs and Java Development Kit (JDK) on the latest platforms for coding,
compiling, and debugging.

This lab enables students to build strong programming logic, structured code development skills,
and prepares them for advanced courses and industry-level software development.
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES

 DO’S

 Without Prior permission do not enter into the Laboratory.

 While entering into the LAB students should wear their ID cards.

 The Students should come with proper uniform.

 Students should sign in the LOGIN REGISTER before entering into the
laboratory.

 Students should come with observation and record note book to the laboratory.

 Students should maintain silence inside the laboratory.

 After completing the laboratory exercise, make sure to shutdown the system
properly.

 DONT’S

 Students bringing the bags inside the laboratory.

 Students using the computers in an improper way.

 Students scribbling on the desk and mishandling the chairs.

 Students using mobile phones inside the laboratory.

 Students making noise inside the laboratory.


SYLLABUS
Course: AL 406 (Java Lab)
Branch/Year/ Sem: Java Lab / II / IV

Module1: Overview of Java, Installation, First Simple Program, Compilation process,


Java Keywords ,Identifiers , Literals, Comments, Data Types, Variables, Dynamic
initialization, type conversion and casting, Operators, Control Statements.

Module2: Declaring Objects, Introducing Methods, Constructors, this Keyword,


Garbage Collection, finalize Method, Overloading Methods, Overloading Constructors,
Using Objects as Parameters, Inheritance, Creating a Multilevel Hierarchy, Packages
and Interfaces, Exception Handling, Multithreaded

Module3: The Applet Class: Applet Basics, The Applet Class, Applet Architecture,
Applet Initialization and Termination, Simple Applet Display Methods, Simple Banner
Applet, Usingthe Status Window, The HTML APPLET Tag, Passing Parameters to
Applets, Improving the Banner Applet.

Module4: Introducing the AWT: Working with Windows, Graphics, and Text, AWT
Classes, Window Fundamentals, Component, Container, Panel, Frame, Working with
Frame Windows, Handling Events in a Frame Window, AWT Controls, Layout
Managers, and Menus, Adding and Removing Controls, Grid Layout, Border Layout,
introduction to swing and servlet.

Module5: Event Handling, Two Event Handling Mechanisms, The Delegation Event
Model, Events, Event Sources, Event Listeners, Event Classes, The Mouse Event Class
and others, JDBC: JDBCODBC Bridge, the connectivity model, the driver manager,
navigating the result set object contents, the JDBC exceptional classes, connecting to
remote database.

HARDWARE AND SOFTWARE REQUIREMENTS:

S. Name of Item Specification


No.
1 Computer System Hard Disk min 5 GB
RAM: 4 GB / 8 GB
Processor: Intel i3 or above

S. Name of Item Specification


No.
1 Operating system Window XP or 2000
Editor NetBeans, Eclipse, IntelliJ IDEA, or
Notepad++
RATIONALE:
The course is designed to teach students the foundational and advanced features of Java
programming. It provides hands-on experience in writing efficient, reusable, and scalable
Java applications.

PREREQUISITE:-
Basic knowledge of programming in C/C++ is preferred. Prior understanding of object-
oriented principles is helpful but not mandatory.

COURSE OBJECTIVES AND OUTCOMES

 Course Objectives

1. To learn the basics of Java programming and syntax.


2. To understand and implement object-oriented principles using Java.
3. To develop Java programs for exception handling and multi-threading.
4. To work with Java I/O and file-handling mechanisms.
5. To create GUI-based applications and explore collection frameworks.

 Course Outcomes

1. At the end of the course student will be able to:


2. Write structured Java programs using core syntax and OOP concepts.
3. Create classes, objects, and apply inheritance and polymorphism.
4. Implement exception handling and concurrent programming in Java.
5. Perform file operations and use streams for input/output.
6. Develop GUI-based Java applications and work with collections.
Index
Date of Page Date of Grade & Sign
S.No Exp. Name of the Experiment No. Submission of the Faculty

1 Write a Java program to implement a simple


calculator using switch-case. (CO1)

2 Write a Java program to demonstrate class and


object using a Student class. (CO1)

3 Write a Java program to implement single,


Multilevel, Hierarchical inheritance using Animal,
Dog and Cat classes. (CO2)

4 Write a Java program to demonstrate constructor


chaining using this and super keywords. (CO3)

5 Write a Java program to create an abstract class


Shape and extend it in Circle. (CO2)

6 Write a Java program to define an interface Vehicle


and implement it in classes Car and Bike. (CO2)

7 Write a Java program to demonstrate multiple


inheritance using two interfaces. (CO2)

8 Write a Java program demonstrating method


overriding using Vehicle and Bike classes. (CO2)

9 Write a Java program to demonstrate method


overloading by creating a class Calculator that has
multiple addition() methods with different parameter
types and counts. (CO4)

10 Write a Java program to create a custom exception


class and use it. (CO3)
Program Outcome (PO)

The engineering graduate of this institute will demonstrate:


a) Apply knowledge of mathematics, science, computing and engineering fundamentals to computer
science engineering problems.
b) Able to identify, formulate, and demonstrate with excellent programming, and problem solving skills.
c) Design solutions for engineering problems including design of experiment and processes to meet
desired needs within reasonable constraints of manufacturability, sustainability, ecological,
intellectual and health and safety considerations.
d) Propose and develop effective investigational solution of complex problems using research
methodology; including design of experiment, analysis and interpretation of data, and combination of
information to provide suitable conclusion. synthesis
e) Ability to create, select and use the modern techniques and various tools to solve engineering
problems and to evaluate solutions with an understanding of the limitations.
f) Ability to acquire knowledge of contemporary issues to assess societal, health and safety, legal and
cultural issues.
g) Ability to evaluate the impact of engineering solutions on individual as well as organization in a
societal and environmental context, and recognize sustainable development, and will be aware of
emerging technologies and current professional issues.
h) Capability to possess leadership and managerial skills, and understand and commit to professional
ethics and responsibilities.
i) Ability to demonstrate the team work and function effectively as an individual, with an ability to
design, develop, test and debug the project, and will be able to work with a multi-disciplinary team.
j) Ability to communicate effectively on engineering problems with the community, such as being able
to write effective reports and design documentation.
k) Flexibility to feel the recognition of the need for, and have the ability to engage in independent and
life- long learning by professional development and quality enhancement programs in context of
technological change.
l) A practice 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 entrepreneurship.
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implementation of a Simple
Calculator using Switch-Case in Java
Lab: Java Lab (AL-406)

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title
Implementation of a Simple Calculator using Switch-Case in Java
2 Neatly Drawn and labeled experimental setup
3 Theoretical solution of the instant problem
3.1 Algorithm
Start
2. Prompt the user to enter two numbers and an operator (+,
-, *, /)
3. Use a switch-case structure to perform the operation based
on the operator:
• + ➝ Addition
• - ➝ Subtraction
• * ➝ Multiplication
• / ➝ Division (check for divide by zero)
4. Display the result
5. Stop

3.2 Program
import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
double num1, num2, result;
char operator;

Scanner sc = new Scanner(System.in);

System.out.print("Enter first number: ");


num1 = sc.nextDouble();

System.out.print("Enter second number: ");


num2 = sc.nextDouble();

System.out.print("Enter operator (+, -, *, /): ");


operator = sc.next().charAt(0);

switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Result: " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + result);
break;

Page 10
case '*':
result = num1 * num2;
System.out.println("Result: " + result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero.");
}
break;
default:
System.out.println("Invalid operator!");
}

sc.close();
}
}

4 Tabulation Sheet

INPUT OUTPUT
5
10,5,+ 15.0
20,4,/ 5.0
7,3,* 21.0

Results
The program successfully accepts two numbers and an operator
from the user and performs the correct arithmetic operation using a
switch-case. It also handles invalid operators and division by zero
cases.

Page 11
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Demonstration of Class and Object
using a Student Class in Java
Lab: Java Lab (AL-406)

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title
Demonstration of Class and Object using a Student Class in Java
2 Neatly Drawn and labeled experimental setup

Page 12
3 Theoretical solution of the instant problem
3.1 Algorithm
1. Start
2. Define a class named Student
•Declare instance variables: name, roll number, and marks
• Define a method display() to show details
1. Create an object of the Student class in the main() method
2. Assign values to the object
3. Call the display() method to show student data
4. Stop

3.2 Program
class Student {
String name;
int rollNo;
double marks;

// Method to display student details


void display() {
System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNo);
System.out.println("Marks: " + marks);
}
}

public class StudentDemo {


public static void main(String[] args) {
// Creating object of Student class
Student s1 = new Student();

// Assigning values
s1.name = "Anubhi Jain";
s1.rollNo = 101;
s1.marks = 92.5;

// Calling display method


s1.display();
}
}

Page 13
4 Tabulation Sheet

INPUT OUTPUT
5
“Anil Jain”, 101, 92.5 Student Name: Anil Jain
Roll Number: 101
Marks: 92.5

Results

The program successfully demonstrates the use of class and object


by creating a Student class and using it to store and display student
information using object-oriented concepts in Java.

Page 14
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implementation of Single,
Multilevel, and Hierarchical Inheritance using
Lab: Java Lab (AL-406) Animal, Dog, and Cat Classes in Java

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title
Implementation of Single, Multilevel, and Hierarchical Inheritance
using Animal, Dog, and Cat Classes in Java

2 Neatly Drawn and labeled experimental setup

Page 15
3 Theoretical solution of the instant problem
3.1 Algorithm
Start
2. Create a base class Animal with a method eat()
3. Derive Dog and Cat classes from Animal (Hierarchical
Inheritance)
4. Derive a Puppy class from Dog (Multilevel Inheritance)
5. In each derived class, create specific methods (e.g., bark(),
meow(), weeping())
6. In the main() method, create objects of each class and call their
respective methods
7. Stop

3.2 Program
// Base class
class Animal {
void eat() {
System.out.println("Animal is eating...");
}
}

// Single & Multilevel Inheritance


class Dog extends Animal {
void bark() {
System.out.println("Dog is barking...");
}
}

// Multilevel Inheritance
class Puppy extends Dog {
void weep() {
System.out.println("Puppy is weeping...");
}
}

// Hierarchical Inheritance
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing...");
}
}

Page 16
public class InheritanceDemo {
public static void main(String[] args) {
// Single Inheritance
Dog dog = new Dog();
dog.eat(); // from Animal
dog.bark(); // from Dog

System.out.println();

// Multilevel Inheritance
Puppy puppy = new Puppy();
puppy.eat(); // from Animal
puppy.bark(); // from Dog
puppy.weep(); // from Puppy

System.out.println();

// Hierarchical Inheritance
Cat cat = new Cat();
cat.eat(); // from Animal
cat.meow(); // from Cat
}
}

4 Tabulation Sheet
CLASS METHOD OUTPUT
OBJECT CALLED
Dog eat(), bark() Animal is eating…Dog is barking…
Puppy eat(), bark(), Animal is eating…Dog is barking…Puppy
weep() is weeping…
Cat eat(), meow() Animal is eating…Cat is meowing…

5 Results
The program successfully demonstrates:
•Single Inheritance through the Dog class,
•Multilevel Inheritance via the Puppy class extending Dog,
•Hierarchical Inheritance using both Dog and Cat extending the
common Animal base class.

Page 17
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Demonstration of Constructor
Chaining using this and super Keywords in Java
Lab: Java Lab (AL-406)

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title
Demonstration of Constructor Chaining using this and super
Keywords in Java
2 Neatly Drawn and labeled experimental setup

Page 18
3 Theoretical solution of the instant problem
3.1 Algorithm
1. Start
2. Create a base class Person with a parameterized constructor
3. Create a derived class Student that extends Person
4. Define multiple constructors in Student
1. Use this() to call another constructor of the same class
2. Use super() to invoke the parent class constructor
5. Create a main() method and instantiate the Student class
6. Observe constructor call order through printed messages
7. Stop

3.2 Program
class Person {
Person(String name) {
System.out.println("Person constructor called. Name: " + name);
}
}

// Derived class
class Student extends Person {
Student() {
this("John", 101); // Calls another constructor in the same class
System.out.println("Default Student constructor called.");
}

Student(String name, int rollNo) {


super(name); // Calls the constructor of Person class
System.out.println("Student constructor called. Roll No: " +
rollNo);
}
}

public class ConstructorChainingDemo {


public static void main(String[] args) {
Student s1 = new Student();
}
}

Page 19
4 Tabulation Sheet
CONSTRUCTOR OUTPUT
CALL
new Student() Person constructor called. Name:
John
Student constructor called. Roll
No: 101
Default Student constructor
called.

5 Results
The program demonstrates constructor chaining in Java:
• this() is used to invoke another constructor of the same class
• super() is used to call the constructor of the parent class
This confirms how Java manages constructor execution order in
inheritance and overloading.

Page 20
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implementation of Abstract Class
Shape and its Extension in Circle Class
Lab: Java Lab (AL-406)

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title
Implementation of Abstract Class Shape and its Extension in Circle
Class

Page 21
2 Neatly Drawn and labeled experimental setup

3 Theoretical solution of the instant problem


3.1 Algorithm
Start
2. Define an abstract class Shape with an abstract method area()
3. Create a subclass Circle that extends Shape
4. Implement the area() method in Circle class using formula: π × r²
5. In the main() method, create an object of Circle
6. Call the area() method and display the result
7. Stop
3.2 Program
abstract class Shape {
abstract void area(); // Abstract method
}

// Concrete subclass
class Circle extends Shape {
double radius;

Circle(double r) {
radius = r;
}

// Implementing abstract method


void area() {
double result = Math.PI * radius * radius;
System.out.println("Area of Circle: " + result);
}

Page 22
}

public class AbstractDemo {


public static void main(String[] args) {
Circle c = new Circle(5.0);
c.area();
}
}

4 Tabulation Sheet
INPU OUTPUT
T
5.0 Area of Circle:
78.53981633974483
3.5 Area of Circle:
38.48451000647496
2.0 Area of Circle:
12.566370614359172

5 Results
The program successfully demonstrates the use of abstract classes
and method overriding.
The abstract class Shape provides a general structure, and Circle
provides a concrete implementation of the area() method.

Page 23
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Implementation of Interface in Java using Vehicle,
Lab: Java Lab (AL-406) Car, and Bike classes.

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Page 24
Grade: Cross the grade.
A B C D F

Tutor

6 Title: Implementation of Interface in Java using Vehicle, Car, and Bike classes.
7 Neatly Drawn and labeled experimental setup:

8 Theoretical solution of the instant problem


8.1 Algorithm:
1. Define an interface Vehicle with abstract methods start() and stop().

2. Create class Car that implements the Vehicle interface.

3. Implement start() and stop() methods in Car class.

4. Create class Bike that implements the Vehicle interface.

5. Implement start() and stop() methods in Bike class.

6. Create a main class InterfaceDemo.

7. Instantiate objects of Car and Bike using Vehicle reference.

8. Call start() and stop() methods for both objects.

9. Display the output.

Page 25
8.2 Program:
// Interface definition
interface Vehicle {
void start();
void stop();
}

// Car class implementing Vehicle interface


class Car implements Vehicle {
public void start() {
System.out.println("Car has started.");
}

public void stop() {


System.out.println("Car has stopped.");
}
}

// Bike class implementing Vehicle interface


class Bike implements Vehicle {
public void start() {
System.out.println("Bike has started.");
}

public void stop() {


System.out.println("Bike has stopped.");
}
}

// Main class
public class InterfaceDemo {

Page 26
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle bike = new Bike();

System.out.println("Car actions:");
car.start();
car.stop();

System.out.println("\nBike actions:");
bike.start();
bike.stop();
}
}
9 Tabulation Sheet

INPUT OUTPUT
Car actions:
Car has started.
Car has stopped.

Bike actions:
Bike has started.
Bike has stopped.

Results: The experiment successfully demonstrates the implementation of interfaces in Java.


The Car and Bike classes implement the Vehicle interface, and method overriding is
used to provide specific behavior.
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Demonstration of Multiple Inheritance in Java
Lab: Java Lab (AL-406) using Two Interfaces.

EVALUATION RECORD Type/ Lab Session:


Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor

Page 27
1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Tutor

1 Title: Demonstration of Multiple Inheritance in Java using Two Interfaces.


2 Neatly Drawn and labeled experimental setup:

3 Theoretical solution of the instant problem


3.1 Algorithm:
 Define interface A with method methodA().
 Define interface B with method methodB().
 Create a class MyClass that implements both interfaces.
 Provide implementations for methodA() and methodB() in MyClass.
 Create a main() method in a class named MultipleDemo.
 Create an object of MyClass and invoke both methods.
 Display the output.
3.2 Program:
// Interface A

Page 28
interface A {
void methodA();
}

// Interface B
interface B {
void methodB();
}

// Class implementing both interfaces


class MyClass implements A, B {
public void methodA() {
System.out.println("Method A from Interface A");
}

public void methodB() {


System.out.println("Method B from Interface B");
}
}

// Main class
public class MultipleDemo {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
}
}
4 Tabulation Sheet

INPUT OUTPUT
5
Method A from Interface A
Method B from Interface B

Page 29
Results: The experiment demonstrates that Java allows multiple inheritance through interfaces.
The class MyClass successfully implements both interfaces A and B without any ambiguity
or conflict.

Acropolis Institute of Technology and Research, Indore


Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Demonstration of Method Overriding in Java
Lab: Java Lab (AL-406)
using Vehicle and Bike Classes.
EVALUATION RECORD Type/ Lab Session:
Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Page 30
Grade: Cross the grade.
A B C D F

Tutor

1 Title: Demonstration of Method Overriding in Java using Vehicle and Bike Classes.
2 Neatly Drawn and labeled experimental setup:

3 Theoretical solution of the instant problem


3.1 Algorithm:
 Define a base class Vehicle with a method run().
 Define a subclass Bike that extends Vehicle.
 Override the run() method in Bike.
 In the main() method of class OverrideDemo, create an object of type Vehicle but
instantiate it with Bike.
 Call the run() method and observe that the Bike version is executed (polymorphism).

3.2 Program:
// Parent class
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
Page 31
}

// Subclass that overrides run()


class Bike extends Vehicle {
@Override
void run() {
System.out.println("Bike is running safely");
}
}

// Main class
public class OverrideDemo {
public static void main(String[] args) {
Vehicle obj = new Bike(); // Upcasting
obj.run(); // Calls the overridden method in Bike
}
}

4 Tabulation Sheet

INPUT OUTPUT
Bike is running safely
5

Results: This experiment successfully demonstrates method overriding in Java.


Even though the object is referenced by the Vehicle class, the overridden run()
method of the Bike class is executed, proving runtime polymorphism.

Page 32
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title: Demonstration of Method Overloading in Java
Lab: Java Lab (AL-406)
using Calculator Class.
EVALUATION RECORD Type/ Lab Session:
Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Page 33
Grade: Cross the grade.
A B C D F

Tutor

1 Title: Demonstration of Method Overloading in Java using Calculator Class.


2 Neatly Drawn and labeled experimental setup:

3 Theoretical solution of the instant problem


3.1 Algorithm:
 Create a class Calculator.

 Define multiple add() methods in it:

 One that takes two integers.

 One that takes two doubles.

 One that takes three integers.

 In a separate class OverloadDemo, create an object of Calculator.

 Call each version of the add() method with appropriate arguments.

 Display the results.

Page 34
3.2 Program:
// Class with overloaded add() methods
class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

// Main class
public class OverloadDemo {
public static void main(String[] args) {
Calculator calc = new Calculator();

System.out.println("Addition of 2 integers: " + calc.add(10, 20));


System.out.println("Addition of 2 doubles: " + calc.add(5.5, 6.3));
System.out.println("Addition of 3 integers: " + calc.add(1, 2, 3));
}
}
4 Tabulation Sheet

INPUT OUTPUT
(10,20) Addition of 2 integers: 30
(5.5,6.3) Addition of 2 doubles: 11.8
(1,2,3) Addition of 3 integers: 6

Page 35
5 Results: This experiment successfully demonstrates method overloading. The
Calculator class has multiple add() methods, and the correct one is chosen based
on the number and types of arguments provided at compile time.

Acropolis Institute of Technology and Research, Indore


Department of CSE (Artificial Intelligence & Machine
Learning)
Lab: Java Lab (AL-406) Group / Title: Creating and Using a Custom Exception in Java.
EVALUATION RECORD Type/ Lab Session:
Name Enrollment No. 0827AL
Performing on First submission Second submission
Extra Regular

Grade and Remarks by the Tutor


1. Clarity about the objective of experiment
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work

Additional remarks

Grade: Cross the grade.


A B C D F

Page 36
Tutor

1 Title: Creating and Using a Custom Exception in Java.


2 Neatly Drawn and labeled experimental setup:

3 Theoretical solution of the instant problem


3.1 Algorithm:
 Create a class CustomException that extends Exception.
 Define a constructor to pass custom error messages.
 Create another class ExceptionDemo.
 Inside it, create a method checkNumber(int num) that throws CustomException if the
number is negative.
 In the main() method, call checkNumber() inside a try-catch block.
 Display appropriate messages for valid and invalid input.

3.2 Program:
// Custom exception class
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}

Page 37
}

// Class to use the custom exception


public class ExceptionDemo {

// Method that throws the custom exception


static void checkNumber(int num) throws CustomException {
if (num < 0) {
throw new CustomException("Negative numbers are not allowed!");
} else {
System.out.println("Valid number: " + num);
}
}

// Main method
public static void main(String[] args) {
try {
checkNumber(10);
checkNumber(-5); // This will throw the exception
} catch (CustomException e) {
System.out.println("Caught Custom Exception: " + e.getMessage());
}
}
}
4 Tabulation Sheet

INPUT OUTPUT
Valid number: 10
5
Caught Custom Exception: Negative
numbers are not allowed!

Results: The experiment demonstrates how to define and use a custom exception

Page 38
class in Java. The program successfully throws and catches an exception based on
specific business logic (negative number check).

Page 39

You might also like