(***********************************************************************
Mathematica-Compatible Notebook
This notebook can be used on any computer system with Mathematica 3.0,
MathReader 3.0, or any compatible application. The data for the notebook
starts with the line of stars above.
To get the notebook into a Mathematica-compatible application, do one of
the following:
* Save the data starting with the line of stars above into a file
with a name ending in .nb, then open the file inside the application;
* Copy the data starting with the line of stars above to the
clipboard, then use the Paste menu command inside the application.
Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode. Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).
NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.
For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
web: http://www.wolfram.com
email: info@wolfram.com
phone: +1-217-398-0700 (U.S.)
Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)
(*CacheID: 232*)
(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[ 27423, 830]*)
(*NotebookOutlinePosition[ 28509, 865]*)
(* CellTagsIndexPosition[ 28465, 861]*)
(*WindowFrame->Normal*)
Notebook[{
Cell[CellGroupData[{
Cell[TextData["24. Object-Oriented Programming"], "Title",
Evaluatable->False,
AspectRatioFixed->True],
Cell["Last revision: April 15 1998", "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"We continue the study of contexts and packages in the context of \
object-oriented programming, the theme of this section."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" operations/structures introduced: \n",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox[" ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["Object-Oriented Programming",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"]
}], "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Basic Object-Oriented Programming"], "Section",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Object-oriented programming",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" refers to a particular style of programming in which programs are written \
in a way which permits simple, planned expansions and additions by later \
programmers. The governing principle is that some functions (the \"public\" \
functions) defined by one programmer are meant to be used by further \
programmers, while other functions and data (the \"private\" ones) are not \
meant to be used by someone else, since the functions do very specific tasks \
with little general value. This style of programming also encourages \
programmers to bind together functions and data into a structure called an \
object; this structure encapsulates all \"public\" and \"private\" data and \
functions and controls access to any of the functions or data by external \
agents. (Object-oriented programming languages, such as C++, also provide \
ways for programmers to use functions without knowing their exact names, \
using a programming technique called \"polymorphism\", but this technique is \
beyond the scope of this course.) While ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" is not an object-oriented programming language per se, we can mimic part \
of this style of programming by using contexts and packages. We will not \
form true objects in the sense of object-oriented programming---you should \
take a course in object-oriented programming to do so---but we will consider \
the separation of functions and data into the public and the private.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"The method we will use is to decide, first, what data and functions should \
be \"public\", common property of any user or programmer using our code. All \
other data and functions will be deemed \"private\", and we will enforce this \
distinction by placing private data and functions in a special context. We \
will have two contexts, then, one overarching context which will contain all \
of our data and functions associated to a particular programming problem, and \
one context within the first, a private context, which contains those \
functions and data which we want to prevent others from modifying."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Example: A Team Roster"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"As an example, we'll return to our team roster problem. Recall the \
following solution to the problem of keeping a list of names, together with \
the total number of individuals on the team."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Clear[sportsNames,totalNames,addName,deleteName]\nsportsNames = {};\n\
totalNames = 0;\naddName[s_] := \n\t\t\tModule[{},\n\t (* uses \
sportsNames, totalNames *)\n\t sportsNames = Union[sportsNames,{s}];\n\t \
totalNames = Length[sportsNames];\n\t ]\ndeleteName[s_] :=\n\t \
Module[{},\n\t \t (* uses sportsNames, totalNames *)\n\t \t sportsNames = \
Complement[sportsNames,{s}];\n\t \t totalNames = Length[sportsNames];\n\t \
]\n\t "], "Input",
AspectRatioFixed->True],
Cell[TextData["addName[\"Magic\"]\naddName[\"Shaq\"]\ntotalNames"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"How could we improve on this solution? First, we notice that while we \
would want others to use the functions \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["addName[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["deleteName[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", we would ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["not ",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" want others to have the ability to change \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["sportsNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" independently, if only because they might add a name and forget to \
increment \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". Hence we'll need to make \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["sportsNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" private data, while \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["addName",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["deleteName",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" can be public functions. Similarly, since we don't want others to \
initialize our data themselves, we'll provide a public function \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["sportsInit[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" which they can execute, in which we'll perform the initialization. \
Finally, we'll provide a way for a user to request the number of names on the \
roster, using a public function \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["getNumber[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", and a way for a user to request the roster list, using the public \
function \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["getPlayerList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Before we get started, let's remove all previous definitions so that we \
don't leave any names in the context \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Global`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Remove[addName,deleteName,sportsNames,totalNames]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"Below we'll give a package solution, followed by an analysis of the details \
of its construction."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["BeginPackage[\"Sports`\"];"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"sportsInit::usage = \"sportsInit[] clears the current team roster\";\n\n\
addName::usage = \"addName[string] adds a name to the team roster\";\n\n\
deleteName::usage = \"deleteName[string] deletes a name from the team \
roster\";\n\ngetNumber::usage = \"getNumber[] returns the number of players \
currently on the team roster\";\n\ngetPlayerList::usage = \"getPlayerList[] \
returns a list of the players on the team roster\";"], "Input",
AspectRatioFixed->True],
Cell[TextData["Begin[\"Sports`Private`\"];"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"sportsInit[] := (sportsNames = {}; totalNames = 0;);\naddName[s_String] := \n\
\t\t\tModule[{},\n\t (* uses sportsNames, totalNames *)\n\t \
sportsNames = Union[sportsNames,{s}];\n\t totalNames = \
Length[sportsNames];\n\t ];\ndeleteName[s_String] :=\n\t Module[{},\n\t \t \
(* uses sportsNames, totalNames *)\n\t \t sportsNames = \
Complement[sportsNames,{s}];\n\t \t totalNames = Length[sportsNames];\n\t \
];\ngetNumber[] := totalNames;\ngetPlayerList[] := sportsNames;\n\n\
sportsInit[];\n\t "], "Input",
AspectRatioFixed->True],
Cell[TextData["End[];"], "Input",
AspectRatioFixed->True],
Cell[TextData["EndPackage[];"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Let's walk through this solution. First, using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["BeginPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", we set up a separate context for our functions and data, so that we \
won't damage any other functions or data with the same names. At the end, \
\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["EndPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" will add \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" to the context search path, so we'll have access to any functions and \
data in the context \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". We then provide usage lines for each public function, a",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox[" very important step",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
". These usage lines important because they mention each name which we'll \
want to be public, thereby establishing those names as names inside the \
context \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". Next, we enter a new context, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`Private`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". At this stage any new names declared will be in this separate \
context, while any names already declared will still be public, in \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". The effect is that \"sportsNames\" is a private name, with the long \
form \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`Private`sportsNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", while \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["getNumber[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" is a public name, with the long form \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`getNumber[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". We make the requisite definitions, and then ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["execute",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["sportsInit[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", so the initialization will happen whether or not the user remembers \
explicitly to do so. Then \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["End[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" takes us out of the context \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`Private`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", without adding that context to the context search path, and, finally, \
\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["EndPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" ends the context \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", adding \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" to the context search path.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Let's examine how our package works."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["getNumber[]"], "Input",
AspectRatioFixed->True],
Cell[TextData["addName[\"Shaq\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData["getPlayerList[]"], "Input",
AspectRatioFixed->True],
Cell[TextData["getNumber[]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"Now suppose we want to alter the private data. We can't seem to get to \
it!"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["totalNames"], "Input",
AspectRatioFixed->True],
Cell[TextData["sportsNames"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Why is this? Because the names are part of \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`Private`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", which is not in the context search path. It is true that by \
specifying \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`Private`totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", we can get to the actual definition, and therefore change the value, \
but this causes an appropriate burden on the new programmer. If the package \
is to be circumvented in its intent, there are still ways to alter the data \
directly, but the new programmer will have to go to relatively great lengths \
to do so. (Incidentally, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" provides certain attributes, such as \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Protected",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadProtected",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Locked",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", which prevent changes to definition, but we won't use them, under the \
premise that security levels need not be high in our environment!)",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Designing Programs Using Packages"], "Section",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
"Object-oriented concepts, implemented using packages, are therefore \
desirable components of any ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" code. These object-oriented concepts dovetail nicely with bottom-up \
analysis, so in designing a package, it is best to attempt both. Decide what \
data, and what functions performing simple operations on this data, will be \
needed before you treat the larger problem. While you do so, decide which \
functions and data should be private and which should be public. "
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"The standard format of code for packages is as follows. First, enter a \
\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["BeginPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" statement. Then, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["mention",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" each and every name which you will want to be public in your package. \
One way to do so is to provide usage lines for each and every item of data \
and function which the users will use; another way is just to enter the name \
followed by a semicolon. Next, use a \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Begin[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" statement with a special context name, usually including the name \
\"Private`\". Proceed to enter the definition of each function, public or \
not, keeping in mind that all new names will be part of the private context. \
Finally, exit the private context using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["End[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and finish the package with \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["EndPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". To begin, mimic the example above.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["IMPORTANT",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
": Realize that in programming a package, the order of execution---and the \
separation of some lines into their own cells, such as \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["BeginPackage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", for example---is crucial. If you write a package, execute its cells, \
and then go back to make changes, simply executing the cells you've changed \
may not perform the operation you desire. It may be best to execute \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Remove[\"*\"]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", which removes all user-defined definitions in the \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Global`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" context, and then \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Remove[\"Context`*\"]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[",\" substituting for \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Context",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" each of the contexts you are using, separately. Finally, re-execute \
each cell, in order. At this point you will be in a good position to debug \
your program. (At worst, you may need to save your file, restart ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[", and read the notebook back in.)",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Another Example"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"We wish to design a package which will keep track of many grades of equal \
weight in a class. The package should have functions which permit (a) \
initialization, (b) entering in a grade, (c) removing a grade, (d) printing \
out current grades, (e) computing the current average, and (f) determining, \
given the grades already entered, what average grade is necessary on the \
remaining assignments in order that the final average be a 60, 70, 80, or a \
90. Just as in the example above, we will have private data: the list of \
grades. We will also have public functions, namely, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["gradeInit[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["addGrade[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["deleteGrade[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["listGrades[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["findAverage[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["whatDoINeed[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["BeginPackage[\"Grades`\"];"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"gradeInit::usage = \"gradeInit[] sets the number of current grades to 0\";\n\
\naddGrade::usage = \"addGrade[n] adds a numeric grade n to the list of \
grades\";\n\ndeleteGrade::usage = \"deleteGrade[n] deletes a numeric grade n \
from the list\";\n\nlistGrades::usage = \"listGrades[] lists the grades \
currently entered\";\n\nfindAverage::usage = \"findAverage[] determines the \
average of the current grades\";\n\nwhatDoINeed::usage = \"whatDoINeed[n] \
determines, given n, the total number of grades in the course, the grades \
necessary to end the course with an average of 60, 70, 80, and 90\";\n"],
"Input",
AspectRatioFixed->True],
Cell[TextData["Begin[\"Grades`Private`\"];"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"gradeInit[] := (grades={};);\n\naddGrade[n_] := (grades = \
Append[grades,n];);\n\ndeleteGrade[n_] := Module[{a},\n\t\t\ta = \
Position[grades,n];\n\t\t\tIf[Length[a]>=1,\n\t\t\t grades = \
Delete[grades,Take[a,1]],\n\t\t\t Print[\"Grade not found\\n\"]];\n\t\t\t];\
\n\t\t\t\nlistGrades[] := grades;\n\nfindAverage[] := If[Length[grades]>0,\n\t\
\t Apply[Plus,grades]/Length[grades],\n\t\t Print[\"No grades\\n\"]];\n\t\t\n\
whatDoINeed[n_] := Module[{a,b,c},\n\t\t\tWhich[\n\t\t\t Length[grades]<1,\n\
\t\t\t {60,70,80,90},\n\t\t\t Length[grades]=n,\n\t \
Print[\"Final average already determined: \",\n\t findAverage[]];\n\t \
]];\n\t \ngradeInit[];"], "Input",
AspectRatioFixed->True],
Cell[TextData["End[];"], "Input",
AspectRatioFixed->True],
Cell[TextData["EndPackage[];"], "Input",
AspectRatioFixed->True],
Cell[TextData["Here are some sample user commands:"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"addGrade[90]; addGrade[90]; addGrade[60];\nwhatDoINeed[4]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"The student cannot attain a 90 average, given one more grade, but could \
attain an 80 average by earning an 80 on the next test."], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Closed]]
}, Open ]]
},
FrontEndVersion->"Macintosh 3.0",
ScreenRectangle->{{0, 832}, {0, 604}},
WindowToolbars->{},
WindowSize->{520, 509},
WindowMargins->{{108, Automatic}, {30, Automatic}},
PrintingCopies->1,
PrintingPageRange->{1, Automatic},
PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}},
ShowCellLabel->True,
ShowCellTags->False,
RenderingOptions->{"ObjectDithering"->True,
"RasterDithering"->False},
MacintoshSystemPageSetup->"\<\
00<0004/0B`000003809T?o>old"
]
(***********************************************************************
Cached data follows. If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file. The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[1731, 51, 106, 2, 136, "Title",
Evaluatable->False],
Cell[1840, 55, 98, 2, 26, "SmallText",
Evaluatable->False],
Cell[1941, 59, 197, 4, 46, "Text",
Evaluatable->False],
Cell[2141, 65, 535, 19, 40, "SmallText",
Evaluatable->False],
Cell[CellGroupData[{
Cell[2701, 88, 110, 2, 50, "Section",
Evaluatable->False],
Cell[2814, 92, 1850, 36, 276, "Text",
Evaluatable->False],
Cell[4667, 130, 686, 10, 77, "Text",
Evaluatable->False],
Cell[CellGroupData[{
Cell[5378, 144, 103, 2, 42, "Subsection",
Evaluatable->False],
Cell[5484, 148, 267, 5, 38, "Text",
Evaluatable->False],
Cell[5754, 155, 510, 8, 170, "Input"],
Cell[6267, 165, 102, 1, 40, "Input"],
Cell[6372, 168, 3165, 108, 116, "Text",
Evaluatable->False],
Cell[9540, 278, 431, 15, 25, "Text",
Evaluatable->False],
Cell[9974, 295, 102, 1, 22, "Input"],
Cell[10079, 298, 173, 4, 25, "Text",
Evaluatable->False],
Cell[10255, 304, 79, 1, 22, "Input"],
Cell[10337, 307, 481, 7, 120, "Input"],
Cell[10821, 316, 80, 1, 22, "Input"],
Cell[10904, 319, 572, 9, 190, "Input"],
Cell[11479, 330, 59, 1, 22, "Input"],
Cell[11541, 333, 66, 1, 22, "Input"],
Cell[11610, 336, 4478, 148, 168, "Text",
Evaluatable->False],
Cell[16091, 486, 110, 2, 25, "Text",
Evaluatable->False],
Cell[16204, 490, 64, 1, 22, "Input"],
Cell[16271, 493, 70, 1, 22, "Input"],
Cell[16344, 496, 68, 1, 22, "Input"],
Cell[16415, 499, 64, 1, 22, "Input"],
Cell[16482, 502, 152, 4, 25, "Text",
Evaluatable->False],
Cell[16637, 508, 63, 1, 22, "Input"],
Cell[16703, 511, 64, 1, 22, "Input"],
Cell[16770, 514, 1867, 57, 90, "Text",
Evaluatable->False]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[18686, 577, 110, 2, 30, "Section",
Evaluatable->False],
Cell[18799, 581, 609, 12, 64, "Text",
Evaluatable->False],
Cell[19411, 595, 1719, 51, 90, "Text",
Evaluatable->False],
Cell[21133, 648, 2013, 60, 103, "Text",
Evaluatable->False],
Cell[CellGroupData[{
Cell[23171, 712, 95, 2, 42, "Subsection",
Evaluatable->False],
Cell[23269, 716, 1822, 56, 90, "Text",
Evaluatable->False],
Cell[25094, 774, 79, 1, 22, "Input"],
Cell[25176, 777, 662, 10, 150, "Input"],
Cell[25841, 789, 80, 1, 22, "Input"],
Cell[25924, 792, 895, 13, 320, "Input"],
Cell[26822, 807, 59, 1, 22, "Input"],
Cell[26884, 810, 66, 1, 22, "Input"],
Cell[26953, 813, 109, 2, 25, "Text",
Evaluatable->False],
Cell[27065, 817, 111, 2, 42, "Input"],
Cell[27179, 821, 204, 4, 46, "Text",
Evaluatable->False]
}, Closed]]
}, Closed]]
}, Open ]]
}
]
*)
(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)