1. Procedures that compute/return results
Procedures that compute/return results
So far all the effect of our procedures has been to produce its effect by drawing
on the canvas. Sometimes it is useful to have procedures that take inputs and
compute some quantity as their result. We have seen examples of this in the
procedures cos(...) and sin(...) that take an angle as input and compute some
trigonometric result. Other examples include computing a student's GPA from homework
or exam scores, computing the balance in an account after a given number of years, etc.
To create a procedure that returns a value wee need to:
- decide what kind of value the procedure returns (an int, a double, etc.)
and replace void with that kind
- compute the result, store it in a Post-It note
and return the result
Adding penalty points
We modified the guessing game so that the player gets penalty points for wrong
answers. The penalty scheme depended on how far away the guess is from the secret
number. We chose the following penalty rule:
up to 2 units away --- 1.5 pt penalty
up to 4 units away --- 2.5 pt penalty
up to 6 units away --- 3.5 pt penalty
anything else --- 4.5 pt penalty
Procedure for computing absolute values
A simple subtraction is not enough to compute how far away the guess is from the
secret number, since we might get a negative value. We can use a procedure that
knows how to compute and absolute value of a given number.
Here is the description in English and its Java translation of the procedure for
computing an absolute value:
In English In Java
To compute the absolute value of a given number: double absValue( double n )
{
if the given number is 0 or above: if (n >= 0)
{
give back the given number return n;
}
otherwise: else
{
give back the negative of the given number return -n;
}
}
Computing the penalty points
Next we added a procedure for computing penalty points based on given rules. In the
guessing game the penalty points were determined by the distance between the secret
number and the player's guess.
In English In Java
To compute penalty points given guess, secret: double computePenalty( int guess, int secret )
{
compute difference between guess and secret double diff = absValue( guessNumber - secretNumber );
if difference is at most 2: if (diff <= 2)
{
give back 1.5 points return 1.5;
}
otherwise if difference is at most 4: else if (diff <= 4)
{
give back 2.5 points return 2.5;
}
otherwise if difference is at most 6: else if (diff <= 6)
{
give back 3.5 points return 3.5;
}
otherwise: (anything else) else
{
give back 4.5 points return 4.5;
}
}
Using the new procedures
Now that we have the new procedures we need to use them every time the player makes
an incorrect guess. We already have code that informs the player that the guess is
incorrect, so that seems to be a good place to add the new code:
void playGuessingGame( int low, int high, int maxTrials )
{
double hintX = canvas.getWidth() / 2;
double hintY = canvas.getHeight() / 2;
// int secret = (low + high) / 2;
int secret = canvas.getRandomInt( low, high ) / 2;
int guess = canvas.readInt( "Guess a number" );
int trials = 1;
double totalPenalty = 0;
while( guess != secret && trials < maxTrials )
{
canvas.clear();
if (guess < low || guess > high)
{
canvas.drawText( hintX, hintY, "Guess is out of range!!!", "red" );
}
else
{
// computing the new penalty and updating total
double curPenalty = computePenalty( guess, secret );
totalPenalty = totalPenalty + curPenalty;
// displaying the penalty to the player
canvas.drawText( hintX, hintY, "Total Penalty so far is " + totalPenalty + " points.", "red" );
}
canvas.sleep( 1 );
guess = canvas.readInt( "Guess a number" );
trials = trials + 1;
}
canvas.clear();
if (guess == secret)
{
canvas.drawText( hintX, hintY, "Congratulations, you win!!!", "blue" );
}
else
{
canvas.drawText( hintX, hintY, "Sorry, play again!!!", "blue" );
}
}