Experiment no.
3
Name of experiment: Make a Java Program for display two students’ info by creating and object and access
their class members.
Objective: To understand the concepts of object creation, class members, and member access in Java.
Problem analysis: The problem analysis of this program is to defines two student class with two attributes:
name and age. It includes a constructor for initializing the objects and a method displayInfo() to display their
details.
Algorithm:
1. Start
2. Define a class named 'Student' with two attributes: name and age
3. Define a constructor for the 'Student' class that initializes the 'name' and 'age' attributes.
4. Define a method 'displayInfo()' within the 'Car' class that prints the 'brand' and 'year' attributes.
5. In the 'Main' class, define the 'main' method.
6. Inside the 'main' method, create an instance 'student1' of the 'Student' class, passing 'Joy' as the name
and ‘21' as the age to the constructor.
7. Call the 'displayInfo()' method using the ' student1' instance.
8. End.
Source code:
public class Student
String name;
int age;
public Student(String name, int age)
[Link] = name;
[Link] = age;
public void displayInfo()
[Link]("Name: " + name);
[Link]("Age: " + age);
public class Main
{
public static void main(String[] args)
Student student1 = new Student("Joy", 21);
Student student2 = new Student("Roy", 20);
[Link]("Student 1:");
[Link]();
[Link]("\nStudent 2:");
[Link]();
Output:
Discussion: The Java program successfully demonstrates the process of creating an object, initializing its
members, and accessing these members through a method within the class. This exercise reinforces the
understanding of basic object-oriented programming concepts in Java. In this program we use two attributes
name and age. We includes a constructor for initializing the objects and a method displayInfo() to display their
details.