html5-img
1 / 82

COP3502 Programming Fundamentals for CIS Majors 1

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.

meda
Download Presentation

COP3502 Programming Fundamentals for CIS Majors 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

  2. Chapter 4 • Loops • for • while • do-while

  3. Chapter 5 • Methods • Input arguments • Output • Overloading • Code reusability • Scope of variables

  4. Methods

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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); }

  10. 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

  11. 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

  12. 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

  13. 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);

  14. 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; }

  15. 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

  16. 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.

  17. 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

  18. A return statement is not required for a void method. • return still can be used for terminating the method • This is not often done

  19. 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

  20. This program demonstrates calling a method max to return the largest value among a set of values. TestMax

  21. 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; }

  22. 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; }

  23. 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; }

  24. 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; }

  25. 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; }

  26. 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; }

  27. (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; }

  28. 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; }

  29. 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; }

  30. 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; }

  31. 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; }

  32. Methods reduce redundant coding and enable code reuse. • Methods modularize code and improve the quality of the program. PrimeNumberMethod

  33. Chapter 5 • Methods • Input arguments • Output

  34. Methods • Overload • … • Memory management

  35. Write a method that converts a decimal integer to a hexadecimal. Decimal2HexConversion

  36. 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)

  37. Memory & Methods

  38. A data structure • Last in, first out (LIFO) • Two basic operation • Pop • Push Pop Push Z Y x

  39. 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

  40. 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.

  41. 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

  42. What about reference types? • E.g. Random r = new Random(); Actual Object … Space required for main method: r (reference) Heap Memory Stack Memory

  43. 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

  44. 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

  45. Method Overload

  46. Different versions of the same method accepting different arguments TestMethodOverloading

  47. 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

  48. 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.

  49. 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; } }

  50. Variable Scope

More Related