sqrt), and how to test doubles for equality (you can only make sure they are equal within some given tolerance).
class Foo{
double x;
Foo(double x){
this.x = x;}
double squareRoot(){
return Math.sqrt(this.x);}
}
class Examples {
Examples () {}
Foo f = new Foo(16.0);
boolean testSqRoot =
check this.f.squareRoot() expect 4.0 within 0.01;
}
+-------+
| Shape |<--------------------------+
+-------+ |
+-------+ |
| |
/ \ |
--- |
| |
--------------------------------------- |
| | | |
+--------------+ +--------------+ +--------------+ |
| Square | | Circle | | Combo | |
+--------------+ +--------------+ +--------------+ |
+-| CartPt nw | +-| CartPt center| | Shape top |----+
| | int size | | | int radius | | Shape bottom |----+
| | IColor color | | | IColor color | +--------------+
| +-------------+ | +--------------+
+----+ +-----------+
| |
v v
+-------+
| CartPt|
+-------+
| int x |
| int y |
+-------+
totalArea that computes the total area
of a shape. For the shape that consists of two components, add the areas.
(The ProfessorJ Math library defines a constant called PI that you will
need for this problem). For this and any other problem that asks you to design
a method, make sure that your method is adequately tested in the Examples class.
moveBy that produces a new shape moved by the given distance in the vertical and horizontal directions.
isWithin that determines whether the given point is within this shape. If you get stuck, refer to Section 12.1 in the
text for help.
import draw.*;
import colors.*;
import geometry.*;
class Examples{
Examples() {}
Canvas c = new Canvas(200, 200);
boolean makeDrawing =
// initialize the canvas...
this.c.show() &&
// ...and draw a solid circle on the canvas
this.c.drawDisk(new Posn(100, 150), 50, new Red());
}
The three import statements on the top indicate that we are using
code programmed by someone else and available in the libraries named
draw, colors, and geometry.
Open the Help Desk and look under the Teachpacks for
How to Design Classes to find out more about drawing and the
Canvas class. Understanding how to draw will be helpful when you design a
game in the next homework assignment.
Design the method drawShape that draws this shape on the given Canvas.
Using web-based turnin, turn in a single file hw3.username.bjava (where username is the wpi login name of one of the partners in your homework pair) containing all code and documentation for this assignment. Both partners' names and wpi login names should appear in a comment at the top of the file.