• /  73
  • 下載費用: 9.90積分  

Java語言程序設計10E LiangPPT習題案例06slide.ppt

'Java語言程序設計10E LiangPPT習題案例06slide.ppt'
*Chapter 6 Methods*Opening ProblemFind the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.*Problemint 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);*Problemint 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);*Solutionpublic 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));}*ObjectivesTo define methods with formal parameters (§6.2).To invoke methods with actual parameters (i.e., arguments) (§6.2).To define methods with a return value (§6.3).To define methods without a return value (§6.4).To pass arguments by value (§6.5).To develop reusable code that is modular, easy to read, easy to debug, and easy to maintain (§6.6).To write a method that converts hexadecimals to decimals (§6.7).To use method overloading and understand ambiguous overloading (§6.8).To determine the scope of variables (§6.9).To apply the concept of method abstraction in software development (§6.10).To design and implement methods using stepwise refinement (§6.10).*Defining MethodsA method is a collection of statements that are grouped together to perform an operation.*Defining MethodsA method is a collection of statements that are grouped together to perform an operation.*Method SignatureMethod signature is the combination of the method name and the parameter list.*Formal ParametersThe variables defined in the method header are known as formal parameters. *Actual ParametersWhen a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.*Return Value TypeA 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.*Calling MethodsTesting the max methodThis program demonstrates calling a method max to return the largest of the int valuesTestMaxAnimation*Calling Methods, cont.animation*Trace Method Invocationi is now 5animation*Trace Method Invocationj is now 2animation*Trace Method Invocationinvoke max(i, j)animation*Trace Method Invocationinvoke max(i, j)Pass the value of i to num1Pass the value of j to num2animation*。省略部分。he other operand is a number or a character. So, the preceding expression can be simplified as follows: 'a' + Math.random() * ('z' - 'a' + 1) So a random lowercase letter is(char)('a' + Math.random() * ('z' - 'a' + 1))*Case Study: Generating Random Characters, cont.To generalize the foregoing discussion, a random character between any two characters ch1 and ch2 with ch1 < ch2 can be generated as follows:(char)(ch1 + Math.random() * (ch2 – ch1 + 1)) *The RandomCharacter Class// RandomCharacter.java: Generate random characterspublic class RandomCharacter { /** Generate a random character between ch1 and ch2 */ public static char getRandomCharacter(char ch1, char ch2) { return (char)(ch1 + Math.random() * (ch2 - ch1 + 1)); }  /** Generate a random lowercase letter */ public static char getRandomLowerCaseLetter() { return getRandomCharacter('a', 'z'); }  /** Generate a random uppercase letter */ public static char getRandomUpperCaseLetter() { return getRandomCharacter('A', 'Z'); }  /** Generate a random digit character */ public static char getRandomDigitCharacter() { return getRandomCharacter('0', '9'); }  /** Generate a random character */ public static char getRandomCharacter() { return getRandomCharacter('\u0000', '\uFFFF'); }}TestRandomCharacterRandomCharacter*Stepwise Refinement (Optional)The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the “divide and conquer” strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems. *PrintCalender Case Study Let us use the PrintCalendar example to demonstrate the stepwise refinement approach. PrintCalendar*Design Diagram*Design Diagram*Design Diagram*Design Diagram*Design Diagram*Design Diagram*Design Diagram*Implementation: Top-DownA Skeleton for printCalendarTop-down approach is to implement one method in the structure chart at a time from the top to the bottom. Stubs can be used for the methods waiting to be implemented. A stub is a simple but incomplete version of a method. The use of stubs enables you to test invoking the method from a caller. Implement the main method first and then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:*Implementation: Bottom-UpBottom-up approach is to implement one method in the structure chart at a time from the bottom to the top. For each method implemented, write a test program to test it. Both top-down and bottom-up methods are fine. Both approaches implement the methods incrementally and help to isolate programming errors and makes debugging easy. Sometimes, they can be used together.*Benefits of Stepwise Refinement Simpler ProgramReusing MethodsEasier Developing, Debugging, and TestingBetter Facilitating Teamwork
關 鍵 詞:
liangppt 習題 10 10e 案例 06slide 程序設計 語言 06 java
 天天文庫所有資源均是用戶自行上傳分享,僅供網友學習交流,未經上傳用戶書面授權,請勿作他用。
關于本文
本文標題:Java語言程序設計10E LiangPPT習題案例06slide.ppt
鏈接地址: http://www.476824.live/p-51617565.html
關于我們 - 網站聲明 - 網站地圖 - 資源地圖 - 友情鏈接 - 網站客服點擊這里,給天天文庫發消息,QQ:1290478887 - 聯系我們

本站為“文檔C2C交易模式”,即用戶上傳的文檔直接賣給(下載)用戶,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有【成交的100%(原創)】。本站是網絡服務平臺方,若您的權利被侵害,侵權客服QQ:1290478887 歡迎舉報。

[email protected] 2017-2027 http://www.476824.live 網站版權所有

粵ICP備19057495號 

收起
展開
球探网即时蓝球比分 福建快三基本走势图今天 山东群英会app 聚天下配资 天津体彩十一选五走走势图 辽宁35选7综合版 排列三最近500期走势 快乐赛车计划 广东快乐10分人工计划 旅游理财骗局揭秘 广西十一选五基本走图