0% found this document useful (0 votes)
101 views

JPL Assignment-04

1) The document describes a Java program assignment to create a Student class with name, city, and age attributes and addData and printData methods to input and display student data. 2) The main method creates two Student objects s1 and s2, calls the addData method to input data for each, and calls printData to display the data. 3) The program demonstrates OOP concepts like classes, objects, methods, and how to define and access class attributes and methods through object instances.

Uploaded by

Arman Chhag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

JPL Assignment-04

1) The document describes a Java program assignment to create a Student class with name, city, and age attributes and addData and printData methods to input and display student data. 2) The main method creates two Student objects s1 and s2, calls the addData method to input data for each, and calls printData to display the data. 3) The program demonstrates OOP concepts like classes, objects, methods, and how to define and access class attributes and methods through object instances.

Uploaded by

Arman Chhag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment-04

Name: Harsh Sonigara


Roll No: 2213220
Class: SY-4 Batch A
Subject: JPL

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++

Objective: Objective: The objective is to impart fundamentals of object-oriented


programming in Java, including defining classes, invoking methods, using class libraries, etc.

Outcome: Outcomes: After learning these concept students will be able to


1.Analyze the necessity for Object Oriented Programming paradigm over structured programming
and become familiar with the fundamental concepts in OOP like encapsulation, Inheritance and
Polymorphism
2.Design and develop java programs with methods and access the methods by using instances
of class ie: objects
3.Understand the concept of Packages.

Theory:

CLASS:

Java is an object-oriented programming language. Everything in Java is associated with classes


and objects, along with its attributes and methods, For example: in real life, a car is an object,
The car has attributes, such as weight and colour, and methods, such as drive and brake, ‘A class
is a group of objects which have common properties. It is a template or blueprint from which
objects are created, Itis a logical entity. It can't be physical.

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 has three characteristics:


● State: represents the data (value) of an object.
● Behavior: represents the behavior (functionality) of an object such as deposit, withdraw,
etc.
● Identity: An object identity is typically implemented via a unique ID. The value of the
ID is not visible to the external user. However, it is used internally by the JVM to identify
each object uniquely.

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

OBJECT AND CLASS EXAMPLE: main Within the Class


In this example, we have created a Student class which has two data members id and name. We
are creating the object of the Student class by new keyword and printing the object's value.

Here, we are creating a main() method inside the class.

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

A package in Java is used to group related classes. Think of it as a folder in a file


directory. We use packages to avoid name conflicts, and to write a better maintainable code.
Packages are divided into two categories:
● Built-in Packages (packages from the Java API)
● User-defined Packages (create your own packages)

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:

import package.name.Class; // Import a single class


import package.name.*; // Import the whole package
Pre-defined packages

1. java.applet 2. java.awt 3. java.beans

4. java.io 5. java.lang 6. java.lang.ref

7. java.math 8. java.net 9. java.nio

11. java.text 12. java.util


10. java.sql

13. java.util.zip 14. javax.sql 15. javax.swing

Algorithm:

Step 1: Start

Step 2: Declare and Define Class Student with required variables and methods addData
and printData.

Step 3: Define main method

Step 4: Create objects of class Student S1, S2

Step 5: Access the methods with S1, S2.

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:

You might also like