SlideShare ist ein Scribd-Unternehmen logo
1 von 70
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods
Chapter Objectives ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Chapter Objectives (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Predefined Classes ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
class  Character   (Package:  java.lang ) Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e class  Character   (Package:  java.lang ) (continued)
Java Programming: From Problem Analysis to Program Design, 4e class  Character   (Package:  java.lang ) (continued)
Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 4e
User-Defined Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Syntax ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Syntax (continued) ,[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e ,[object Object],[object Object],[object Object]
Equivalent Method Definitions Java Programming: From Problem Analysis to Program Design, 4e public static double  larger( double  x,  double  y) { double   max; if   (x >= y) max = x; else max = y; return   max; }
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e
Equivalent Method Definitions (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Equivalent Method Definitions (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e The  int  variable  num  contains the desired sum to be rolled
Palindrome Number ,[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Solution:  isPalindrome  Method Java Programming: From Problem Analysis to Program Design, 4e public static boolean  isPalindrome(String str) { int  len = str.length();  int  i, j; j = len - 1;  for  (i = 0; i <= (len - 1) / 2; i++) { if  (str.charAt(i) !=  str.charAt(j)) return false ; j--;  } return true ;  }
Flow of Execution ,[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Programming Example: Largest Number ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 4e static  Scanner console =  new  Scanner(System.in); public static void  main(String[] args) { double  num; double  max;  int  count; System.out.println(&quot;Enter 10 numbers.&quot;); num = console.nextDouble();  max = num;  for  (count = 1; count < 10; count++)  { num = console.nextDouble();  max = larger(max, num);  } System.out.println(&quot;The largest number is &quot; + max);  }
Sample Run: Largest Number Java Programming: From Problem Analysis to Program Design, 4e ,[object Object],[object Object],[object Object],[object Object]
Void Methods ,[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 4e
Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 4e
Primitive Data Type Variables as Parameters ,[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters   ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Uses of  Reference Variables as Parameters   ,[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters: type  String Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e String str = &quot;Hello&quot;;  //Line 5
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e stringParameter(str);  //Line 7
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e pStr = &quot;Sunny Day&quot;;  //Line 14
Reference Variables as Parameters: type  String  (continued) Java Programming: From Problem Analysis to Program Design, 4e Variables before the statement in Line 8 executes
Java Programming: From Problem Analysis to Program Design, 4e ,[object Object],[object Object]
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java Programming: From Problem Analysis to Program Design, 4e ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scope of an Identifier within a Class ,[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Scope of an Identifier within a Class (continued) ,[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Scope of an Identifier within a Class (continued) ,[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Scope of an Identifier within a Class (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Scope Rules ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Scope Rules (continued) ,[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 4e
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Scope Rules (continued) Java Programming: From Problem Analysis to Program Design, 4e
Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 4e
Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading:  An Introduction ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading (continued) ,[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Method Overloading (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Programming Example: Data Comparison ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Programming Example: Data Comparison (continued) Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: From Problem Analysis to Program Design, 4e Programming Example: Data Comparison (continued)
Chapter Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e
Chapter Summary (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Java Programming: From Problem Analysis to Program Design, 4e

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (16)

9781111530532 ppt ch12
9781111530532 ppt ch129781111530532 ppt ch12
9781111530532 ppt ch12
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
9781111530532 ppt ch11
9781111530532 ppt ch119781111530532 ppt ch11
9781111530532 ppt ch11
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
9781111530532 ppt ch01
9781111530532 ppt ch019781111530532 ppt ch01
9781111530532 ppt ch01
 
Ppt chapter06
Ppt chapter06Ppt chapter06
Ppt chapter06
 
Ppt chapter05
Ppt chapter05Ppt chapter05
Ppt chapter05
 
Chap04
Chap04Chap04
Chap04
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Ppt chapter10
Ppt chapter10Ppt chapter10
Ppt chapter10
 
Ppt chapter11
Ppt chapter11Ppt chapter11
Ppt chapter11
 
Class method
Class methodClass method
Class method
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 

Andere mochten auch

Isolation Andrew Flynn 04.11.14 (1)
Isolation Andrew Flynn 04.11.14 (1)Isolation Andrew Flynn 04.11.14 (1)
Isolation Andrew Flynn 04.11.14 (1)Andrew Flynn
 
Andrew_Flynn_Task_A (1)
Andrew_Flynn_Task_A (1)Andrew_Flynn_Task_A (1)
Andrew_Flynn_Task_A (1)Andrew Flynn
 
303 Task B 24.09.14
303 Task B 24.09.14303 Task B 24.09.14
303 Task B 24.09.14Andrew Flynn
 
Math 302 midterm exam
Math 302 midterm examMath 302 midterm exam
Math 302 midterm examDafnaHabrew
 
305 task B electronic
305 task B  electronic305 task B  electronic
305 task B electronicAndrew Flynn
 
Format Of All Statutory Registers Under Companies Act
Format Of All Statutory Registers Under Companies ActFormat Of All Statutory Registers Under Companies Act
Format Of All Statutory Registers Under Companies ActPraveen Kumar
 
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test)
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test) K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test)
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test) LiGhT ArOhL
 
Beyond the Gig Economy
Beyond the Gig EconomyBeyond the Gig Economy
Beyond the Gig EconomyJon Lieber
 

Andere mochten auch (8)

Isolation Andrew Flynn 04.11.14 (1)
Isolation Andrew Flynn 04.11.14 (1)Isolation Andrew Flynn 04.11.14 (1)
Isolation Andrew Flynn 04.11.14 (1)
 
Andrew_Flynn_Task_A (1)
Andrew_Flynn_Task_A (1)Andrew_Flynn_Task_A (1)
Andrew_Flynn_Task_A (1)
 
303 Task B 24.09.14
303 Task B 24.09.14303 Task B 24.09.14
303 Task B 24.09.14
 
Math 302 midterm exam
Math 302 midterm examMath 302 midterm exam
Math 302 midterm exam
 
305 task B electronic
305 task B  electronic305 task B  electronic
305 task B electronic
 
Format Of All Statutory Registers Under Companies Act
Format Of All Statutory Registers Under Companies ActFormat Of All Statutory Registers Under Companies Act
Format Of All Statutory Registers Under Companies Act
 
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test)
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test) K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test)
K to 12 Grade 3 MATHEMATICS NAT (National Achievement Test)
 
Beyond the Gig Economy
Beyond the Gig EconomyBeyond the Gig Economy
Beyond the Gig Economy
 

Ähnlich wie Java Methods Chapter Overview

9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
9781439035665 ppt ch03
9781439035665 ppt ch039781439035665 ppt ch03
9781439035665 ppt ch03Terry Yoast
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02Terry Yoast
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
9781439035665 ppt ch07_passing_primitivetypeasobjects
9781439035665 ppt ch07_passing_primitivetypeasobjects9781439035665 ppt ch07_passing_primitivetypeasobjects
9781439035665 ppt ch07_passing_primitivetypeasobjectsTerry Yoast
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06Terry Yoast
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05Terry Yoast
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamedMd Showrov Ahmed
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06Terry Yoast
 

Ähnlich wie Java Methods Chapter Overview (20)

9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
9781439035665 ppt ch03
9781439035665 ppt ch039781439035665 ppt ch03
9781439035665 ppt ch03
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Chap07
Chap07Chap07
Chap07
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chap03
Chap03Chap03
Chap03
 
Chap03
Chap03Chap03
Chap03
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chap02
Chap02Chap02
Chap02
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
9781439035665 ppt ch07_passing_primitivetypeasobjects
9781439035665 ppt ch07_passing_primitivetypeasobjects9781439035665 ppt ch07_passing_primitivetypeasobjects
9781439035665 ppt ch07_passing_primitivetypeasobjects
 
9781439035665 ppt ch06
9781439035665 ppt ch069781439035665 ppt ch06
9781439035665 ppt ch06
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 
Chap14
Chap14Chap14
Chap14
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamed
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mehr von Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Java Methods Chapter Overview

  • 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods
  • 2.
  • 3.
  • 4.
  • 5. Predefined Classes (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 6. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  • 7. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  • 8. Java Programming: From Problem Analysis to Program Design, 4e Predefined Classes (continued)
  • 9. class Character (Package: java.lang ) Java Programming: From Problem Analysis to Program Design, 4e
  • 10. Java Programming: From Problem Analysis to Program Design, 4e class Character (Package: java.lang ) (continued)
  • 11. Java Programming: From Problem Analysis to Program Design, 4e class Character (Package: java.lang ) (continued)
  • 12. Syntax: Value-Returning Method Java Programming: From Problem Analysis to Program Design, 4e
  • 13.
  • 14.
  • 15.
  • 16. Equivalent Method Definitions Java Programming: From Problem Analysis to Program Design, 4e public static double larger( double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  • 17. Java Programming: From Problem Analysis to Program Design, 4e
  • 18. Java Programming: From Problem Analysis to Program Design, 4e
  • 19.
  • 20.
  • 21. Java Programming: From Problem Analysis to Program Design, 4e The int variable num contains the desired sum to be rolled
  • 22.
  • 23. Solution: isPalindrome Method Java Programming: From Problem Analysis to Program Design, 4e public static boolean isPalindrome(String str) { int len = str.length(); int i, j; j = len - 1; for (i = 0; i <= (len - 1) / 2; i++) { if (str.charAt(i) != str.charAt(j)) return false ; j--; } return true ; }
  • 24.
  • 25.
  • 26. Solution: Largest Number Java Programming: From Problem Analysis to Program Design, 4e static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num; double max; int count; System.out.println(&quot;Enter 10 numbers.&quot;); num = console.nextDouble(); max = num; for (count = 1; count < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println(&quot;The largest number is &quot; + max); }
  • 27.
  • 28.
  • 29. Void Methods with Parameters: Syntax Java Programming: From Problem Analysis to Program Design, 4e
  • 30. Void Methods with Parameters: Syntax (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 31.
  • 32.
  • 33.
  • 34. Reference Variables as Parameters: type String Java Programming: From Problem Analysis to Program Design, 4e
  • 35. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 36. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 37. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 38. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e String str = &quot;Hello&quot;; //Line 5
  • 39. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e stringParameter(str); //Line 7
  • 40. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e pStr = &quot;Sunny Day&quot;; //Line 14
  • 41. Reference Variables as Parameters: type String (continued) Java Programming: From Problem Analysis to Program Design, 4e Variables before the statement in Line 8 executes
  • 42.
  • 43. Java Programming: From Problem Analysis to Program Design, 4e
  • 44. Java Programming: From Problem Analysis to Program Design, 4e
  • 45. Java Programming: From Problem Analysis to Program Design, 4e
  • 46. Java Programming: From Problem Analysis to Program Design, 4e
  • 47. Java Programming: From Problem Analysis to Program Design, 4e
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58. Scope Rules: Demonstrated Java Programming: From Problem Analysis to Program Design, 4e
  • 59. Scope Rules: Demonstrated (continued) Java Programming: From Problem Analysis to Program Design, 4e
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68. Java Programming: From Problem Analysis to Program Design, 4e Programming Example: Data Comparison (continued)
  • 69.
  • 70.