1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown, 2004-7.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
The Java Programming Language
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Outline Java program structure Basic program elements
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
Java Programming: From the Ground Up
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
The Java Programming Language
Input, Output, and Processing
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Adding and Subtracting Rational Numbers
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
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.
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.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Software Engineering 4, Julian Richardson, 30 April Static Analysis Software Engineering HX3 Julian Richardson
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Operator Overloading Week 5.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
The Hashemite University Computer Engineering Department
Error Handling Tonga Institute of Higher Education.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
The need for Programming Languages
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
C Basics.
Introduction to C++ Programming
MSIS 655 Advanced Business Applications Programming
Adding and Subtracting Rational Numbers
Java’s Central Casting
Java 1/31/2017 Back to Objects.
Building Java Programs
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
Chapter 3 Debugging Section 3.4
Presentation transcript:

1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,

2 Program Correctness Correctness is an important aspect of software quality. There are a number of different types of errors: ●Lexical error: using words or symbols not in the language ●Syntax error: using words or symbols in the wrong order ●Semantic error: using constructs in a meaningless way ●Run-time error: your code cannot be executed ●Logical error: your program compiles and runs, but doesn't do what it was meant to do

3 What could go wrong? (I) Lexical error: keywords or symbols not in the language ●Misspellings and typos ● total = mutlipy(x, y); ● total <- multiply(x,y); Syntax error: words or symbols in the wrong order ●Example: wrong type of bracket ● total = multiply(x, y): ● total multiply(x,y); picked up at compile time

4 What could go wrong? (II) Semantic error: constructs used in the wrong way ● int sum = true; ●calling a method where the class doesn't define that method Run-time error: your code cannot be executed, i.e. crashes ● int values[] = new int[10]; last = values[20]; Logical error: Your program compiles and runs, but doesn't do what it was meant to do ● int values[] = {3,4,5,6,7} //compute the average of the array double ave = (value[0] + values[4]) / 5.0; may be picked up at compile time if lucky may be picked up at compile time if lucky the hardest errors to spot – the language tools give you no help – you must implement test routines to check for logical errors the hardest errors to spot – the language tools give you no help – you must implement test routines to check for logical errors

5 A possible Software Development process 1 ? Type the whole program in in one go, off the top of your head Repeat compile and hack away at the compile-time errors until no errors For each example given in assignment sheet run the program while program is not giving correct results hack away at parts that might be causing the problem repeat compile and hack away at the compile-time errors until no errors Add comments as time allows Submit the program 1. suggested by Dr Derek Bridge

6 A better process (expanding on the development process from last week) determine the class interface for each item in the interface ●write test-cases in a test method ●write pseudocode method body first ●write Java code ●add test cases ●compile and run test cases

7 Testing: the proper attitude ●Goal is destructive - aim is to break the program ●You should assume you will never eliminate all the bugs on a program of any reasonable complexity ●Question: what is testing complete? ●Testing is tedious – nobody likes doing it – so we try to automate it ●Write a test driver for each class ●the test driver should create an object and test the methods on a variety of inputs, including random ones ●a test-driver is reused, so when we change the code, we run the test driver again to see if we have introduced more bugs ●You can use the main method as a test driver, or write another static method, or another class

8 Automated Testing ●try to automate it as much as possible ●comparing actual values returned by methods with the required values should be automated ●calculate the required values by hand and hard-code them into the driver ●check some known properties of the actual values ●include code that computes the required value by some other approach ●you will not be able to test every possible permutation ●aim for good coverage – a smaller number of well-targeted tests is better than many redundant ones ●choose test cases that are representative of the data you expect the final program to use ●test using boundary cases – values at the limits of what is acceptable

9 Example Class: Fraction Representing rational numbers (i.e. fractions). We need to be able to create them from the numerator (top) and denominator (bottom), compare them, add them, subtract them, multiply them, divide them, and display them. Constructor: ●create with a (integer) numerator and denominator public methods: ●add another fraction and return a fraction ● (and subtract, multiply and divide) ●check if is equal to another fraction and return boolean ●check if is less than another fraction and return boolean ●represent as a string

10 The Fraction public interface public class Fraction { public Fraction(int top, int bottom); public String toString(); public boolean isEqualTo(Fraction other); public boolean isLessThan(Fraction other); public Fraction plus(Fraction other); public Fraction minus(Fraction other); public Fraction multiply(Fraction other); public Fraction divideBy(Fraction other); public static void main(String[] args); } Test driver … and now go over each one in turn, writing statements to test it in the test driver.

11 Constructor and toString() /** * Creates a new Fraction object with numerator and * denominator. Either input may be negative top the numerator bottom the denominator; cannot be 0 */ public Fraction(int top, int bottom) Testing the Constructor (and other methods) will be made easier if we specify the toString() method: /** * Returns a String representation of the Fraction * in normal form: i.e. returns "2/3" and never "4/6". a String representation */ public String toString()

12 Starting the testDriver /** * Test driver. Displays results to standard output. */ public static void main(String[] args) { Fraction f; // Testing the constructor and toString f = new Fraction(2,3); if (! (f.toString().equals("2/3"))) System.out.println("Error: " + f + " should be 2/3"); f = new Fraction(4,6); if (! (f.toString().equals("2/3"))) System.out.println("Error: " + f + " (input = (4,6)" + " should be 2/3"); } Other test cases might include e.g. 3/2, -2/3, -4/6, -3/2, 6/4, -6/4, 3/1, -3/1, 0/1, 3/3, -3/3, 1/1, -1/1, 0/3, 2/-3, -2/-3, 1/0, 3/0, -3/0, 0/0

13 Continuing the interface: plus public Fraction plus(Fraction other) //minus, multiply, divide are similar /** * Returns a new Fraction that represents the sum of this * Fraction and the input Fraction. other the Fraction we are adding; must not be null a Fraction representing the sum */

14 Continuing the test driver f1 = new Fraction(1,2); f2 = new Fraction(1,3); f3 = new Fraction(5,6); System.out.println("Test: " + f1 + " + " + f2 + " == " + f3); if (! (f1.plus(f2).toString().equals(f3.toString()))) System.out.println("Error!"); //exploiting mathematical properties: // f1 + f2 – f1 == f2 System.out.println("Test: f1 + f2 – f1 == f2"); if (! (f1.plus(f2).minus(f1).toString().equals(f2.toString()))) System.out.println("Error!);

15 Interface (cont): isEqualTo() public boolean isEqualTo(Fraction other) //lessThan is similar /** * Determines whether the input Fraction is equal to this one. * True if and only if input Fraction is not null, and its * reduced form is identical to the reduced form of this one. other the input Fraction to be compared true if mathematically equal; false otherwise */

16 Improving the test driver f1 = new Fraction(1,2); f2 = new Fraction(1,3); f3 = new Fraction(5,6); f4 = new Fraction(2,6); System.out.println("Test: 1/3 == 2/6"); if (! (f2.isEqualTo(f4))) System.out.println("Error!"); System.out.println("Test: " + f1 + " + " + f2 + " == " + f3); if (! (f1.plus(f2).isEqualTo(f3))) System.out.println("Error!"); //exploiting mathematical properties: // f1 + f2 – f1 == f2 System.out.println("Test: f1 + f2 – f1 == f2"); if (! (f1.plus(f2).minus(f1).isEqualTo(f2))) System.out.println("Error!);

17 Using Random Tests System.out.println("Test: random testing"); for (int i = 0; i<1000; i++) { int r1 = (int)(Math.random() * 100 – 50); int r2 = (int)(Math.random() * 100 – 50); int r3 = (int)(Math.random() * 100 – 50); int r4 = (int)(Math.random() * 100 – 50); if (r2 != 0 && r4 != 0) { f1 = new Fraction(r1,r2); f2 = new Fraction(r3,r4); if (! (f1.plus(f2).minus(f1).isEqualTo(f2))) System.out.println("Error! " + f1 + "::" + f2); }