Skill Programming EXPT 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Experiment No.

:3
Aim: Write Java Program on class and object

Input Specification: roll no of type int and name of type string.

Output Specification: roll no of type int and name of type string.

Objectives:-Student/we will learn to define class, and create the object of the class.

.
Theory:
In this program we are using the concept of class and object . So we are seeing these
concepts in detail here.

Defining a Class:

A class is a user defined data type with template that serves to define its properties. Once the
class has been defined, we can create “variables” of that type using declarations that are similar
to the basic type declarations.

The basic definition is:

Class classname
{
[fields declarations;]
[method declarations;]
}
}
Remember these variables are only declared and therefore no storage space has been created in
the memory. Instance variables are also known as member variables.

Method Declaration:

The methods are declared inside the body of the class but immediately after the declaration of
instance variables. The general form of method declaration is: type method name (parameter-
list)
{

Method-body;
}
Accessing Class Members:
As we have created objects each containing its own set of variables, we should assign values to
these variables in order to use them in program.
All variables must be assigned values before they are used. We must use the concerned object
and the dot operator as shown below:

objectname.variablename = value;
objectname.methodname(parameter-list);

logic of the program.


In this program student class is created which consists of data members roll no. of type
int and name f type string.insertdata(int r,String n) is defined to initialize roll no and name . void
getdata() method is defined such that with this method we are accepting the value of roll no and
name from user. Display() method is defined to display the value of roll no and name. main
method class named studentmain is defined. In this objects s1 and s2 are created.s1.insertdata(
1,sachin) is called so s1got roll no as 1 and name as sachin. S2.getdata() is called to get the roll
no and name from user. Display() method is called to display roll no and name of s1 and s2.

Algorithm:

Step 1: Start

Step 2: Create a class student and define it.

Step 3: In class student , Declare roll no of type int and name of type string

Step 4: define methods insertdata(int r,String n) and getdata() to get roll no and name for
objects of student class.

Step 5: define method display() to display roll no and name of objects of student class.

Step 6: define main method class studentmain in which create objects s1 and s2

of class student

Step 7: call insertdata() method to get roll no and name for s1 and call getdata()

method to get roll no and name for s2.

Step 8: call display() method to display roll no and name for s1 and s2.

Step 9: stop.
Program(Code):

// Program no.3: program on class and object.

import java.io.*;

class Student
{
int rollno;
String name;

void insertdata(int r, String n)


{
rollno=r;
name=n;
}

void getdata()throws Exception


{
DataInputStream R1 =new DataInputStream(System.in);
System.out.println("Enter roll no");
rollno=Integer.parseInt(R1.readLine());
System.out.println("name");
name=R1.readLine();
}

void displayInformation()
{
System.out.println(rollno+" "+name);
}
}

class studentmain
{
public static void main(String args[])throws Exception
{
Student s1=new Student();
Student s2=new Student();
s1.insertdata(1,"Sachin");
s2.getdata();
s1.displayInformation();
s2.displayInformation();
}
}
Output:

C:\>
C:\>d:

D:\>cd programs

D:\programs>path="C:\jdk1.6.0_23\bin";

D:\programs>javac studentmain.java
Note: studentmain.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\programs>java studentmain
Enter roll no
2
name
ravi
1 Sachin
2 ravi

Outcome: with this we/student will able to use userdefined class and object in the java program.

You might also like