Lab 1
Lab 1 Description
Start by setting up a project in your favorite IDE and dowloading all the libraries we will use into some folder on your computer.
Warmup
Add to your project the file ExamplesCircleShape.java, add the tester.jar to the classpath, and run the program.
Read the code and see how the examples and test cases are designed.
The running of the tests is invoked in the main method as
Tester.runReport(e, true, true);
The first boolean argument indicates that we want to see a pretty-print display of all data defined in the Examples class. The second boolean argument indicates that we want to see the display of all test results, successful and failed ones as well.
In most cases we only show the failed test results. Also, if the Examples class defines a large number of sample data, we may choose not to display the data.
Shapes
The starter files:
Code with just the data definitions and examples of data ExamplesShapesData.java.
Code with the data definitions, examples of data, and some method definitions ExamplesShapesMethods.java.
Code with the data definitions for a complex shapes (the Combo class), examples of data, and some methods definitions ExamplesShapeCombo.java.
Add to your project the one of these files. Add to the project the tester.jar library file and run the project from the main method in the class Examples....
Study the code and the output to see the design of the examples and the design of the tests.
The code in ExamplesShapesData.java shows you this.
Run the code in ExamplesShapesMethods.java. Therea are three tests that failed. The tester recognized that the programmer tried to compare an integer with a double number and flagged it as an error. There are two ways of fixing this: one is to compare two exactly same double numbers (900.0), the other is to use the checkInexact variant for the test definition. There are examples of this earlier in the code.
For a pedagogical discussion of the design of inexact tests see inexact.html.
Now add a few more examples of shapes and a few more tests, just to see how it works.
Extend the definition of shapes by replacing the abstract class AShape by an interface IShape and adding a new class Combo that combines two shapes, a top one and a bottom one.
The code in the ExamplesShapeCombo.java shows you the solution.
Rewrite all methods as needed, add the methods for the Combo variant, together, of course, with the appropriate tests.
Design the method that produces the larger of the two shapes: this and the given one.
Design the method that produces a shape that looks the same as this one but is twice the size.
Think what would be needed to test the second method without having the tester library.
Mobiles
Start a new project. You can try to start from the scratch, or use the partially built program to speed up the work. Your goal is to design a representation of a mobile and design some methods that will help the designer of the physical objects. Because we want to see the drawing of the mobile, we add the libraries worldcanvas.jar, worldimages.jar, and colors.jar to the classpath. (The code already has the necessary import statements.)
The starter files:
Code with just the data definitions ExamplesMobileData.java.
Code with the data definitions and examples of data ExamplesMobileExamples.java.
Code with the data definitions, methods for drawing the mobiles, and examples of data ExamplesMobileDrawings.java.
Code with the data definitions, methods for drawing the mobiles, one defined method, the complete templates, examples of data, and tests for the one defined method. ExamplesMobileBasic.java.
Solution code ExamplesMobileSolution.java.
Design the data representation of a mobile. A mobile is either a simple ball (we know its color and weight) hanging on a thin line of some length, or it is a thin line of some length that has at the end a horizontal strut (we know the length of the left and right part from the place where it is suspended) with a mobile hanging from each end of the strut.
Import javalib.colors and define colors as new Black(), new White(), new Red(), new Blue(), new Green(), or new Yellow().
Here are some examples:
Simple mobile: Complex mobile: |
|
| | |
| ----+------ |
20 | | |
blue 30 | |
red ---+--- |
| | |
10 10 |
green red |
Design the following methods for your mobiles:
Design a method that will produce the total height of this mobile. Each ball has height 10.
Design a method that will produce a new mobile where every red ball is replaced with a yellow one.
Design the method that determines whether the mobile is balanced. The mobile is balanced when the weight of the left mobile multiplied by the length of the left strut equals the weight of the right mobile multiplied by the length of the right strut, and every mobile hanging from each strut is also balanced.
Think what would be needed to test the second and the third method without having the tester library.