A program which features a Circle class - just about like the blue.shapes.Circle class.
The program will read a real number corresponding to the radius of a circle, create the circle, and display its area and perimeter.
class CircleValuesApp
{
/ /Main method
static public void main (String args [])
{
IO.print (``Radius? ``);
double r = IO.read_double ();
Circle c = new Circle (r);
IO.println (``Area = ``+ c.area ());
IO.println (``Perimeter = ``+ c.perimeter ());
}
class Circle
{
/ /Instance variables
double radius;
/ /Constructor
public Circle (double x)
{
radius = x;
}
/ /Methods
public double area ()
{
return (radius * radius * Math.PI);
}
public double perimeter ()
{
return (2 * radius * Math.PI);
}
|
Definition
The GLOBAL PROCEDURAL APPLICATION ARCHITECTURE is the application architecture in which:
- all variables which appear anywhere within the class are declared local to the class, and hence are global to all of the methods of the class.
- all methods defined in the class are commands.
An operational procedure for writing an application consistant with the global procedural application architecture.
- Sketch a main method which hypothesizes the existence of commands which do what needs to be done.
- Add to ``one'' a ``data dictionary'' which associates variables with the concepts introduced in the main method.
- Refine the commands hypothesized in the main method in such a way that they ``fit in'' with the data dictionary.
Once you have done this, or as you are doing it, place the code where it belongs in program text.
Global procedural architecture for the ``line of squares''
The main method
static public void main (String arg [])
{
readProblemParameters ();
computeAreaOfOneSquare ();
computeLengthOfSide ();
computeNumberOfExternalEdges ();
computePerimeter ();
displayPerimeter ();
}
Data dictionary
static private double figureArea;
static private int nrSquares;
static private double squareArea;
static private double sideLength;
static private int eeCount;
static private double perimeter;
Refine the commands in the context of # 1 and # 2
static private void readProblemParameters ()
{
IO.print (``Area of figure? ``);
figureArea = IO.read_double ();
IO.print (``NR of squares? ``);
nrSquares = IO.read_int ();
}
static private void computeAreaOfOneSquare ()
{
squareArea = figureArea /nrSquares;
}
static private void computeLengthOfSide ()
{
sideLength = Math.sqrt (squareArea);
}
static private void
computeNumberOfExternalEdges ()
{
eeCount = (nrSquares * 2) + 2;
}
static private void computePerimeter ()
{
perimeter = eeCount * sideLength;
}
static private void displayPerimeter ()
{
IO.println (``Perimeter is ``+ perimeter);
}
|