Java Reference
In-Depth Information
We print the value that is returned by the getX method, and then we print a message
that describes what value we expect to see.
This is a very important step. You want to spend some time thinking what the expected
result is before you run a test program. This thought process will help you understand
how your program should behave, and it can help you track down errors at an early
stage.
Determining the expected result in advance is an important part of testing.
In our case, the rectangle has been constructed with the top left corner at (5, 10). The
x-direction is moved by 15 pixels, so we expect an x-value of 5+15=20 after the
move.
Here is a complete program that tests the moving of a rectangle.
ch02/rectangle/MoveTester.java
1 import java.awt.Rectangle;
2
3 public class MoveTester
4 {
5 public static void main(String[] args)
6 {
7 Rectangle box = new Rectangle( 5 , 10 ,
20 , 30 );
8
9 // Move the rectangle
10 box.translate( 15 , 25 );
11
12 // Print information about the moved rectangle
13 System.out.print( Ðx: Ñ );
14 System.out.println(box.getX());
15 System.out.println( ÐExpected: 20Ñ );
16
17 System.out.print( Ðy: Ñ );
18 System.out.println(box.getY());
19 System.out.println( ÐExpected: 35Ñ );
20 }
21 }
Search WWH ::




Custom Search