CS1101: Programming Methodology

Slides:



Advertisements
Similar presentations
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Advertisements

FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Software Engineering and Design Principles Chapter 1.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.
Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Well-behaved objects Improving your coding skills 1.0.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS 201 Functions Debzani Deb.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Guide To UNIX Using Linux Third Edition
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
Ranga Rodrigo. Class is central to object oriented programming.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Modular Programming. Modular Programming (1/6) Modular programming  Goes hand-in-hand with stepwise refinement and incremental development  Makes the.
The Java Programming Language
CSE 219 Computer Science III Program Design Principles.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
WEEK 4 Class Activities Lecturer’s slides.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
CS1101: Programming Methodology Aaron Tan.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS1101: Programming Methodology
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
UNIT 13 Separate Compilation.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
CS1101: Programming Methodology
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
1 Program Planning and Design Important stages before actual program is written.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
Chapter 3: User-Defined Functions I
The Hashemite University Computer Engineering Department
CS1101: Programming Methodology
CS1101: Programming Methodology
CS1010: Programming Methodology
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Bugs CS100 how to prevent them, how to find them and how to terminate them.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Repetition Statements
CS1101X Programming Methodology
Testing and Debugging.
Loop Structures.
CS1010 Programming Methodology
CS1010 Programming Methodology
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Problem Solving Techniques
CS1100 Computational Engineering
MSIS 655 Advanced Business Applications Programming
Lecture Notes – Week 4 Chapter 5 (Loops).
Week 4 Lecture-2 Chapter 6 (Methods).
Review of Previous Lesson
The while Looping Structure
Presentation transcript:

CS1101: Programming Methodology

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 © CS1101 (AY Semester 1)Week6 - 2

Last Week’s Exercise #4: Prime-Number Your DL should have discussed this in last week’s discussion session. Write a program PrimeTest.java to check if a positive integer is a prime. Sample runs: © CS1101 (AY Semester 1)Week6 - 3 Enter a positive integer: is a prime. Enter a positive integer: is not a prime.

Modular Programming (1/6) Download PrimeTestNonModular.java and PrimeTest.java. Study and discuss them. Modular programming  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. © CS1101 (AY Semester 1)Week6 - 4

Modular Programming (2/6) 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)  Should be well-defined  Should do one task © CS1101 (AY Semester 1)Week6 - 5

Modular Programming (3/6) A well-defined module  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.  Example: setColour(String c) in the Ball class  Example: getColour() in the Ball class © CS1101 (AY Semester 1)Week6 - 6 public void setColour(String c) { colour = c; } Takes in String c. Does not return any value (void). public String getColour() { return colour; } Takes in no parameter. Returns a String.

Modular Programming (4/6) Advantages of modular programming:  Easy to replace  E.g.: When you discover a better algorithm for isPrime(int), you just replace that method without affecting any other parts of the program or other programs.  Easy to reuse  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 PrimeTestNonModular.java and PrimeTest.java to do the above. © CS1101 (AY Semester 1)Week6 - 7

Modular Programming (5/6) Reusability of code  If isPrime(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 isPrime(int).  See CountPrimes.java which is an application program that makes use of the Prime class.  It is so short and sweet!  Any other application that requires the isPrime(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. © CS1101 (AY Semester 1)Week6 - 8

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

Last Week’s Exercise #2: AsterisksV1.java Complete AsterisksV1.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: © CS1101 (AY Semester 1)Week Enter n: 7 ******* Done! Enter n: -2 Done! Download AsterisksV1Completed.java and study it.

AsterisksV2.java Now, write AsterisksV2.java to read in a positive integer n and display n rows of asterisks in the following fashion.  First row has 1 asterisk, second row 3 asterisks, third row 5 asterisks, etc. Your program should be modular. There should be a method printStars(int k) to print a row of k asterisks. Sample runs: © CS1101 (AY Semester 1)Week Enter n: 4 * *** ***** ******* Enter n: 7 * *** ***** ******* ********* *********** *************

AsterisksV3.java Now, write AsterisksV3.java to read in a positive integer n and display n rows of asterisks in a “Christmas Tree” pattern. How do you adapt AsterisksV2.java for this? Sample runs: © CS1101 (AY Semester 1)Week Enter n: 4 * *** ***** ******* Enter n: 7 * *** ***** ******* ********* *********** *************

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

Programming Errors 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. Runtime errors  Occur at runtime.  Java’s exception mechanism can catch such errors. Logic errors  Program runs but produces incorrect result.  Hard to characterize, hence hardest to fix. Programming errors are also known as bugs  Origin: a moth in the Mark I computer. © CS1101 (AY Semester 1)Week6 - 14

Testing and Debugging (1/5) Testing  To determine if a code contains errors. Debugging  To locate the error and fix it. Documentation  To improve maintainability of the code.  Include sensible comments, good coding style and clear logic. © CS1101 (AY Semester 1)Week Testing Yes Error? Debug

Testing and Debugging (2/5) 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. © CS1101 (AY Semester 1)Week static double maxmax(double a, double b) Returns the greater of two double values.

Testing and Debugging (3/5) Manual walkthroughs  Tracing with pencil-and-paper.  Verbal walkthroughs Print statements  Easy to add  Provide information:  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  Not practical to add print statements in every method  Too many print statements lead to information overload  Removal of print statements tedious © CS1101 (AY Semester 1)Week6 - 17

Testing and Debugging (4/5) Example on writing print statements © CS1101 (AY Semester 1)Week System.out.print("Enter 6-digit Matriculation number: "); int number = scanner.nextInt(); // Extract the digits int digit6 = number%10; number /= 10; int digit5 = number%10; number /= 10; int digit4 = number%10; number /= 10; int digit3 = number%10; number /= 10; int digit2 = number%10; int step1 = digit2*2 + digit3*6 + digit4*2 + digit5*4 + digit6; System.out.println("Step 1 = " + step1); // for checking int step2 = step1 % 13; System.out.println("Step 2 = " + step2); // for checking int step3 = 13 - step2; System.out.println("Step 3 = " + step3); // for checking :

Testing and Debugging (5/5) 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 © CS1101 (AY Semester 1)Week6 - 19

Testing Thoroughly (1/3) Test your programs with your own data  Do not rely on CourseMarker to test your programs! We discussed this in week 4. Richard couldn’t spot the error in this code (FindMaxV2.java) of his: © CS1101 (AY Semester 1)Week // To find the maximum among 3 integer // values in variables num1, num2, num3. int max = 0; if (num1 > num2 && num1 > num3) max = num1; if (num2 > num1 && num2 > num3) max = num2; if (num3 > num1 && num3 > num2) max = num3;  He claimed that he tested it on many sets of data:,,, etc. and the program works for all these.  What did he miss out in his testing?

Testing Thoroughly (2/3) In testing your programs thoroughly, do not forget about boundary or special cases!  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? © CS1101 (AY Semester 1)Week6 - 21

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

Debugger Debugger usually provides the following  Stepping  Breakpoint  Watches (inspecting variables) We will illustrate these on DrJava © CS1101 (AY Semester 1)Week6 - 23

Other readings You will learn other advance testing techniques when you do Software Engineering. Some websites  ging.html ging.html  ugingAndTesting.htm ugingAndTesting.htm © CS1101 (AY Semester 1)Week Program testing can be used to show the presence of bugs, but never to show their absence. ~ Edgar Dijkstra

MasterMind (1/3) Let’s play the game! © CS1101 (AY Semester 1)Week6 - 25

MasterMind (2/3) You will be given the following on the module website (“Resources” – “Lectures”). Look for “Week 6”, “MasterMind”.  Peg.java  You are not to modify this file.  MMCode.java  You are to complete the countSinks(MMCode) and countHits(MMCode) methods.  You should not change the other methods in the program.  MasterMindGUI.class  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  These are the gif files you need for the GUI interface.  MasterMind.java  This is the application program (with the main method). You are to complete this program. © CS1101 (AY Semester 1)Week6 - 26

MasterMind (3/3) Treat this as your holiday exercise!  This is actually a past year’s lab exercise. You may see the write-up on the module website, “CA – Labs”, under “Old Labs (AY2007/8 Semester 1)”, “Lab #4: Selection and Repetition II”. If you do not want the GUI features, just remove those statements in MasterMind.java which are suffixed with the comment “– GUI enhancement”. Note that the programs may contain some Java syntax/features not yet covered:  ‘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. © CS1101 (AY Semester 1)Week6 - 27

Summary for Today Writing modular programs How to test your programs Using the debugger © CS1101 (AY Semester 1)Week6 - 28

Announcements/Things-to-do (1/2) Complete the following  MasterMind Take-home lab #3 deadline: 28 September 2009, Monday, 23:59hr. Next week is recess  Revise and prepare for mid-term test  Mid-term test on 3 October 2009, Saturday To prepare for next lecture  Read Chapter 7 and its PowerPoint file before you come for lecture. © CS1101 (AY Semester 1)Week6 - 29

Announcements/Things-to-do (2/2) Sit-in Lab #1  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. © CS1101 (AY Semester 1)Week Happy Recess!

End of File © CS1101 (AY Semester 1)Week6 - 31