You are on page 1of 11

JAVA SE

(CORE JAVA)
LECTURE-14
Today’s Agenda

• Creating methods in class.

• Initializing data members of the class.

• Creating parameterized methods of a class.


Creating methods in a class

• The major principle of Object oriented programming i.e.


Encapsulation is implemented using methods in a class.

• In Java we just define a method in class, no declaration is


required.

• Let us understand this from our previous example.


class Student }
{ }
private int roll; class UseStudent
private char grade; {
private float per; public static void main(String [ ]
public void setData( ) args)
{ {
roll=10; Student s=new Student( );
grade=‘A’l s.setData();
per=66.5f; s.showData();
} }
public void showData( ) }
{
S.O.P(“Roll, grade and percentage is
”+roll,grade,per);
Initializing Data Members
at Runtime

import java.util.*;
class Student
{
private int roll;
private char grade;
private float per;
public void setData()
{
Scanner kb= new Scanner(System.in);
System.out.println(“Enter roll, grade and percentage”);
roll=kb.nextInt();
grade=kb.next();
per=kb.nextFloat();
}
Initializing Data Members
at Runtime

public void showData( )


{
S.O.P(“Roll is ”+roll);
S.O.P(“Grade is ”+grade);
S.O.P(“Percentage is ”+per);
}
}
class UseStudent
{
public static void main(String [ ] args)
{
Student s=new Student( );
s.setData();
s.showData( );
}
}
Creating Parameterized
Methods

class Student
{
private int roll;
private char grade;
private float per;
public void setData(int r, char g, float p)
{
roll=r;
grade=g;
float=p;
}
public void showData( )
{
S.O.P(“Roll is ”+roll +“\nGrade is ”+grade+“\nPercentage is ”+per);
}
}
Creating Parameterized
Methods

class UseStudent
{
public static void main(String [ ] args)
{
Scanner kb=new Scanner(System.in);
Student s=new Student( );
S.O.P(“Enter roll, grade and percentage ”);
int roll=kb.nextInt( );
char grade=kb.next( );
float per=kb.nextFloat( );
s.setData(roll,grade,per);
s.showData( );
}
}
Exercise

• Write an object oriented program to create an entity class


called Circle, with an integer data member radius.
Provide following methods in your class
1. setRadius( ), which should accept an integer as
argument and should initialize radius with it.
2. calculateArea( ), which should calculate and display
area of the circle.
3. calculateCircumference( ), which should calculate
and display circumference of the circle.
• Now design a driver class called UseCircle, which should
accept radius from user, create and initialize Circle object
and should display its area and circumference.
Exercise

• Write an object oriented program to create an entity class called


Worker, with 2 data members called hw and payRate of type
int and double respectively. Provide following methods in your
class
1. setData( ), which should accept 2 arguments and initial;ize hw
and payRate with them
2. getSalary( ), which should calculate and return salary of the
worker. The rule for calculating salary is that upto 40hrs the
salary is hw*payRate and for hours above than 40 the
payRate is doubled.
• Now design a driver class called UseWorker, which should
accept hours worked and payRate from user, create and
initialize Worker object and should display the salary of the
Worker
End Of Lecture 14

For any queries mail us @: scalive4u@gmail.com


Call us @ : 0755-4271659, 7879165533

Agenda for Next Lecture:


1. Initializing Objects or Data member
2. Explicit Initialization.
3. Using initializer
4. Using constructor.

You might also like