Download presentation
Presentation is loading. Please wait.
1
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown, 2004-2007
2
2 Improving the Fraction design The interface specified in last lecture was: 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); } public int getNumerator(); public int getDenominator();
3
3 Designing the instance variables ●after specifying the interface, and stating how we will test the class, we need to define the representation ●Option 1: private double decimalValue; ●Option 2: private int numerator; private int denominator; ●should we "reduce" the fractions immediately? ●how do we represent negative fractions? ●whichever option we choose, it should be hidden – should not affect the interface, and we could change it later
4
4 Fraction instance variables /** * The Fraction's numerator * Fractions are stored in reduced form (e.g. 2/3 not 4/6) * 0/X is stored as 0/1. The numerator carries the sign. */ private int numerator; /** * The Fraction's denominator * Will always be positive. */ private int denominator;
5
5 Implementing the Constructor /** * Creates a new Fraction object with numerator and * denominator. Either input may be negative * @param top the numerator * @param bottom the denominator; cannot be 0 */ public Fraction(int top, int bottom) { /* Pseudocode: If fraction represents zero reduce denominator to 1 Else reduce numerator and denominator using gcd if denominator is negative move its sign to numerator */
6
if (top == 0) { //represent the zero fraction numerator = 0; denominator = 1; } else { int n = top; // create temporary num and denom. int d = bottom; /* gcd method requires +ve args, so pass abs values */ int g = gcd(Math.abs(n), Math.abs(d)); n = n / g; d = d / g; if (d < 0) {/* move any sign up to num */ n = - n; d = - d; } numerator = n; denominator = d; }
7
7 Greatest Common Divisor /** * Returns the greatest common divisor * of two positive integers. * @param a - any int; must be > 0. * @param b - any int; must be > 0. * @return the gcd of a and b. */ private static int gcd(int a, int b) { // Euclid's algorithm (recursive) if (b != 0) return gcd(b, a % b); else return a; } This is a "helper" method – it is only used inside the class – if there is no need for other objects to use it, we can make it private This is a "helper" method – it is only used inside the class – if there is no need for other objects to use it, we can make it private
8
8 Implementing String toString() /** * Returns a String representation of the Fraction in normal * form: i.e. it returns "2/3" and never "4/6". * @return a String representation */ public String toString() { return (numerator + "/" + denominator); }
9
9 Implementing boolean isEqualTo(Fraction f) /** * Determines whether 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. * @param other the input Fraction to be compared * @return true if mathematically equal; false otherwise */ public boolean isEqualTo(Fraction other) { if (other == null) return false; if (numerator == other.getNumerator() && denominator == other.getDenominator()) return true; else return false; }
10
10 Implementing Fraction plus(Fraction f) /** * Returns a new Fraction that represents the sum of this * Fraction and the input Fraction. * @param other the Fraction we are adding; must not be null * @return a Fraction representing the sum */ public Fraction plus(Fraction other) { /* a/b + c/d = ((a*d) + c*b))/(b*d) */ if (other == null) { System.out.println("ERROR: null fraction"); System.exit(-1); } int newTopFirstTerm = numerator * other.getDonominator(); int newTop2ndTerm = other.getNumerator() * denominator; int newTop = newTopFirstTerm + newTop2ndTerm; int newBottom = denominator * other.getDenominator(); return new Fraction(newTop,newBottom); }
11
11 Glass-box testing ●now that we have written the method bodies, we can do glass-box testing ●devise tests which examine all paths in the control flow ●devise tests which examine all statements ●don't forget manual inspection of the source code ●only now should we compile, run and debug...
12
12 Other methods for Fraction? ●we also suggested some methods for creating Fractions from integers, and for comparing to integers: public Fraction(int top); public boolean isEqualTo(int other); public boolean isLessThan(int other); ●Do we need all three of them? Could we re-use any code? Should we reduce the effort for the programmer, or reduce the number of methods we must write?
13
13 Improving the Test Driver It can be tedious writing out the sequence of statements required for each test: ●a comment so we know what is being tested ●a possible output so that the user knows ●the test ●the conditional statement to display an error message if the returned value is not what was required and it can make it difficult to read. We could simplify things by providing a standard "test" method in a Tester class.
14
14 A separate Tester class /** * A class for automating test methods. Takes a test and some * display strings, and displays result to standard output. * Maintains a count of number of failed tests. */ public class Tester { /** * The number of errors encountered by this Tester object * Initially set to 0. */ private int errors; /** * Default constructor. Initialises errors to 0. */ public Tester() {errors = 0; }
15
15 /** * Test method. Receives a boolean, a message String * and an Error String; displays the message String; * if the boolean is false, displays the error String. * If there is an error, increments the error count * @param test a boolean : was test true or false? * @param mes a String to display before the test * @param error a String to display if test found an error */ public void test(boolean test, String mess, String error){ System.out.println(pre + "... "); if (test == false) { System.out.println("Error: " + error); errors++; } /** * returns number of errors found by this Tester object. * @return the number of errors */ public int getErrors() { return errors; } }
16
16 the improved test driver public static void main(String args[]) { Fraction f, f1, f2, f3, f4; Tester tester = new Tester(); f1 = new Fraction(2,3); tester.test(f1.toString().equals("2/3"), "Testing Constructor and toString()", f1 + " should be 2/3"); f1 = new Fraction(2,-3); tester.test(f1.toString().equals("-2/3"), "Testing Constructor for (-)ve fractions", f1 + " should be -2/3"); //and so on System.out.println("*** " + tester.getErrors() + " errors reported."); the test the message the error report
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.