Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes - Intermediate

Similar presentations


Presentation on theme: "Classes - Intermediate"— Presentation transcript:

1 Classes - Intermediate
Chapter 4

2 Chapter 4 4.0 Classes – Intermediate Method overloading
Object as parameter Object as method type Array of objects Composite objects Application

3 Methods Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Solution: int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) System.out.println("Sum from 20 to 30 is " + sum); for (int i = 35; i <= 45; i++) System.out.println("Sum from 35 to 45 is " + sum);

4 Methods Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Solution: int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) System.out.println("Sum from 20 to 30 is " + sum); for (int i = 35; i <= 45; i++) System.out.println("Sum from 35 to 45 is " + sum);

5 Methods Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Solution using method: public static int sum(int i1, int i2) { int sum = 0; for (int i = i1; i <= i2; i++) sum += i; return sum; } public static void main(String[] args) { System.out.println("Sum from 1 to 10 is " + sum(1, 10)); System.out.println("Sum from 20 to 30 is " + sum(20, 30)); System.out.println("Sum from 35 to 45 is " + sum(35, 45));

6 Defining Methods A method is a collection of statements that are grouped together to perform an operation.

7 Method Signature Method signature is the combination of the method name and the parameter list.

8 Formal Parameters The variables defined in the method header are known as formal parameters.

9 Actual Parameters When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

10 Return Value Type A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void.

11 Overloaded Methods/Constructor
Multiple methods can share name as long as: They have different number of formal parameters. The formal parameters are of different data types when the number of formal parameters is the same. Formal parameter The variables defined in the method header Actual parameter A variable/expression listed in a call to a method

12 Overloaded Methods/Constructor
We called these multiple methods as overloaded methods. As long as their method signature is different, they can be overloaded. Method overloading is one of the ways that Java supports polymorphism. Java uses the type or number of arguments as its guide to determine which version of overloaded method to actually call.

13 Example 1: The following method headings are examples of overloaded methods. public void methodABC() public void methodABC(int x, double y) public void methodABC(double y, int x) public void methodABC(int x, double y, char z)

14 Example 2: In the following class, we can see that there are two(2) constructors; default and normal. As constructor is a method, it also can be overloaded. Defining multiple constructors for a class provides the client programmer flexibility in creating instances. The client programmer can choose which methods that suit her/his needs.

15 public class Pelajar{ private String name, idNo; private double mark; public Pelajar(){ name=""; idNo=null; mark=0.0; } public Pelajar(String nama, String id, double markah){ name=nama; idNo=id; mark=markah; public void setPelajar( name=JOptionPane.showInputDialog(“Name:”); id=JOptionPane.showInputDialog(“ID no:”); mark=Double.parseDouble(JOptionPane.showInputDialog(“Mark:”)); // …..other methods

16 Example 3: The same condition goes to other methods. For example the following setMark() methods. The client programmer can call/use either one. public void setMark(){ String input= JOptionPane.showInputDialog(“Mark:”); mark=Double.parseDouble(input); } public void setMark(double markah){ mark=markah;

17 Example 4: The following is an example of multiple methods with the same name. Although , the 2nd and the 3rd methods have different return type, there will be a compile-time error as they have similar data type and the number of parameter. It shows that if two(2) methods have different return type, it still can not be overloaded because the data type and parameters are the much concern.

18 public void setQuizMark(){ mark=JOptionPane
public void setQuizMark(){ mark=JOptionPane.showInputDialog(“The mark:”); } public void setQuizMark(double markah){ mark=markah; public double setQuizMark (double markah ){ return mark; Similar in number of parameter and data type

19 Example 5: RectangleChapter4 Class

20

21

22 Example 5: Which class will be run to execute the code?
What are the expected output for the code? What are the overloaded methods being used in class RectAppChapter4? How many object are declared within class RectAppChapter4?


Download ppt "Classes - Intermediate"

Similar presentations


Ads by Google