Classes - Intermediate
description
Transcript of Classes - Intermediate
Classes - IntermediateChapter 4
Chapter 4
4.0 Classes – Intermediate Method overloading Object as parameter Object as method type Array of objects Composite objects Application
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++) sum += i;System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;for (int i = 35; i <= 45; i++) sum += i;System.out.println("Sum from 35 to 45 is " + sum);
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++) sum += i;System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;for (int i = 35; i <= 45; i++) sum += i;System.out.println("Sum from 35 to 45 is " + sum);
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));}
Defining Methods
A method is a collection of statements that are grouped together to perform an operation.
Method Signature
Method signature is the combination of the method name and the parameter list.
Formal Parameters
The variables defined in the method header are known as formal parameters.
Actual Parameters
When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
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.
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.
• The variables defined in the method header
Formal parameter
• A variable/expression listed in a call to a method
Actual parameter
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.
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)
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.
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}
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;
}
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.
public void setQuizMark(){mark=JOptionPane.showInputDialog(“The mark:”);
}
public void setQuizMark(double markah){mark=markah;
}
public double setQuizMark (double markah ){mark=markah;return mark;
}
Similar in number of parameter and data type
Example 5:
RectangleChapter4 Class
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?