P Dunn's Super CS1 Site

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
P Dunn's Super CS1 Site  
 
 
 
Class Notes

Wednesday September 6 , 2000
 
Synopsis
This class covered what a binding and environment is.   This helps in keeping straight of what the computer defines and what values it gets as the program is executed.   This helps in debugging a program when need be.  

Also powerful ideas were covered such as problem decomposition (defined below) and it's application thereof.

    Input Stream:   | Environment
    ---------------------------
    5 9 | n1 - > 5
    | n2 - > 9
    | sum - > 14
 

Output Stream:   The sum is 14  

Definition   A BINDING is an association from a name to an object (value).

Definition   An ENVIRONMENT is a set of bindings.

Problem A necklace is constructed from 20 Blue Beads and 14 Red Beads.   Each blue bead cost $ .22 while each red bead cost $ .33.   The cost of the thread is $ .14 per inch and 24 inches are required.   What is the total cost of the necklace?  

Definition   PROBLEM DECOMPOSITION is the problem solving strategy of decomposing a problem into a set of sub-problems,  solving each of the sub-problems,  and then composing a solution to the original problem from the solutions to the sub-problems.

Informal Solution Description To determine the total material cost (ie.   to solve the problem) we need only use Problem Decomposition and solve the following sub-problems;

  • Find or determine the cost of the blue beads.
  • Find or determine the cost of the red beads.
  • Find the cost of the thread.
 

Sometimes problem decomposition is applied at many levels in solving a problem.   In this case we can apply it to solving each of the sub-problems.

  • We can the find the blue bead cost by
    • Determining the # of blue beads
    • Determining the cost each blue bead
    • Multiplying the # of blue beads times the cost of each blue bead
  • We can the find the red bead cost by
    • Determining the # of red beads
    • Determining the cost each red bead
    • Multiplying the # of red beads times the cost of each red bead
  • We can the find the thread cost by
    • Determining the price per inch of thread
    • Determining the length of thread needed
    • Multiplying the price per inch times the length of the thread
 

Note that each of the problems labelled in the sub-points are trivial.  

REMARK We conceived of the solution in a ``top-down'' manner.  

REMARK When coding the solution we will have to type the Java in a ``bottom-up'' manner.  

REMARK Whenever you write programs in this class you must artfully name all values - within reason.  

- > NecklaceApp  

- >  

  / /Determine the cost of the Blue Beads
  double costPerBlueBead = 0.22;
  int nrBlueBeads = 20;
  double blueBeadCost = costPerBlueBead * nrBlueBeads;
 
  / /Determine the cost of the Red Beads
  double costPerRedBead = 0.33;
  int nrRedBeads = 14;
  double redBeadCost = costPerRedBead * nrRedBeads;
 
  / /Determine the cost of the thread
  double costPerInchOfThread = 0.14;
  double lengthOfThread = 24.0;
  double costOfThread = costPerInchOfThread * lengthOfThread;
 
  / /Solve the original problem by making use of the solutions
  / /to the main sub-problems
  double totalCost = blueBeadCost + redBeadCost + costOfThread;
 
  / /Display the result
  IO.print ( ``total cost of materials is:   ``);
  IO.print (totalCost);
  IO.println ();
 

The Environment - (bindings)
costPerBlueBead - > 0.22
nrBlueBeads - > 20
blueBeadCost - > 4.4
costPerRedBead - > 0.33
nrRedBeads - > 14
redBeadCost - > 4.62
costPerInchOfThread - > 0.14
lengthOfThread - > 24.0
costOfThread - > 3.36
totalCost - > 4.4 + 4.62 + 3.36 = 12.38