This week's project is to use the equivalent of a C++ switch construct in each of our languages. More precisely, here is a C++ function that, given an integer average, returns the corresponding letter grade (assuming 90-100 is an A, 80-89 is a B, etc.):
char letterGrade(int average)
{
switch (average / 10)
{
case 10: case 9:
return 'A';
case 8:
return 'B';
case 7:
return 'C';
case 6:
return 'D';
default:
return 'F';
}
}
Your project is to implement the equivalent function in Java, Ada, Clojure, and Ruby. You must also write a simple "driver program" to test your function/method in each language.
Ada. Where C++ and Java have a switch statement, Ada has a CASE statement, governed by these (simplified) productions:
CaseStmt ::= case Expression is
CaseAlternative ;
CaseAlternatives
OthersAlternative ;
end case
CaseAlternative ::= when ChoiceList => StmtList
CaseAlternatives ::= CaseAlternative ; CaseAlternatives | Ø
OthersAlternative::= when others => StmtList ;
ChoiceList ::= Choice MoreChoices
MoreChoices ::= |Choice MoreChoices | Ø
Choice ::= Expression | Range
Range ::= SimpleExpression..SimpleExpression
The effect is that when the Expression matches a Choice
in a CaseAlternative, then the StmtList associated
with that Choice is executed.
If it matches no Choice and an others choice is present,
then the StmtList associated with others is executed.
Note that the others choice must be the final choice.
Note also that all Choice values must be known at compile-time.
Clojure. In addition to its if function, Clojure has a cond function that offers a more convenient multi-branch behavior. This function is much more general than a switch or SELECT statement, allowing arbitrarily complex conditions to be tested in each "case". Its syntax is as follows:
CondFunction ::= (cond PairList FinalPair)
PairList ::= Condition Expression
MorePairs
MorePairs ::= Ø | Condition Expression
MorePairs
FinalPair ::= :else Expression | Ø
where Condition is an Expression that evaluates to
nil or non-nil.
The effect is that when the function
is called, the Conditions in each PairList are evaluated
sequentially until one evaluates to non-nil.
That Condition's associated Expression is then evaluated,
and the value it produces is the return-value of the cond function.
If present, the FinalPair beginning with :else
provides the equivalent of a C-family default: case.
Ruby. Simliar to Ada, Ruby has a case statement for multi-branch selection. The (simplified) syntax is as follows:
CaseStmt ::= case Expression
when When_args Then Statement
{when When_args Then Statement}
[else
Statement]
end
Then ::= '\n' | then | Ø
where Expression will be tested for a form of equality
with each of When_args, and Statement is the
return value
when that branch is taken.
A Ruby case statement is powerful in ways that this simple exercise will not show you. For some great examples, see this post at Skorks.com.
Turn in. Using an approach like what we did in the lab, make a single script file named proj03-results. Then submit your project by copying that single file into your personal folder in /home/cs/214/current/:
cp proj03-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name.
The grader will access and grade your project results from there,
using the criteria from
this grade sheet.
Calvin > CS > 214 > Projects > 03