



|
|
Byron's CSC212 Web Site
|
Class Notes
Wednesday September 6 , 2000
|
|
|
Programming Variables and Bindings
Class Notes --
Wednesday September 6 , 2000
CSC212 - September 6, 2000
======================================
Lecture Topic: PROGRAMMING VARIABLES & BINDINGS
DEF: A VARIABLE is a name (Identifier) which represents
some concept and which may denote a value.
Ex: temp = tmperature
Suppose this represents the current temperature.
Ex: classCount = 140;
Remark - In Java all variables must be introduced by
specifying the type of value they can denote.
DEF: A VARIABLE DECLARATION is the introduction of a
variable in a program which specifies the type of
value the variable can denote.
Specification of Variable Declaration
<Type> = <Identifiable>
Ex: int n1 = IO.read_int();
a Variable Declaration
Bindings
_____________________________________
Input Stream | Environment
|
5, 9 | n1 -> 5
_________________| n2 -> 9
Output Stream | sum -> 14
|
The Sum is 14 |
DEF: BINDING is an association from a name
to an object (object).
DEF: ENVIRONMENT is a set of bindings (objects).
DEF: PROBLEM DECOMPOSITION is the problem solving
strategy of decomposing a problem into a set
of sub-problems and then composing a solution
to the original problem from the solutions to
the sub-problems.
Problem: Necklace Problem
A necklace is constructed from 20 Blue Beads and 14
Red Beads. Each Blue bead costs $.22 while each Red
bead costs $.33.
The cost of the thread is $.14 per inch, and 24 inches
are required for each necklace.
What is the total cost of the materials for the
necklace?
Application of Problem Decomposition
Separate problerm into 3 parts.
Cost of Red Beads.
Cost of Blue beads.
Cost of Thread.
To determine the total material cost (ie to solve the
problem) we need only use problem decomposition and
solve the following subproblems.
Find the cost of the Blue beads.
Find the cost of the Red beads.
Find the cost of the Thread.
Sometimes problem decompositon is applied at many
levels in solving a problem. In this case, we can
apply it to solving each of the subproblems.
We can find the blue bead cost by
1. Defining the number of Blue Beads
2. Determining the cost of each Blue bead.
We can find the Red bead cost by
1. Defining the number of Red beads.
2. Determining the cost of each Red bead.
We can find the Thread cost by
1. Defining the price per inch of thread.
2. Determining the length of the Thread.
Note that each of the numbered problems are "Trivial".
It is now time to write the program.
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 you must
artfully name all values - within reason.
<?class> The NecklaceApp
<?Program>
// Determine the cost of the blue beads.
// Variables - begins with lower case
// int - Integer number
// double - non integer number
// Determine the cost of the blue beads
int nrBlueBeads = 20;
double costPerBlueBead = 0.20;
double blueBeadCost =
nrBlueBeads * costPerBlueBead;
// Determine the cost of the red beads
int nrRedBeads = 14;
double costPerRedBead = 0.33;
double redBeadCost =
nrRedBeads * costPerRedBead;
// Determine the cost of the thread.
double lengthOfThread = 24.0;
double costPerInchOfThread = 0.14;
double cost of thread =
lengthOfThread * costPerInchOfThread;
// Solve the original problem by making use of
// the solution to the three main subproblems.
double totalCost = blueBeadCost +
redBeadCost + costOfThread;
// Display the Result
IO.print("Total cost of material is ");
IO.print(totalCost);
IO.println();
Bindings:
nrBlueBeads -> 20
costPerBlueBead -> 0.22
blueBeadCost -> 4.40
nrRedBeads -> 14
costPerRedBead -> 0.33
redBeadCost -> 4.62
lengthOfThread -> 24.0
costPerInchOfThread -> 0.14
costOfThread -> 3.36
totalCost -> 12.38
Some things to consider doing ...
Xemacs commands:
Go to "CS1 @ Oswego" site, click on "documentation"
and find the way to the emacs command summary.
Print the 8 page manual for your use,
one page at a time
Put it in your notebook, and bring it to Lab.
=========================================================
123456789112345678921234567893123456789412345678951234567
|
|
|