Recall! $! "" #
' ' )' %&! ( &% class MyClass {
$
Individual things that differentiate one object from another Determine the appearance, state or qualities of objects Represents any variables needed for class to function
' * &
' &
)'! class Car { $ String color; String model; boolean enginestate;
,' * + - %& class Car {. String color; String model; static boolean enginestate = true;
/ 0
Objects communicate to each other through method Determine what instance of the class do to change their internal state. Provide functionality provided with an entire class
$
' * &
' * & ( 12 ( 12 3 12 12
)' (4 ' void startengine() { if (enginestate == true) System.out.println("The engine is already on."); else { enginestate = true; System.out.println("The engine is now on.");
,' (4 ' 5 class Car { String Color, Model; static boolean enginestate=true; void startengine() { if (enginestate == true) 6 12 7 System.out.println("The engine is already on."); else { enginestate = true; System.out.println("The engine is now on.");
! / / / / &
/ Method s declaration provides information to compiler, runtime system, to other class and object. Carries information such as return value of a method, number and types of arguments and what other class and object can call from method. & 4' 89 :& : 1 :28 :9 ;8 :9<
/ Method is also known as function when used in other programming languages such as C, C++,C#, ASP, PHP, and so on. They are also implemented the same concept and syntax except for the modifier and throws clause. main() is also a method, but it plays the most important part in a program where it acts as the controller of a program. Creating or constructing other method(s) means that the tasks in main() method is being reduced.
/ & 4 The return type is either a valid Java type (primitive or class) or void if no value is returned. If the method declares a return type, every exit path out of the method must have a return statement. The method name must be a valid Java identifier Each parameter is separated by a comma. Also, there can be zero parameters. The method body is contained in a set of curly braces or contains a sequence of semicolon delimited Java statements that are executed sequentially or the method body can be empty.
/ '/ "!!!"$! % %" "#!"#!!!!"!
= Code conventions restrict the method names. By convention, method names should be begin with a verb in lower case, followed by capitalized verb if any. Method have unique name within its class. But, method might have same name as other class due to method overloading concept. (4 '
= & Code conventions restrict the method names. By convention, method names should be begin with a verb in lower case, followed by capitalized verb if any. Method have unique name within its class. But, method might have same name as other class due to method overloading concept. 12 (4 '
! / / / / &
(4 ' / & 12 / & %. 1 42 ;< 3 1 2 ; %<
/ &'C " (4 ' ; 1 892 ;&.. 1>=??@ A 2B &.. 1>/ =.??@ A2B &.. 1>-??> A&2B &.. 1>$??> A2B < < '(4
(4 ' / &'C " '! ; * 12 ;< 1 892 ;< <
(4 ' / &'C " )'5 4 ; * 12 / < ;&.. 1>=??@ A 2B &.. 1>/ =.??@ A2B &.. 1>-??> A&2B &.. 1>$??> A2B < 1 892 ;< 12
(4 / &'C " &6 & & ; &5 C 5 &B %CB 1 892 ; &5CDE.B %CDFB & D&5CG %CB < <
/ &'C " & ;&5 C 5 &B %CB 5 &12 ;&5CDE.B %CDFB & D&5CG %CB < 1 892 ;< (4 5 &12
(4 / &'C " ; D B DEB B 1 892 ; D G B &.. 1 2B <
/ &'C " ; D B DEB B 12 ; D G B &.. 1 2B < 1 892 ; < (4 12
(4 / &'C " ; D B DEB B 1 892 ; D G B &.. 1 2B < %.
/ &'C " ; D B DEB B (4 12 1 4 &2 ; D4G&B B < 1 892 ; &.. 1 1 22B <<
#&/ / / / /.
./ (4 No return value &* 12 No parameters Places to determine whether this method will return a value or not 5 & &
./ C " ' ' )'! & H void! 12!
./ (4 Write a method to display a student s name and matric number void displaystudentinfo() { System.out.println( Name + name); System.out.println( Matric no. + no);
./ (4 Where to put the defined method? class Student { void displaystudentinfo() { System.out.println( Name + name); System.out.println( Matric no. + no); public static void main(string[] args) {//Call method here C 77
./ (4 Write a method to calculate the product of variable A variable B. void calculate() { product = A * B;
./ (4 Where to put the defined method? class Count { int product,a = 6, B = 4; void calculate() { product = A * B; C 77 public static void main(string[] args) {//Call method here I J.
./ (4 Write a method to display a grade based on an accepted mark. void findgrade() { if ( mark >= 85 && mark <= 100) grade = A ;
./ (4 class StudentMark { int mark; String grade; Where to put the defined method? void findgrade() { if (mark >= 85 && mark <=100) grade = A ; C 77 public static void main(string[] args) {//Call method here I %.
./ (4 int return value 1 2 This method will return an integer value Can be either int, float, double and char (other than void). Pass by value/ Has parameter &
./ This method is also known as method with parameters and a return value. Pass by value happen when we invoke a method with parameters A copy of the value of the actual parameter is passed to the method. The actual variable outside the method are not affected. Contains return keyword at the end of method.
./ (4 class Count { int add(int a, int b) { int addproduct; addproduct = a * b; return addproduct; K & public static void main(string[] args) { int num1 =4, num2 = 7; I main().
./ (4 class Count { int num1 =4, num2 = 7; int add(int a, int b) { int addproduct; addproduct = a * b; return addproduct; I. K & public static void main(string[] args) {
./ (4 class StudentMark { int mark; char grade; I %. int findgrade(int m) { if (mark >= 85 && mark <=100) grade = A ; K return grade; & public static void main(string[] args) {//Call method here
)./ (4 int return value 12 This method will return an integer value Can be either int, float, double and char (other than void). No parameter = &
)./ (4 class StudentMark { int mark = 90; char grade; I %. = int findgrade() { if (mark >= 85 && mark <=100) grade = A ; K return grade; & public static void main(string[] args) {//Call method here
)./ (4 I. class Calculate { double no1 = 8,no2 =9; = double addvalue() {return no1 + no2; K & public static void main(string[] args) {//Call method here
)./ (4 class Count { int number[],odd = 0; int oddnumber() { for (int i=0;i<5;i++) { if (number[i]% 2 ==1) odd++; return odd; I. = K & public static void main(string[] args) {//Call method here
,./ (4 Two integer parameters No return value 1 4 &2 This method will not return any value # &
,./ (4 I. class Calculate { double no1 = 8,no2 =9, total; # void addvalue( double x, double y) {total= x + y; public static void main(string[] args) {//Call method here
,./ (4 class Count { int number[],odd = 0; void oddnumber(int n[]) { for (int i=0;i<5;i++) { if (n[i]% 2 ==0) odd++; I. L = public static void main(string[] args) {//Call method here
%& & 12. %& & %.! &.! &.