40
1

1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

Embed Size (px)

Citation preview

Page 1: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

1

Page 2: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

2

Reference . . .

Student stu; Reference of Student

stustu

• When the reference is created it points to a null value.

• Before access the reference, objects of the class must be declared.

NULL Value

Page 3: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

3

Constructor . . .

• A class contains a special kind of method called constructor whose name is as same as the class name and no any return type.

class Student{

Student(){

{ {

class Student{

Student(){

{{

Since This constructor does not take any parameters, it is called as the

“default constructor”

Page 4: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

4

Example 1 : (Cont) . . .class Student{

int age;char className;float average;char grade;

Student(){

{ {

class Student{

int age;char className;float average;char grade;

Student(){

{{

class Example{

public static void main(String arg[]){

;()Student stu=new Student{

{

class Example{

public static void main(String arg[]){

Student stu=new Student();{

{

Page 5: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

5

Example 2 . . .

Write a JAVA program to store the information of two students. Each student have a Age, Gender, Marks for mathematics, Marks for statistics, Marks for computer science and average of three subjects.

Attribute Student 1 Student 2

Age 21 23Gender F MMarks for Mathematics 67 89Marks for Statistics 76 54Marks for Computer Science 78 95

Page 6: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

6

Example 2 (cont) . . .

• Step 1: Declare a class to represent student information.

class Student{

int age;char gender;int marks_maths;int marks_stat;int marks_cs;float average;

{

class Student{

int age;char gender;int marks_maths;int marks_stat;int marks_cs;float average;

{

Page 7: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

7

Example 2 (cont) . . .

• Step 2: Define the driver class and the main method.

class Example2{

public static void main(){

{{

class Example2{

public static void main(){

{{

Page 8: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

8

Example 2 (cont) . . .

• Step 3: Create objects from Student class. Since there are two students, we need two objects.

class Example2{

public static void main(String arg[]){

Student stu1=new Student();Student stu2=new Student();

{{

class Example2{

public static void main(String arg[]){

Student stu1=new Student();Student stu2=new Student();

{{

Page 9: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

9

Example 2 (Cont). . .

stu1stu1 stu2stu2

RAMStudent

Methods

age: 32 bits

marks_maths: 32 bits

marks_stat: 32 bits

marks_cs: 32 bits

gender: 16 bits

average: 32 bits

Student

Methods

age: 32 bits

marks_maths: 32 bits

marks_stat: 32 bits

marks_cs: 32 bits

gender: 16 bits

average: 32 bits

Page 10: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

10

Example 2 (cont) . . .

• Step 4: Assign the values for attributes.

class Example2{

public static void main(){

Student stu1=new Student();Student stu2=new Student();

stu1.age=21;

stu2.age=23;{

{

class Example2{

public static void main(){

Student stu1=new Student();Student stu2=new Student();

stu1.age=21;

stu2.age=23;{

{

Page 11: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

11

Example 2 (Cont). . .

stu1stu1 stu2stu2

RAMStudent

Methods

age: 21

marks_maths: 67

marks_stat: 76

marks_cs: 78

gender: F

average: 0.0

Student

Methods

age: 23

marks_maths: 89

marks_stat: 54

marks_cs: 95

gender: M

average: 0.0

Page 12: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

12

Example 2 (cont) . . .

Print the information of each student.

Page 13: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

13

Example 2 (cont) . . .

class Example2{ public static void main() {

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

int tot=stu1.marks_math+stu1.marks_stat+stu1.marks_cs;stu1.average=(float)tot/3;

{{

class Example2{ public static void main() {

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

int tot=stu1.marks_math+stu1.marks_stat+stu1.marks_cs;stu1.average=(float)tot/3;

{{

Calculate the average of three subjects for each student.

Page 14: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

14

Example 2 (cont) . . .

NOTE:The attributes

age gender marks_math marks_statmarks_cs avg

are called as instance variables.

The common characteristic mentioned above take different values for each student. Hence they are declared in the class, but outside the methods.

The common characteristic mentioned above take different values for each student. Hence they are declared in the class, but outside the methods.

Page 15: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

15

Example 2 (cont) . . .

Calculate the class average of three subjects.

If the same value of an attribute is shared by all the class instances are named as class variables and are defined as ‘Static’. They are declared in the classes, but outside the methods.

If the same value of an attribute is shared by all the class instances are named as class variables and are defined as ‘Static’. They are declared in the classes, but outside the methods.

Class average for one subject does not take different values from one student to another. There is only one value for all the objects created from the same class.

Page 16: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

10/13/10 16

Example 2 (cont) . . .

Modify the Student class in order to store the class averages for three subjects.

class Student{

int age;char gender;int marks_maths, marks_stat,marks_cs;float average;

static float classAvg_maths;static float classAvg_stat;static float classAvg_cs;

{

class Student{

int age;char gender;int marks_maths, marks_stat,marks_cs;float average;

static float classAvg_maths;static float classAvg_stat;static float classAvg_cs;

{

Page 17: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

17

Example 2 (cont) . . .

class Example2{ public static void main() {

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

float avg =(float)(stu1.marks_maths+stu2.marks_maths)/2;

Student.classAvg_maths=avg; {{

class Example2{ public static void main() {

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

float avg =(float)(stu1.marks_maths+stu2.marks_maths)/2;

Student.classAvg_maths=avg; {{

Since the class variables are shared by all the instances of the class in which they are defined, they can be accessed directly through the identifier of the class.

Page 18: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

18

Summary . . .

Student 1

Methods

age: 21

marks_cs: 78

gender: F

average: 0.0

Student 2

Methods

age: 23

marks_cs: 95

gender: M

average: 0.0

Student

Methods

class_average_maths = 78

Page 19: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

03/09/10 19

Page 20: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

20

Methods . . .

• Methods are the functions that operate on instances of classes in which they are defined.

• Objects can communicate with each other using methods.

• One class may contain one or more methods.

Page 21: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

21

Method Definition . . .

Method definition has four parts.

• Name of the method

• Return type

• List of parameters

• Body of the method

Method Signature

Page 22: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

22

Method Definition (cont) . . .

return_type method_name (parameter list){

Method_body

{

return_type method_name (parameter list){

Method_body

{

Method Signature

Page 23: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

23

Naming Conventions . . .

• Method names begin with lower case letters. Do not start with numbers or symbols (@, #,%, etc).

• If the method name contains more than one word, the starting letter of the second word is capital or they are combined using the underscore (_).

• The symbols like @, #, *, %, etc are not included in the method names.

• Do not leave spaces in between the words.

• Always use the meaningful names.

Page 24: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

24

Return type . . .

• The return type is the primitive data type or object that a method returns.

• If the method does not return any value, then the return type is void.

• If the return type is not void, then the returning value should have the same type as the return type.

Page 25: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

25

Parameter List . . .

• The parameter list is a set of variable declarations.

• The parameters are separated by commas ,.

• The parameters become local variables in the body of the method whose values are passed when the method is called.

void calculate(int num, float avg){

{

void calculate(int num, float avg){

{

Page 26: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

03/09/10 26

Method body . . .

• The operations performed by a particular method, should be included in the method body.

• Method body contains,- Expressions- Methods calling statements- Control structures- Creation of objects

NOTE: If the returning type of the method is not void, then the

method should return a value.

return num;

Page 27: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

27

Method types . . .

There are two major types of methods

Instance Methods (non-static methods)

Instance Methods (non-static methods)

Class methods(Static methods)Class methods

(Static methods)

Page 28: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

28

Instance Methods . . .

•Instance methods apply and operate on the instances of the class.

• Access Modifiers can be used for instance methods.

• Instance methods can access all the instance variables and class variables declared in the same class that the method defines.

• An Instance method can declare any number of local variables which are accessible only by the method in which they defines.

Page 29: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

29

Call instance methods . . .

• Instance methods are accessed using the dot operator.

object_reference

Dot OperatorDot Operator

method_name (parameter list)

Page 30: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

30

Class/Static Methods . . .

•Class methods operate on the classes and they are defined using the keyword ‘static’.

•Access Modifiers can be used for class methods.

• class methods can access only the class variables declared in the same class that the method defines. They are not allowed to access the instance variables.

Page 31: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

03/09/10 31

Call class/static methods . . .

• Class methods are accessed using the dot operator and the name of the class which defines the method.

Class_name

Dot OperatorDot Operator

method_name (parameter list)

NOTE: main method is also a class method

Page 32: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

32

Example 3 . . .

Write a JAVA program to define a calculator. The calculator should be able to store two numbers and perform following operations on them.

1. Add 2. Subtract 3. Multiply 4. Divide

Example 4 . . .

Write a JAVA program to calculate the surface area and the volume of a cube. You must define separate operations to calculate the surface area and the volume.

Page 33: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

33

this keyword . . .

• ‘this’ keyword is used inside any instance method to refer to the current object which the current method has been called.

• ‘this’ keyword is important where a reference to an object of the current class type is required.

• Methods declared with static (class methods) can not use ‘this’ keyword

Page 34: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

34

this keyword (cont) . . .

class Student{

int tot;

void increment(){

;this.tot=100{

{

class Student{

int tot;

void increment(){

this.tot=100;{

{class Test{

public static void main(String arg[]){

;()Student st=new Student;()st.increment

{{

class Test{

public static void main(String arg[]){

Student st=new Student();st.increment();

{{

The object ‘st’ calls the instance method ‘increment()’.

Hence, ‘this’ refers to the object ‘st’.

Page 35: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

35

Method overloading . . .

• JAVA allows to have more than one method with the same name, but different method signatures. This difference occurs by means of the parameter list, but not by return type.

• The differences between the parameter lists are obtained based on the number or data types of the parameters passes to the methods .

Page 36: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

36

Example 5 . . .

class Example5{

void display(int num){

;System.out.println(num){

void display(float num){

;System.out.println(num)}

{

class Example5{

void display(int num){

System.out.println(num);{

void display(float num)}

System.out.println(num);{

{

Overloaded methods

Page 37: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

37

Example 6 . . .

Write a JAVA program to calculate the average of numbers based on following options.

Average of two integer numbersAverage of three integer numbersAverage of an integer number and a float numberAverage of two integer numbers and a float numberAverage of three float numbersAverage of two float numbers and two integer valuesAverage of two double valuesAverage of three numbers of type integer, double and float

You must use overloaded methods to perform the required operations.

Page 38: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

03/09/10 38

Constructor overloading . . .•As same as the other methods, constructors can also be overloaded, with the same name (name of the class), but different parameter lists.

• Overloaded constructors enable creation of the objects with required properties.

class Student}

int age;

Student(){

}

Student(int age){

;this.age=age}

}

class Student}

int age;

Student()}

{

Student(int age)}

this.age=age;{

{

Overloaded Constructors

Page 39: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

39

Calling overloaded Constructors . . .

class Example{

public static void main(String arg[]){

;Student st=new Student(23){

{

class Example{

public static void main(String arg[]){

Student st=new Student(23);{

{

The required parameters are passed when the constructor is called.

Page 40: 1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects

03/09/10 40

Calling another constructor . . .

A constructor can call another constructor using ‘this’ keyword.

class Student}

int age;

Student(){

;System.out.println(“Welcome to Student class”)}

Student(int age){

;()this;this.age=age

} }

class Student}

int age;

Student()}

System.out.println(“Welcome to Student class”);{

Student(int age)}

this();this.age=age;

{{

The calling statement of another constructor should be the first statement of current constructor