1 Introduction to Methods Method Calls Parameters Return

  • Slides: 12
Download presentation
 1

1

Introduction to Methods Ø Method Calls Ø Parameters Ø Return Type Ø Method Overloading

Introduction to Methods Ø Method Calls Ø Parameters Ø Return Type Ø Method Overloading Ø Accessor & Mutator Methods Ø Student Class: Revisited 2

Method Calls Ø The set of methods defined for an object determines the behavior

Method Calls Ø The set of methods defined for an object determines the behavior of that object. Ø For Example, for an object of Student class; methods: get. ID, set. GPA, and get. Name will make the behavior of this object known to us. Ø After creating an object from a class, a series of methods are called to accomplish some tasks. The following code creates a Student object, then it increments the GPA of this student by 0. 1: Student s = new Student(243, “Husam” , 3. 1); double gpa = s. get. GPA(); s. set. GPA(gpa + 0. 1) ; Ø 3

Methods: Syntax & Naming Ø Syntax modifier return. Type method. Name(para. Type p 1,

Methods: Syntax & Naming Ø Syntax modifier return. Type method. Name(para. Type p 1, . . ){ return expression; statements. . . ; } Ø Example public double calculate. Average(int n 1, int n 2){ double average = (n 1+ n 2) / 2. 0; return average; } } 4

Parameters //main method Student adel = new Student(987623, " Adel", 1. 9); double increment

Parameters //main method Student adel = new Student(987623, " Adel", 1. 9); double increment = 0. 1; adel. set. GPA(adel. qet. GPA()+ increment); Ø Actual parameter: Constant, variable or expression in method call. Ø Example: 2. 0 and adel (implicit actual parameter). //method in Student Class public void set. GPA(double gpa){ this. gpa = gpa; } Ø Formal parameter: Variable in the method definition Ø Example: grade and this (implicit formal parameter). 5

Self-check Exercise What is the difference between actual parameters and formal parameters? Ø Ø

Self-check Exercise What is the difference between actual parameters and formal parameters? Ø Ø What are the problems in the following two program segments of method headers and method calls? // method call double a = 1. 5; int b = 4; object. Reference. method. A(a, b); // method header public void method. A(int n, double x) {. . . } // method call double a = 1. 5; int b = 4, c = 2; object. Reference. method. B(a, b, c); // method header public void method. B(double x, int n) {. . . } Ø Can we pass an Object reference as an actual parameter? 6

Return Type The return type indicates the type of value the method returns –

Return Type The return type indicates the type of value the method returns – a primitive type or object Ø reference. Ø If no value or reference is returned by a method, the return type is specified as void. public double get. GPA(){ return gpa; } public void set. GPA(double new. GPA){ gpa = new. GPA; } Ø A Java method cannot return more than one value. 7

Method Overloading Ø Methods can be overloaded. Ø Overloaded methods: Ø Two or more

Method Overloading Ø Methods can be overloaded. Ø Overloaded methods: Ø Two or more methods of the same class with the same name but different signatures. Ø The return type of a method is not counted as part of its signature. Ø Formal parameters of overloaded constructors and methods must NOT have the same type, order, and count. Ø Example: Valid overloading: public double compute(int num, int num 2){. . . } public double compute(int num, double num 2){. . . } public double compute(int num) {. . . } Ø Example: Invalid overloading: public double compute(int num) {. . . } public int compute(int num) {. . . } 8

Accessor Methods Ø Ø Ø Encapsulation: Data + Methods to operate on the data.

Accessor Methods Ø Ø Ø Encapsulation: Data + Methods to operate on the data. Only methods of the same class can directly access its private instance variables. A public method that returns the private value of an instance variable of an object is called an accessor method. public class Student{ private int id; private String name; private double; public String get. Name(){ return name; } public int get. ID(){ return id; } public double get. GPA(){ return gpa; } } 9

Mutator Methods Ø A method that changes the value of some instance variable is

Mutator Methods Ø A method that changes the value of some instance variable is called a mutator method. public class Student{ private int id; private String name; private double gpa; //. . . public void set. GPA(double new. GPA){ gpa = new. GPA; } //. . . } Ø Classes that do not have any mutator methods are called immutable classes. (Example: String class). 10

Student class ublic class Student{ p private int id; private String name; private double

Student class ublic class Student{ p private int id; private String name; private double gpa; public Student(int id String name, double gpa){ this. id = id; this. name = name; this. gpa = gpa; } public Student(int id, String name){ this(id, name, 0. 0); } public String get. Name(){ return name; } public int get. ID(){ return id; } public double get. GPA(){ return gpa; } public void set. GPA(double new. GPA){ gpa = new. GPA; } } 11

Exercises Ø Write a test program for the Student class. Create two students. Test

Exercises Ø Write a test program for the Student class. Create two students. Test all accessor and mutator methods. Ø Add another constructor to the Student class such that it will take only the name as argument. The ID will be set to 000 and GPA to 0. 0 Ø Add a method called evaluate(gpa). This method will return strings "honor". "good standing" or "under probation" according to the value of gpa. 12