JPL Assignment-04
JPL Assignment-04
Assignment Title: A java program to demonstrate the concept of Class and Object
Aim: Write a java program to create a class Student with data name, city and age along with
method addData and printData to input and display the data, Create the two objects s1, s2 to
declare and access the values.
Pre-Requisites: C/C++
Theory:
CLASS:
OBJECTS IN JAVA
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.
An object is an instance of a class. A class is a template or blueprint from which objects are
created. So, an object is the instance(result) of a class.
Object Definitions:
● An object is a real-world entity.
● An object is a runtime entity.
● The object is an entity which has state and behavior.
● The object is an instance of a class.
METHODS IN JAVA
In Java, a method is like a function which is used to expose the behaviour of an object.
Advantage of Method
● Code Reusability
● Code Optimization
File: Student.java
//Defining a Student class.
class Student
{
//defining fields
int id; //field or data member or instance variable
String name;
public static void main(String args[]) //creating main method inside the Student class
{
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Packages in JAVA
Built-in Packages
The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Environment.
The library contains components for managing input, database programming, and much more.
The complete list can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.
The library is divided into packages and classes. Meaning you can either import a single class
(along with its methods and attributes), or a whole package that contain all the classes that
belong to the specified package.
To use a class or a package from the library, you need to use the import keyword:
Algorithm:
Step 1: Start
Step 2: Declare and Define Class Student with required variables and methods addData
and printData.
Step 6: Stop
Conclusion:
Thus we implemented a Java program that accepts student data and display with the help of
objects.
CODE:
/*Problem Statement: Write a java program to create a class student with data ‘name,
city and age’ along with method addData and printData to input and display the data.
Create the two objects s1,s2 to declare and access the values.*/
//@Author: Harsh Sonigara
//Roll No: 2213220
//Class: SY-4 Batch A
//CODE:
class Student {
private String name;
private String city;
private int age;
public void addData(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
public void printData() {
System.out.println("Name: " + name);
System.out.println("City: " + city);
System.out.println("Age: " + age);
}
}
public class Lab_4 {
public static void main(String[] args) {
Student s1 = new Student();
s1.addData("MIT", "ADT", 5);
System.out.println("Student 1 data:");
s1.printData();
Student s2 = new Student();
s2.addData("XYZ", "ABC", 19);
System.out.println("Student 2 data:");
s2.printData();
}
}
OUTPUT: