Experiment. No .
02
Title: Program on classes and objects.
Criteria Excellent Good Satisfactory Needs Marks
4 3 2 Improvement Obtaine
1 d
Program Error-free, Minor errors,Code worksMajorerrors
Implementation efficient codegood logic with some or incomplete
Complete Good output, issues
Output & Basic output Incomplete
outputwith adequate
proper documentations hown or missing
Documentation
documentation output
Excellent
Viva/Questions responses to Good Satisfactory Poor or no
all questions responses to responses responses
most
questions
GitHub Properly No Proper Incomplete Not Updated
Updated organization
Total
Date:
Subject In-charge Sign:……………….
Experiment No.2
Aim: To write a Java program todemonstratetheconcept of classes and objects,
showcasing object-oriented programmingprinciples such as encapsulation and reuse
code.
Software: IDE: VS Code/Notepad
Java Version: Java jdk 21
Operating System: Windows/Linux/macOS
Pre-lab
Questions:
Q1. What is a class in Java?
Q2. What is an object?
Q3. What is the difference between a constructor and a method?
Q4. What is the use of the new keyword?
Theory:
What is a Class?
A class in Java isablueprintortemplate fromwhichobjects are created. It defines data
members (variables) and methods (functions) that represent the behavior of the
object.
Example:-
class Student
{
int rollNo;
String name;
void display() {
[Link]("Roll No: " + rollNo + ", Name: " + name);
What is an Object?
An object is an instance of a class.
It occupies memory
} and has its own identity, state, and behavior.
Example :-
S}t udent s1 = new Student(); // s1 is an object of Student class
What is a Constructor?
A constructor is a special method in Java that is used to initialize objects of a class.
It is automatically called when an object is created using the new keyword.
A constructor has the same name as the class and does not have a return type, not even v
What is the new keyword in Java?
The new keywordinJavaisusedtocreate objects dynamically during runtime. When you use
new, it:
Allocates memory in the heap for the [Link] the constructor to initialize the object.
StepsinObject-OrientedProgramming: -
1. Define the class with variables and methods.
2. Create objects using the new keyword.
3. Access members using the object ([Link] or [Link]()).
4. Initialize data using constructors or setter methods.
5. Retrieve or display data using getter methods or print statements.
Problem Statement:
To write a Java program to demonstrate the concept of classes and objects, showcasin
oriented programming principles such as encapsulation and reuse of code.
Program:
class Student {
String name;
int age;
double gpa;
Student(String name, int age, double gpa) {
[Link] = name;
[Link] = age;
[Link] = gpa;
}
void printInfo() {
[Link]("---- Student Information ----");
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("GPA: " + gpa);
[Link]("-----------------------------");
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20, 3.8);
Student s2 = new Student("Bob", 22, 3.5);
[Link]("Student 1 -> Name: " + [Link] + ", Age: " + [Link] + ", GPA: " +
[Link]);
[Link]("Student 2 -> Name: " + [Link] + ", Age: " + [Link] + ", GPA: " +
[Link]);
[Link]();
[Link]();
}
}
Output:
Student 1 -> Name: Alice, Age: 20, GPA: 3.8
Student 2 -> Name: Bob, Age: 22, GPA: 3.5
---- Student Information ----
Name: Alice
Age: 20
GPA: 3.8
-----------------------------
---- Student Information ----
Name: Bob
Age: 22
GPA: 3.5
-----------------------------
Post-lab Question: Q1. Can we have multiple classes in a single .java file?
Q2. What happens if the filename doesn't match the public class name?
Q3. What is the role of the main() method in Java?
Q4. What is the difference between Procedure Oriented Programming and Object-
Oriented Programming?
Lab Outcome:
Understandand applythefundamentalsofJavaProgrammingandObject-Oriented
Programming.
Conclusion:
The above experiment demonstrates the use of classes and objects in Java. By
defining a class Student, we encapsulate data (properties) and behavior (methods)
in
one
sharereusable structure. Objects created by this class hold unique data but
common behaviors. This reflects core principles of object-oriented programming
like modularity, reusability, and abstraction.