Colorado State University

Recitation R12 - Testing
Summer 2016

CS160: Foundations in Programming


The purpose of this lab is to:
  1. Call methods from another class by instantiating an object.
  2. To increase your understanding of method testing.

Part One

Write a test program to test all of the methods in a class.
  1. Create a new project in Eclipse called R12.
  2. Download the Java archive TestClass.jar.
  3. Import the TestClass.jar file using Build Path->Add External Archive
  4. Copy the interface file TestInterface.java to the R12/src directory.
  5. Create a new class called R12 with a main method.
  6. Instantiate TestClass in the main method using the TestInterface, as follows:
        // Instantiate test class
        TestInterface inter = new TestClass();
        
  7. Write code to test each of the six methods in the TestClass.
  8. Each method in TestClass has two distinct defects, so find them!

Interface

The following is the interface that you are testing. Pay careful attention to parameters and return values, and the description of the functionality. The testing program will ignore all of your output from System.out, so you can print whatever you would like.
// R12 Interface File
// Author: Chris Wilcox
// Date:   07/26/2016
// Class:  CS160
// Email:  wilcox@cs.colostate.edu

public interface TestInterface {

    // Returns a reversed version of the specified string,
    // which can contain any ASCII character.
    // For example, returns "edcba" for "abcde" 
    public String reverseString(String s);

    // Returns the specified string duplicated.
    // For example, returns "Hello There!Hello There!" for "Hello There!" 
    public String duplicateString(String s);

    // Creates an array of strings with
    // [0] = original, [1] = toUppercase(), [2] = toLowercase().
    // For example, returns {"Whatever", "WHATEVER", "whatever"} for "Whatever"
    public String[] createStrings(String s);

    // Cubes all elements in array of doubles.
    // For example, changes {2.0, 3.0, 4.0} to {8.0, 27.0, 64.0}
    public void cubeArray(double[] array);

    // Adds the elements in array of integers,
    // returning the sum as an integer.
    // For example, returns 21 for {1, 2, 3, 4, 5, 6}
    public int sumArray(int[] array);

    // Sorts an array of integers from low to high.
    // For example, changes {2, 6, 4, 9, 1} to {1, 2, 4, 6, 9}
    public void sortArray(int[] array);
}

Testing

Just a few tips on testing methods:
  1. Test with different sizes of strings and arrays.
  2. Test with different contents, for example a variety of characters in strings.
  3. Always test negative and positive and zero numerical values.
  4. Test with different array orderings.
  5. Don't be afraid of writing a loop to repeat the tests with many different values.
  6. Look for anomalies, i.e. incorrect results from each method that represent a defect.
  7. Overall you just want to adopt the mentality of trying to break things!
Please follow the usual rules for submitting Java programs.

Grading Criteria

Submission

Submit your source file named R12.java to the the Checkin tab on the course web site.

© 2016 CS160 Colorado State University. All Rights Reserved.