The Rectangle Class
Specifications of the rectangle class:
CONSTRUCTOR
new Rectangle (< double >, < double >) -- < rectangle >
OPERATORS
< rectangle > .length () -- < double >
< rectangle > .width () -- < double >
< rectangle > .area () -- < double >
< rectangle > .perimeter () -- < double >
< rectangle > .diagonal () -- < double >
Commands
< rectangle > .display ()
PROBLEM:
Suppose we have a city block of length 480 and width 300 partitioned into 16 rectangular lots.
/ /Model the block and a lot in terms of rectangles.
double blockLength = 480;
double blockWidth = 300;
Rectangle block = new Rectangle (blockLength,
blockWidth);
double lotLength = blockLength / 8.0;
double lotWidth = blockWidth / 2.0;
Rectangle Lot = new Rectangle (lotLength,
lotWidth);
/ /Display the distance from one corner of the block
to the far corner.
IO.println (block.diagonal ());
/ /Declare a variable called pc and bind it to a percent
of the block taken up by one lot.
double pc = (lot.area () /block.area ()) * 100.0;
|
PROBLEM:
What is the surface area of a cylinder of height 28.5 and diameter 11.6?
Note > The surface area of the cylinder equals the area of the top + the area of the bottom + the area of the side.
We will use problem decomposition to solve this problem. We can ``explode'' the cylinder into two circles and a rectangle.
? class -- SurfaceAreaApp
? Needs -- import blue.shapes. *;
? Program
/ /Establish the given information.
double cylinderHeight = 28.5;
double cylinderDiameter = 11.6;
/ /Create the top.
double topRadius = cylinderDiameter / 2.0;
Circle top = new Circle (topRadius);
/ /Create the bottom.
double bottomRadius = topRadius;
Circle bottom = new Circle (bottomRadius);
/ /Create the side.
double sideHeight = cylinderHeight;
double sideLength = top.perimeter ();
Rectangle side = new Rectangle (sideHeight,
sideLength);
/ /Compute the surface area of the cylinder.
double sa = top.area () + bottom.area () +
side.area ();
/ /Display the result.
IO.println (``The surface area = ``+ sa +
``square units.'')
|
String Manipulation
< String > + < String > -- < String >
Example:
``sun'' + ``shine'' -- ``sunshine''
|
< String > + < int > -- < String >
Example:
``x = ``+ 5.2
means ``x = ``+ ``5.2''
means ``x = 5.2''
|