820 likes | 946 Views
COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi. Chapter 4 Loops for w hile do-while. Chapter 5 Methods Input arguments Output O verloading Code reusability Scope of variables. Methods.
E N D
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Chapter 4 • Loops • for • while • do-while
Chapter 5 • Methods • Input arguments • Output • Overloading • Code reusability • Scope of variables
Suppose we want to write a program to find the sum of integers • from 1 to 10 • from 20 to 30 • from 35 to 45
int sum = 0; for (inti = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (inti = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (inti = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); • Obvious solution
int sum = 0; for (inti= 1 ; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (inti = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for (inti = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); x y • What about some refactoring? x y x y x y x y y x
name output modifier input • A better approach is to use a method publicstaticintsum(int x, int y) { int sum = 0; for (int i = x; i <= y; i++) sum += i; return sum; } Method body
First, a method should be defined • Then we can use the method • i.e. calling or invoking a method public static void main(String[] args) { int total1 = sum(1, 10); int total2= sum(20, 30); int total3 = sum(35, 45); int total4 = sum(35,1000); }
public class TestClass{ • public static void main(String[] args) { • int total1 = sum(1, 10); • } • //---------------------------------------------- • publicstaticint sum(int x, int y) • { • int sum = 0; • for (int i = x; i <= y; i++) • sum += i; • return sum; • } • } • When calling a method within the same class, we directly call the method calling directly
public class AnotherClass{ • publicstaticint sum(int x, int y) • { • int sum = 0; • for (int i = x; i <= y; i++) • sum += i; • return sum; • } • } • When calling a method from another class, use class name if a static method • public class TestClass{ • public static void main(String[] args) { • int total1 = AnotherClass.sum(1, 10); • } • } Class name
public class AnotherClass{ • publicintsum(int x, int y) • { • int sum = 0; • for (int i = x; i <= y; i++) • sum += i; • return sum; • } • } • When calling a method from another class, use class name if a static method • public class TestClass{ • public static void main(String[] args) { • AnotherClass a = new AnotherClass(); • int total1 = a.sum(1, 10); • } • } Instance name
Method is • A collection of statements grouped together to perform an operation • To use a method • We invoke the method • E.g. int result = sum(1,10);
Method header • Method signature • Combination of the method name and the parameter list signature publicstaticintsum(int x, int y) { int sum = 0; for (int i = x; i <= y; i++) sum += i; return sum; }
Formal parameter publicstaticintsum(int x, int y) { int sum = 0; for (int i = x; i <= y; i++) sum += i; return sum; } • Parameters public static void main(String[] args) { int total1 = sum(1, 10); } Actual parameter
Formal parameters: • Variables defined in the method header • Actual parameters: • When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
If the method does not return a value, the “return type” is the keyword void. • It means “nothing” will be returned • A method may return a value: • Use return keyword to return the value… • E.g. return sum; • “return” keyword is required if return type is anything other than void
A return statement is not required for a void method. • return still can be used for terminating the method • This is not often done
publicintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; } • Use “return” only once in a method! • Easier to debug and trace public intmax(int x, inty) { if (x > y) returnx; else return y; } Two exit points One exit point: This is better
This program demonstrates calling a method max to return the largest value among a set of values. TestMax
Passing arguments public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } publicstaticint max(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
i is now 5 public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } publicstaticint max(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
jis now 2 public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
invoke method max(i,j) public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } publicstaticint max(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
pass the value of i to x pass the value of j to y public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
Declare variable result public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
(x > y) is true because (5 > 2) public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
result is now 5 public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
return result which is 5 public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
return max(i, j) and assign the return value to k public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticintmax(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
finished public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } Public staticint max(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; }
Methods reduce redundant coding and enable code reuse. • Methods modularize code and improve the quality of the program. PrimeNumberMethod
Chapter 5 • Methods • Input arguments • Output
Methods • Overload • … • Memory management
Write a method that converts a decimal integer to a hexadecimal. Decimal2HexConversion
Converting a decimal number x (e.g. 74) into a hexadecimal number (e.g. 4A) • Divide x by 16 • Save remainder and quotient • Repeat step 1 and 2 until quotient is 0 • Form the hexadecimal number from remainders (the most recent remainder will be the leftmost digit)
A data structure • Last in, first out (LIFO) • Two basic operation • Pop • Push Pop Push Z Y x
How memory is arranged • Registers • Inside the processor, very limited, you have no direct access • RAM • Stack memory • Inside RAM, very fast, lifetime of objects should be known, all primitive variables placed here • Heap memory • Inside RAM, reference values placed here • Constant values • Will be directly replaced in code
Public staticint max(intx, int y) { intresult= 0; if(x > y) result = x; else result = y; return result; } public static void main(String[] args) { inti = 5; int j = 2; int k = max(i, j); } • Each time a method is called, the system stores parameters and variables in stack memory.
How memory is managed? Space required for max method: Result: 5 x:5 y:2 Space required for max method: x:5 y:2 Space required for main method: k: i:5 j:2 Space required for main method: k: i:5 j:2 Space required for main method: k: i:5 j:2 Space required for main method: k: 5 i:5 j:2 Stack is empty
What about reference types? • E.g. Random r = new Random(); Actual Object … Space required for main method: r (reference) Heap Memory Stack Memory
What about reference types? • E.g. Random r = new Random(); Space required for test method: x Actual Object … Space required for main method: r (reference) Heap Memory Stack Memory
If primitive types are passed • Value is passed • Changes inside method will not affect the variable outside the method • If a reference type is passed • Reference is passed (address of memory location containing actual object) • Changes inside the method will affect the variable
Different versions of the same method accepting different arguments TestMethodOverloading
Method overload is only based on input arguments • Method overload can not be based on different output values • Method overload cannot be based on different modifiers
Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. • This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error.
public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { double result = 0; if (num1 > num2) result = num1; else result = num2; return result; } public static double max(double num1, int num2) { double result = 0; if (num1 > num2) result = num1; else result = num2; return result; } }