the questions is: Create a test driver to test the functionality of your Rectangle class created in the previous question. my questions are: 1. what is test driver? 2. how to create it and use it to test the functionality of the classes I created before?

1 Answers

0
Abra On

I'm assuming that your Rectangle class contains methods. A test driver is a separate class that calls the methods of your Rectangle class. Methods, in general, usually have parameters and return values. The test driver should contain code that calls a method in Rectangle with values for parameters. For a given parameter value, you expect the Rectangle method to return a particular value, so in your test driver you call the Rectangle method and then check whether the value returned by that method is the expected value. For example:

public class Rectangle {
    private int length;
    private int width;

    public Rectangle(int width, int length) {
        this.width = width;
        this.length = length;
    }

    public int getArea() {
        return width * length;
    }
}

public class TestDriver {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(4, 3);
        int area = rect.getArea();
        if (area == 12) {
            System.out.println("Test passed.");
        }
        else {
            System.out.println("Test failed.");
        }
    }
}