CS 1101 Programming Methodology http www comp nus

  • Slides: 31
Download presentation
CS 1101: Programming Methodology http: //www. comp. nus. edu. sg/~cs 1101/

CS 1101: Programming Methodology http: //www. comp. nus. edu. sg/~cs 1101/

Week 6: Writing Modular Programs & Testing and Debugging § Last week: § Chapter

Week 6: Writing Modular Programs & Testing and Debugging § Last week: § Chapter 5: Selection Statements (cont’d) § Chapter 6: Repetition Statements § This week: § Chapter 6: Repetition Statements (cont’d) § Writing Modular Programs § Testing and Debugging § Next week: § Recess! (yeah!) § The week after next: § Chapter 7: Defining Your Own Class – Part 2 © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 2

Last Week’s Exercise #4: Prime-Number n n n Your DL should have discussed this

Last Week’s Exercise #4: Prime-Number n n n Your DL should have discussed this in last week’s discussion session. Write a program Prime. Test. java to check if a positive integer is a prime. Sample runs: Enter a positive integer: 131 is a prime. Enter a positive integer: 713 is not a prime. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 3

Modular Programming (1/6) n n Download Prime. Test. Non. Modular. java and Prime. Test.

Modular Programming (1/6) n n Download Prime. Test. Non. Modular. java and Prime. Test. java. Study and discuss them. Modular programming q q q n Goes hand-in-hand with stepwise refinement and incremental development Makes the code easier to develop, test and debug Promotes reusability of codes In general a problem is solved in 3 steps: input computation output. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 4

Modular Programming (2/6) n n n It is customary to write a separate module

Modular Programming (2/6) n n n It is customary to write a separate module to perform the computation step. If the computation is complex, it should be further split into smaller steps and each step performed by a module. A ‘module’ (in Java, it is a method) q q Should be well-defined Should do one task © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 5

Modular Programming (3/6) n A well-defined module q q q n Has a good

Modular Programming (3/6) n A well-defined module q q q n Has a good name (for readability and documentation) Has a clear interface (what parameters does it take? ) May pass back to its caller no result or a single result (what value does it return? ) It’s nothing new! You have seen them before. q Example: set. Colour(String c) in the Ball class public void set. Colour(String c) { colour = c; } q Takes in String c. Does not return any value (void). Example: get. Colour() in the Ball class public String get. Colour() { return colour; } © CS 1101 (AY 2009 -2010 Semester 1) Takes in no parameter. Returns a String. Week 6 - 6

Modular Programming (4/6) n Advantages of modular programming: q Easy to replace v q

Modular Programming (4/6) n Advantages of modular programming: q Easy to replace v q E. g. : When you discover a better algorithm for is. Prime(int), you just replace that method without affecting any other parts of the program or other programs. Easy to reuse v v E. g. : Suppose you want to write a program to count the number of prime numbers between two integers a and b. Compare how you would need to modify Prime. Test. Non. Modular. java and Prime. Test. java to do the above. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 7

Modular Programming (5/6) n Reusability of code q q q If is. Prime(int) is

Modular Programming (5/6) n Reusability of code q q q If is. Prime(int) is a very commonly used method, we could even go a step further… See Prime. java where we define the Prime class in which it contains a class method is. Prime(int). See Count. Primes. java which is an application program that makes use of the Prime class. v It is so short and sweet! Any other application that requires the is. Prime(int) method can use the method in a similar fashion. As the creator of Prime. java, you can hide the source code and provide only Prime. class. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 8

Modular Programming (6/6) n A method should not mix computation with input/output. (Except for

Modular Programming (6/6) n A method should not mix computation with input/output. (Except for very special situation. ) q Example: The following is not desirable. public static boolean is. Prime(int num) { boolean is. Prime = false; if (num > 1) { is. Prime = true; for (int i = (int) Math. sqrt(num); i>1 && is. Prime; i--) if (num % i == 0) is. Prime = false; } if (is. Prime) System. out. println(num + " is prime. "); else System. out. println(num + " is not prime. "); return is. Prime; } © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 9

Last Week’s Exercise #2: Asterisks. V 1. java n n n Complete Asterisks. V

Last Week’s Exercise #2: Asterisks. V 1. java n n n Complete Asterisks. V 1. java to read in an integer n and print n asterisks on a single line. (If n is non-positive, then no asterisk will appear. ) Sample runs: Enter n: 7 ******* Done! Enter n: -2 Done! n Download Asterisks. V 1 Completed. java and study it. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 10

Asterisks. V 2. java n Now, write Asterisks. V 2. java to read in

Asterisks. V 2. java n Now, write Asterisks. V 2. java to read in a positive integer n and display n rows of asterisks in the following fashion. q n n First row has 1 asterisk, second row 3 asterisks, third row 5 asterisks, etc. Your program should be modular. There should be a method print. Stars(int k) to print a row of k asterisks. Sample runs: Enter n: 4 * ******* © CS 1101 (AY 2009 -2010 Semester 1) Enter n: 7 * ****************** Week 6 - 11

Asterisks. V 3. java n n n Now, write Asterisks. V 3. java to

Asterisks. V 3. java n n n Now, write Asterisks. V 3. java to read in a positive integer n and display n rows of asterisks in a “Christmas Tree” pattern. How do you adapt Asterisks. V 2. java for this? Sample runs: Enter n: 4 * ******* © CS 1101 (AY 2009 -2010 Semester 1) Enter n: 7 * ****************** Week 6 - 12

Take-home lab assignment #3 n n It has been released. Deadline is the Monday

Take-home lab assignment #3 n n It has been released. Deadline is the Monday when school reopens after the recess, 28 September 2009, 23: 59 hr. Any questions? © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 13

Programming Errors n Compilation errors Syntax error (example: missing a semi-colon). ¨ Semantic error.

Programming Errors n Compilation errors Syntax error (example: missing a semi-colon). ¨ Semantic error. (For example, applying modulus % on floating -point value for certain programming languages. In Java , is it fine? Yes!) ¨ Easiest type of errors to fix. ¨ n Runtime errors Occur at runtime. ¨ Java’s exception mechanism can catch such errors. ¨ n Logic errors Program runs but produces incorrect result. ¨ Hard to characterize, hence hardest to fix. ¨ n Programming errors are also known as bugs ¨ Origin: a moth in the Mark I computer. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 14

Testing and Debugging (1/5) n Testing ¨ n Debugging ¨ n To determine if

Testing and Debugging (1/5) n Testing ¨ n Debugging ¨ n To determine if a code contains errors. To locate the error and fix it. Documentation To improve maintainability of the code. ¨ Include sensible comments, good coding style and clear logic. ¨ Testing Error? Yes Debug © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 15

Testing and Debugging (2/5) n Modularization and interfaces ¨ ¨ ¨ Problem is broken

Testing and Debugging (2/5) n Modularization and interfaces ¨ ¨ ¨ Problem is broken into sub-problems and each sub-problem is tackled separately – divide-and-conquer. Such a process is called modularization. The modules are possibly implemented by different programmers, hence the need for well-defined interfaces. The signature of a method (its return type, name and parameter list) constitutes the interface. The body of the method (implementation) is hidden – abstraction. Good documentation (example: comment to describe what the method does) aids in understanding. static double max(double a, double b) Returns the greater of two double values. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 16

Testing and Debugging (3/5) n Manual walkthroughs Tracing with pencil-and-paper. ¨ Verbal walkthroughs ¨

Testing and Debugging (3/5) n Manual walkthroughs Tracing with pencil-and-paper. ¨ Verbal walkthroughs ¨ n Print statements Easy to add ¨ Provide information: ¨ v v ¨ Which methods have been called The value of parameters The order in which methods have been called The values of local variables and fields at strategic points Disadvantages v v v Not practical to add print statements in every method Too many print statements lead to information overload Removal of print statements tedious © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 17

Testing and Debugging (4/5) n Example on writing print statements System. out. print("Enter 6

Testing and Debugging (4/5) n Example on writing print statements System. out. print("Enter 6 -digit Matriculation number: "); int number = scanner. next. Int(); // Extract int digit 6 int digit 5 int digit 4 int digit 3 int digit 2 the digits = number%10; = number%10; number /= /= 10; 10; int step 1 = digit 2*2 + digit 3*6 + digit 4*2 + digit 5*4 + digit 6; System. out. println("Step 1 = " + step 1); // for checking int step 2 = step 1 % 13; System. out. println("Step 2 = " + step 2); // for checking int step 3 = 13 - step 2; System. out. println("Step 3 = " + step 3); // for checking : : © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 18

Testing and Debugging (5/5) n Tips and techniques ¨ ¨ ¨ ¨ ¨ Start

Testing and Debugging (5/5) n Tips and techniques ¨ ¨ ¨ ¨ ¨ Start off with a working algorithm Incremental coding/test early/fix bugs as you find them Simplify the problem Explain the bug to someone else Recognize common errors (such as using ‘=’ instead of ‘==’, using ‘==’ instead of equals( ), infinite loop, etc. ) Recompile everything Test boundaries Test exceptional conditions Take a break © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 19

Testing Thoroughly (1/3) n Test your programs with your own data q n Do

Testing Thoroughly (1/3) n Test your programs with your own data q n Do not rely on Course. Marker to test your programs! We discussed this in week 4. Richard couldn’t spot the error in this code (Find. Max. V 2. java) of his: // To find the maximum among 3 integer // values in variables num 1, num 2, num 3. int max = 0; if (num 1 > num 2 && num 1 > num 3) max = num 1; if (num 2 > num 1 && num 2 > num 3) max = num 2; if (num 3 > num 1 && num 3 > num 2) max = num 3; q q He claimed that he tested it on many sets of data: <3, 5, 9>, <12, 1, 6>, <2, 7, 4>, etc. and the program works for all these. What did he miss out in his testing? © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 20

Testing Thoroughly (2/3) n In testing your programs thoroughly, do not forget about boundary

Testing Thoroughly (2/3) n In testing your programs thoroughly, do not forget about boundary or special cases! q n These are the cases where the program may give the wrong answer – a common error In the Primality Test problem (checking if an integer is a prime), what are the boundary cases? © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 21

Testing Thoroughly (3/3) n It is also important to test all the paths that

Testing Thoroughly (3/3) n It is also important to test all the paths that your program control flow can take q q Design test data to check all paths Example Test data: <x=0, z=1> to test path A, B, G, H; <x=3, z=3> to test path E, F, C, D; etc. © CS 1101 (AY 2009 -2010 Semester 1) if (x != 3) { y = 5; } else { z = z - x; } if (z > 1) { z = z / x; } else { z = 0; } if (x != 3) A E y=5 z=z-x B F if (z > 1) C z=z/x D G z=0 H Week 6 - 22

Debugger n Debugger usually provides the following q q q n Stepping Breakpoint Watches

Debugger n Debugger usually provides the following q q q n Stepping Breakpoint Watches (inspecting variables) We will illustrate these on Dr. Java © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 23

Other readings n n You will learn other advance testing techniques when you do

Other readings n n You will learn other advance testing techniques when you do Software Engineering. Some websites http: //www. cs. wustl. edu/~kjg/CS 101_SP 97/Notes/Debugging/debu gging. html ¨ http: //www. csl. mtu. edu/cs 2321/www/SELectures/SELeccture 3_Deb uging. And. Testing. htm ¨ Program testing can be used to show the presence of bugs, but never to show their absence. ~ Edgar Dijkstra © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 24

Master. Mind (1/3) n Let’s play the game! © CS 1101 (AY 2009 -2010

Master. Mind (1/3) n Let’s play the game! © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 25

Master. Mind (2/3) n You will be given the following on the module website

Master. Mind (2/3) n You will be given the following on the module website (“Resources” – “Lectures”). Look for “Week 6”, “Master. Mind”. q Peg. java v q MMCode. java v v q This is the GUI interface. You do not need the source code, but if you have problem with this class file, you may request for the source code so that you can compile it. . gif files v q You are to complete the count. Sinks(MMCode) and count. Hits(MMCode) methods. You should not change the other methods in the program. Master. Mind. GUI. class v q You are not to modify this file. These are the gif files you need for the GUI interface. Master. Mind. java v This is the application program (with the main method). You are to complete this program. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 26

Master. Mind (3/3) n Treat this as your holiday exercise! q n n This

Master. Mind (3/3) n Treat this as your holiday exercise! q n n This is actually a past year’s lab exercise. You may see the write-up on the module website, “CA – Labs”, under “Old Labs (AY 2007/8 Semester 1)”, “Lab #4: Selection and Repetition II”. If you do not want the GUI features, just remove those statements in Master. Mind. java which are suffixed with the comment “– GUI enhancement”. Note that the programs may contain some Java syntax/features not yet covered: q q ‘this’, overloaded constructors, etc. These will be explained in the next lecture when we cover Chapter 7. ‘char’ data type which is covered in Chapter 9. You may read up on your own. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 27

Summary for Today n n n Writing modular programs How to test your programs

Summary for Today n n n Writing modular programs How to test your programs Using the debugger © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 28

Announcements/Things-to-do (1/2) n Complete the following q Master. Mind n Take-home lab #3 deadline:

Announcements/Things-to-do (1/2) n Complete the following q Master. Mind n Take-home lab #3 deadline: 28 September 2009, Monday, 23: 59 hr. n Next week is recess q q n Revise and prepare for mid-term test Mid-term test on 3 October 2009, Saturday To prepare for next lecture q Read Chapter 7 and its Power. Point file before you come for lecture. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 29

Announcements/Things-to-do (2/2) n Sit-in Lab #1 q q q To be conducted during this

Announcements/Things-to-do (2/2) n Sit-in Lab #1 q q q To be conducted during this week’s discussion session. Please be punctual. Make-up lab will only be allowed if you miss the lab with valid reason and proof. Topics tested include everything you have learned. Sit-in lab #1 constitute 5% of your final grade. Happy Recess! © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 30

End of File © CS 1101 (AY 2009 -2010 Semester 1) Week 6 -

End of File © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 31