CSC 102 Lecture Notes Week 2
Introduction to Incremental Development and Systematic Testing
More Java Basics
/**** * Class SimpleFractionTest is a very small example illustrating what your * FractionTest class can look like for Programming Assignment 1. */ public class SimpleFractionTest { /** * Call the test method for testNumeratorDenominator. In the complete * FractionTest you're writing, this main method calls all of the faction * test methods. */ public static void main(String[] args) { testNumeratorDenominatorConstructor(); } /** * Test the full initializing constructor of the Fraction class with three * sample test cases. In the full version of this test method you'll need * some additional test cases. Use the guidelines in Lecture Notes 2 to * help figure out what the additional test case should be. */ private static void testNumeratorDenominatorConstructor() { Fraction f; // value produced by the constructor int n; // convenience varible for the numerator value int d = 0; // convenience varible for the denominator value // Test Case 1: check the boundary case of a zero numerator "0/1". f = new Fraction(0,1); if ((n = f.getNumerator()) != 0 || (d = f.getDenominator()) != 1) { System.out.println("Got " + n + "/" + d + ", expected 0/1"); } // Test Case 2: check a simple case the doesn't need reduction. f = new Fraction(1,2); if ((n = f.getNumerator()) != 1 || (d = f.getDenominator()) != 2) { System.out.println("Got " + n + "/" + d + ", expected 1/2"); } // Test Case 3: check a case that needs some reduction. f = new Fraction(4,8); if ((n = f.getNumerator()) != 1 || (d = f.getDenominator()) != 2) { System.out.println("Got " + n + "/" + d + ", expected 1/2"); } } }
/**** * Class SimpleFractionTest is a very small example illustrating what your * FractionTest class can look like for Programming Assignment 1. */ public class SimpleFractionTest { /** * Call the test method for testNumeratorDenominator. In the complete * FractionTest you're writing, this main method calls all of the faction * test methods. */ public static void main(String[] args) { testNumeratorDenominatorConstructor(); } /** * Test the full initializing constructor of the Fraction class with three * sample test cases. In the full version of this test method you'll need * some additional test cases. Use the guidelines in Lecture Notes 2 to * help figure out what the additional test case should be. */ private static void testNumeratorDenominatorConstructor() { Fraction f; // value produced by the constructor // Test Case 1: check the boundary case of a zero numerator "0/1". test(0, 1, 0, 1); // Test Case 2: check a simple case the doesn't need reduction. test(1, 2, 1, 2); // Test Case 3: check a case that needs some reduction. test(4, 8, 1, 2); } /** * Output an error if the given Fraction f does not have the given * values for nExpected and dExpected for its numerator and denominator. */ private static void test(int nIn, int dIn, int nExpected, int dExpected) { int n; // convenience variable for the numerator value int d = 0; // convenience variable for the denominator value Fraction f = new Fraction(nIn, dIn); if ((n = f.getNumerator()) != nExpected || (d = f.getDenominator()) != dExpected) { System.out.println("Got " + n + "/" + d + " expected " + nExpected + "/" + dExpected); } } }
That's it for our the introduction to testing.
We'll have plenty more to say about it as the quarter goes on.
In the meantime for these notes, we'll resume our discussion on
the basics of object-oriented programming in Java.
int i = 120; double d = 120.65; boolean b = false; char c = 'x'
String s = "xyz"; int[] a = {1, 2, 3}; Rectangle r = new Rectangle(10, 20, 100, 200);
Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }
public class Rectangle { int x; int y; int width; int height; // // Simple accessor methods are typically called "getters" // /** * Return the x coordinate value. */ public int getX() { return x; } /** * Return the y coordinate value. */ public int getY() { return y; } /** * Return the width. */ public int getWidth() { return width; } /** * Return the height. */ public int getHeight() { return height; } // // Simple mutator methods are typically called "setters". // /** * Set the x coordinate to the given int value. */ public void setX(int x) { this.x = x; } /** * Set the y coordinate to the given int value. */ public void setY(int y) { this.y = y; } /** * Set the width to the given int value. */ public void setWidth(int width) { this.width = width; } /** * Set the height to the given int value. */ public void setHeight(int height) { this.height = height; } // ... other methods of the Rectangle class, as shown in ../Rectangle.java }
the following are some sample comparisons:int i = 120; double d = 120.65; boolean b = false; char c = 'x';
i == 120; // true i == 120.65; // false i == d; // false i == c; // true d == c; // false c == b; // compilation error: incompatible types i == b; // compilation error: incompatible types d == b; // compilation error: incompatible types
String s = "xyz"; int[] a = {1, 2, 3}; Rectangle r = new Rectangle(10, 20, 100, 200);
s == "xyz"; // true s == "abc"; // false int[] a2 = {1, 2, 3}; a == a2; // false a.equals(a2); // false ( .equals on arrays defaults to == ) Rectangle r2 = new Rectangle(10, 20, 100, 200); r == r2; // false r.equals(r2); // true ( this works given Rectangle.equals )
r == r2
r.equals(r2)
boolean equals(Rectangle r) { return x == r.x && y == r.y && width == r.width && height == r.height; }
/**** * * Examples of how equality works for literal string values. See Lecture Notes * 2 for some explanatory discussion. * */ public class StringEquality { public static void main(String[] args) { String s1 = "xyz"; String s2 = "xyz"; String s3 = new String("xyz"); String s4 = new String("xyz"); System.out.println(s1 == s2); // true System.out.println(s1 == s3); // false System.out.println(s3 == s4); // false System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // true System.out.println(s3.equals(s4)); // true } }
private final int MAX_TRANSACTIONS = 100;
is the string "The value of i and j are 10 and 20.""The value of i and j are " + i " and " + j "."
try { . . . method call that could throw an exception . . . catch (ExceptionName e) { . . . code that handles the exception, e.g., by printing an error message . . . }
try { // Test that exception is thrown with a zero denominator value. new Fraction(1, 0); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException thrown as expected"); }