



|
|
Byron's CSC212 Web Site
|
Class Notes
Tuesday October 10 , 2000
|
|
|
Circle Class
Class Notes --
Tuesday October 10 , 2000
CSC212 - October 10, 2000
=========================
Topics: 1) Write Circle Class
2) Lab Intro
Basic Architecture
Commands
Functions
Class
Lecture:
A program which features a circle class - just about
like the blue.shapes circle class.
The program will read a real number cir.IO the radius
of a circle, create the circle and display it's area
and perimeter.
class CircleValuesApp
{
// Main Method
// "Class"
static public real main (-------)
{
IO.print ("Radius? "); (Prompt)
double r = IO.read_double();
Circle c = new Circle (r);
IO.println("Area = " + c.area());
IO.println("Perimeter = " + c.perimeter());
}
java CreateValues
Radius? 2.5
stuff output
stuff output
class Circle
{
// Instance Variables
double radius;
// Constructor - Parameter acts like variable,
but returns a value.
public (Nothing Here) Circle (double x)
{
radius = x;
}
// Methods
public double area()
{
return (radius * radius * Math.PI);
}
public double perimeter ()
{
return (2 * radius * Math.PI);
}
{
// End Class
}
}
This Weeks Lab: Stepwise Refinement Challange.
Write code to compute surface area of culinder.
RefinementApp.java
Revise "Read CylinderDimensions"
APPLICATION ARCHITECTURE:
Global Procedural Application Architecture
DEF: The Global Procedural Appl Arch is the appl arch
in which -
1) All values which appear anywhere within the class are
declared local to the class, and hence are global to
all of the methods of the class.
2) All methods defined in the class are commands.
Sketch - The "Block Structure of this Application:
_______________________________
| |
| class |
| all variables here |
| ___________________ |
| | | |
| | Command | |
| |___________________| |
| ___________________ |
| | | |
| | Command | |
| |___________________| |
|_______________________________|
An operational procedure for writing an application
consistant with the Global Procedural Appl Arch
1) Sketch a main method which hypothesises the existance
of commands which do the deed (pseudo-code).
2) Add to one (1) a "data dictionary" which assimulates
variables with the concepts introduced in the main
method.
3) Refine the commands hypothesised in the main method
in such a way that they "fit in" with the "data
dictionary".
Once you have done this, or as your are doing it,
place the code where it belongs in the program text.
Example:
class _______
{
// Data Dictionary
2) stuff
stuff
// Main Method
1) stuff
stuff
// Refined Commands
3) stuff
stuff
Use Global Procedural Method to program "Line Of Squares"
Global Procedural Arch for "LineOfSquares"
We will be doing this for "Die"
1) The Main Method
static public void main (------)
{
readProblemParameters();
computeAreaOfOneSquare();
computeLengthOfSide();
computeNumberOfExternalEdges();
computePerimeter();
displayPerimeter();
}
2) Data Dictionary
static private double figureArea;
static private int nrSquares ;
static private double squareArea;
static private double sideLength;
static private int eeCount; (External Edge)
static private double perimeter ;
2) Refine the commands in the context of #1 and #2
static private void readProblemParemeters
{
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 = M(nrSquares *2) + 2;
}
static private void computePerimeter()
{
perimeter = eeCount * sideLength;
}
static private void displayPerimeter()
{
IO.println("Perimeter is " + perimeter);
}
1) As a relult of running...
Read Problem Parameters
The Variables:
figureArea & nrSquares were Bound
2)As a result of running:
computeAreaOfOneSource
The Variable:
squareArea was Bound
==========================================================+
123456789012345678921234567893123456789412345678951234567
|
|
|