
Problem Solving Patterns Programming Principles and Problem Solving Patterns CS1 of CSJS

Goal Directed Planning

Metatext on
Goal Directed Planing


Goal directed planning is a "procedurally flavored" form of the problem solving strategy of problem decomposition.
Problem
Perimeter of the Figure


The squares in the following figure are congruent. Their collective area is187.32units. What is the perimeter of the figure?
Definition of
goal directed planning


Goal directed planning
is the problem solving strategy of
 identifying a goal (initially the "ultimate" goal)
 determining what information is required to achieve that goal
 establishing as subgoals the problems of securing the information required to achieve the ultimate goal
 "recursively" applying this same approach to the various subgoals which are nontrivial.
Note
an informal solution the Necklace Problem


How might one proceed to solve the Perimeter Problem? Perhaps by working backwards from the goal to established facts!That is, by proceding along a "solution path" which is created by means of a goal directed planning.
 To determine the figure's perimeter, we need to know the length of each external line segment as well as the number of external line segments.
 To determine the number of external line segments, we will count.
 We can determine the length of each external line segment, if we know the area of each small square.
 We can determine the area of each small square from the total area of the figure and the number of small squares.
 To determine the area of the figure, we recall the problem statement.
 To determine the number of small squares, we count.
Note
a programming approach for the problem


We can construct a program to solve the problem by simply recording an observation or expressing a computation for each of the "subproblems" identified in the goal directed planning session. In doing so, we will work, roughly speaking, from the facts towards the goal. That is, we will
 Record the number of small squares.
 Record the total area of the figure.
 Compute the area of one small square.
 Compute the area of one small square.
 Record the number of external line segments.
 Compute the figure's perimenter.
Java program
PerimeterOfFigureApp


Program which features problem decomposition. 

// REQUIRED PACKAGES
import blue.io.*;
// APPLICATION CLASS
class PerimeterOfFigureApp
{
static public void main (String args[])
{
// Solve the problem
int nrSmallSquares = 15;
double totalArea = 187.32;
double smallSquareArea = ( totalArea / nrSmallSquares );
double smallSquareSide = Math.sqrt(smallSquareArea);
int nrExternalSegments = 20;
double perimeter = ( smallSquareSide * nrExternalSegments );
// Print the result
IO.print("The perimeter of the figure is ");
IO.print(perimeter);
IO.println(" units.");
}
}
Java specification of some
java.lang.Math functionality


The
Math
class of the java.lang package provides lots of functionality for performing numeric processing, only a very small fraction of which is presented at this time. 


Math.PI
This expression denotes the classic Greek constant defined as the ratio of the circumference of a circle to its diameter.

Math.sqrt(<double>
)><double>
Return the square root of the given number.

Math.pow(<double>
,<double>
)><double>
Return the first number raised to the power of the second number.
Example
numeric processing


In the program previous program we employed
Math.sqrt(smallSquareArea)
to compute the square root of the small square area. We might write
Math.PI * Math.pow(15,2)
to compute the area of a circle of radius15.
It is useful to name constants in certain situations. The naming of domain specific values and various classic values tends to enhance program integrity, to promote readability, and to facilitate maintenance tasks.
Java specification of
named constants


A constant can be viewed as a static variable of some type which cannot be rebound once it is introduced. 

The following sort of statement can be placed just prior to the definition of the first method in a class.

staticfinal<Type>
Identifier=<Expression>
The variable introduced is bound to the value of the expression in such a way that it cannot be rebound to another value.
Java program
AreaOfCircleApp


Imagine that you know nothing about the java.lang.Math class. The following program illustrates how you might name your own constant called PI . The program simply reads a real number corresponding to the diameter of a circle and displays the area of the circle. 

// REQUIRED PACKAGES
import blue.io.*;
// APPLICATION CLASS
class AreaOfCircleApp
{
static final double PI = 3.14;
static public void main (String args[])
{
// Read the diameter
IO.print("Please enter the diameter: ");
double diameter = IO.read_double();
// Compute and print the area
double radius = diameter / 2.0;
double area = PI * ( radius * radius );
IO.print("The area is: ");
IO.print(area);
IO.println(" square units.");
}
}
Note
typing convention for constants


Constants are conventionally written in all capital letters.


