Presentation is loading. Please wait.

Presentation is loading. Please wait.

Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:

Similar presentations


Presentation on theme: "Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:"— Presentation transcript:

1 Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author: Your Name // Description of what the program accomplishes // Include enough to make sense. // Modification History: Created xx/xx/xxxx // //************************************************************ Every method has Method Interface documentation // Pre: list of parameter data types and what the method expects // Post: Object type ‘returned’ or void Explain as appropriate. public static int ……. This is the method heading

2 A return statement is required in all methods and is the last line before } // end of method Initialize all variable declarations. char code = ‘a’; Note: One of your major tasks as a student is to ask questions – about anything, anytime, relevant to the class. Always think concepts / plan. Details will come.

3 A java program is a class A method is a block of code that accomplishes a task and adheres to the method interface protocol All methods in camden’s class have only one return statement. This adheres to the ‘single entry – single exit’ concept. Note that java could care less if you have 1000 exit points in a method. Your job as an excellent programmer/analyst is to always maintain control. Think about the ramifications. Method interface components // Pre: …. Comment This indicates what the method requires if used (is ‘called’ or ‘invoked’). // Post: ….. Comment This indicates what the method will return when complete. Note – either nothing or 1 value Method heading public static int checkCode(char let, String name)

4 Method heading public static int checkCode(char let, String name) public or private ( in this case public) static or nothing (in this case static) void or a data/object type (in this case int) Name of method (in this case checkCode) List of parameters that the method accepts from the caller (in this case 2 values, an int and a String) To call checkCode char letter = ‘K’; String aname = “Jill Student”; int codeValue = 0; codeValue = checkCode(letter, aname); This sends control to checkCode with the specific values and checkCode returns control back with the appropriate int value.

5 What examples have we had? double anum = 3; double sroot = 0; sroot = MATH.sqrt(anum); int slength = 0; String title = “The Grapes of Wrath”; slength = title.length(); Can you write the method interface for length?

6 Volunteer to be method temp (your job, temp, is to return the temperature whenever you are called. Let’s negotiate …) Volunteer to be method temp that accepts a zip code (your job, temp(int zip) is to return the current temperature in the requested zip code) Volunteer to be method hi (your job, hi, is to say the word “hi” loudly) Volunteer to be method hi that accepts a positive integer (your job hi(int num) is to say the word “hi” loudly num times) Method interface for all.

7 Volunteer to be method getInt (your job, getInt, is to ask a user for an integer and to return that integer) Method interface: Volunteer to be method getInt that accepts a message (String) that you show to the user, get the integer and return it. Method interface:

8 String objects “The sky is falling!” “O” “ ” String characters are ‘indexed’ starting at 0 String S1 = “Some words.”; The letter S is the first character and is at index 0 The second character is 0, index 1 The third character is m, index 2 So the character at index 5 is the letter w, the 6 th character of the String char someLet = ‘ ‘; someLet = S1.charAt(5);

9 String objects So what is the 1 st character of a String S1? What is the length of a String S1? What is the last character of String S1? What happens if a string S2 is of length 8 and I ask for S2.charAt(12)? TRC

10 String objects Work a bunch of examples

11 int option --- some value – can only be integer or char or String … switch (option) { case 1: java statements break; case 2: java statements break; case 3: java statements break; case 9: java statements break; default: System.out.println("Invalid choice"); } …

12 if (speed 200) System.out.println(“Speed not valid”); Java statement - done in class Given double average = 0; int score1 = 65; int score2 = 88; int score3 = 84; write the java statement to compute the average Given String variables S1 and S2, write java statement(s) to see if they are the same. Question: are S1 and S2 primitive data types? The “equals” method. In general, in particular String equals if (S1.equals(S2)) System.out.println(“S1 and S2 are equal.”);


Download ppt "Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:"

Similar presentations


Ads by Google