CS1 Course Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
CS1 Course Site  
 
 
 
Wait , WAIT !!What's that word ? ( Class Notes ( without definitions ) )

Monday September 13 , 2000
 
Remark
As an activity to aquaint us with the creation and use of objects (i.e,  scripts) we will solve a collection of problems posed in the context of a ``shapes world.'' A conceptual entity consisting of Circles,  squares,  rectangles,  triangles and polygons- a realm in which interesting computations on shapes abound.
The circle class
We will model a circle in terms of one number,  its radius,  and the following programs:
  • new Circle (< double >) -- > < circle >
    • This program creates a new circle who's radius is the given number.
  • < circle > .radius () -- > < double >
    • compute the radius of the given circle
  • < circle > .diameter () -- > < double >
    • compute the diameter of the circle.
  • < circle > .area () -- > < double >
    • compute the area of the circle
  • < circle > .perimeter () -- > < double >
    • compute the perimeter of the circle.
  • < circle > .describe ()
    • describes the circle
  • < circle > .expand (< double >)
    • increases the radius of the circle by the given number.
  • < circle > .shrink (< double >)
    • decrease the radius of the circle by the given number.
Tasks
Suppose c and d have been bound to circles.
  1. Display the area of circle c.
    IO.println (c.area ());
  2. Declare a variable called Avg and bind it to the average value of the radii of the two circles
    double avg = (c.radius () + d.radius ()) / 2.0;
  3. Expand circle d by adding 10 to its radius
    d.expand (10.0);
  4. Display the area of a circle of radius 15. 

    Circle clara = new Circle (15);
    double areaOfClara = clara.area ();
    IO.println (areaOfClara);

Notes
Circle is a class
clara is an instance
IO is a class
15 is a ``primitive atomic type.''
area is an instance method.
println is a static method 

Problem
A circular garden of diameter 58.2 meters is surrouned by a 2 meter brick walkway.   What is the area of the walkway?
To solve this we will use Problem Decomposition.
The Java program
< CLASS > -- > WalkwayApp
< NEEDS > -- > import blue.shapes.   *;
< PROGRAM > -- >

/ /record the given information
double diameterOfGarden = 58.2;
double walkwayWidth = 2.0;
/ /create the garden
double radiusOfGarden = (diameterOfGarden / 2.0);
Circle garden = new Circle (radiusOfGarden);
/ /create the extended garden
double radiusOfXGarden = (radiusOfGarden + walkwayWidth);
Circle xgarden = new Circle (radiusOfXGarden);
/ /compute the area of the walkway
double walkWayArea = xgarden.area () - garden.area ();
/ /display result
IO.print ( ``The area is:   ``);
IO.print (walkWayArea);
Bindings
garden --- > o
xgarden -- > O