P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Monday , September 11 , 2000
 
Synopsis
This lecture covered another powerful idea of problem solving called Goal Directed Planning which is defined below and how it can be used to solve programming problems.  

Also Java's Math class was covered as well as simple arithmetic in Java.

  Another Problem - Goal Directed Planning  

The squares in the following figure (not recreated here - only in paper notes) which is 5 squares high and 5 squares wide with the start being 1 square on top and expanded by 1 in each row until 5 are across and 5 are high.   What is the permiter of the figure?  

REMARK
This problem can readily be solved by application of the problem solving strategy known as ``Goal Directed Planning''.  

REMARK
Goal Directed Planning is a ``procedural'' variant of problem decomposition,  which we discussed in the September 6 th class.  

Definition   GOAL DIRECTED PLANNING is the problem solving strategy of...

  1. Identifying a goal.
  2. Determining the information needed to achiveve the goal.
  3. Establish as sub-goals the problems of securing the information needed to achieve the goal.
  4. ``Recursively'' applying the same method to the problems established as sub-goals.

 

Solving the permiter problem by means of Goal Directed Planning.

  • To determine the perimeter of the figure you need to know:
    • The length of a small square side
    • The number of such sides
  • To determine the length of a small square side we need to know:
    • The area of the square
  • To determine the area of one small square
    • The # of small squares
    • The total area of the figure
 

An approach to writing the program.  

Working in a ``bottom-up'' fashion either

  • Record an item of information,  or
  • Compute a value.
thus...
(In a bottom-up fashion)
  • Record the total area
  • Record the number of small squares
  • Computing the area of a small square
  • Compute the length of one side
  • Record # of small sides around the perimeter
  • Compute the perimeter
 

In Java  

 

  / /Compute the perimeter
  double totalArea = 187.32;
  int numSmallSquares = 15;
  double squareArea = totalArea /numSmallSquares;
  double sideLength = Math.sqrt (squareArea);
  int numExternalSides = 20;
  double perimeter = sideLength * numExternalSides;
 
  / /Display
  IO.println ( ``The perimeter is'' + perimeter + ``square units'');
 

More on the Math class
(1) Form:   Math.sqrt (< double >) - > < double >
Meaning:   Compute the square root of the permiter.
ex:
double value = Math.sqrt (16.0);
value - > 4.0;  

(2) Form:   Math.PI - > < double >
Meaning:   Reference the classic greek constant - 3.14 ..
ex:
double r = 5.8;
double a = r * r * Math.PI;  / /the area of a circle of radius 5.8  

(3) Form:   Math.pow (< double >,  < double >) - > < double >
Meaning:   Raise the first value to the power of the second
ex:
Math.pow (3.0,  2) - > 9.0
Math.pow (3,  4) - > 81.0
Legal:   Math.pow (1000,  1 / 2.0) - > cube root of a 1000 = 10  

Note on arithmetic
< int > + < int > - > < int >
< double > + < double > - > < double >
< int > + < double > - > < double >
similarly for * - /
ex:
IO.println (5 / 2) - > 2
IO.println (5.0 / 2.0