S.J.S. tutorial 2: if statements, for loops and strings
SECTION(2, Tutorial 2)
m5_question(BO(Question 2.1:) The following code returns
whether or not the current parameter TT(ch) is a vowel. The parameter
TT(ch) is of type char which is used to hold the components of a
string. That is to say, strings are built out of sequences of chars.
Also note the use of the CLSS(Character)TT(.toUpperCase) function to convert
chars into uppercase chars so that the code works equally well for
TT(isVowel(STRI('a'))) and TT(isVowel(STRI('A'))). Study, compile and
run the following code. Does it print what you expected it to? If
not, then fix the bug.
m4_begin_indent
NU()CLASS CLSS(Scrabble) EOL
BEGIN EOL
PD FUNCTION BOOLEAN FUNC(isVowel)(CHAR VARI(ch)) EOL
PD BEGIN EOL
PD PD ch = CHARACTER.toUpperCase(ch); EOL
PD PD IF ((ch == STRI('A')) OR (ch == STRI('E')) OR (ch == STRI('I')) OR (ch == STRI('O')) OR (ch == STRI('U'))) EOL
PD PD THEN RETURN TRUE; EOL
PD PD ELSE RETURN FALSE; EOL
PD END EOL
PD BEGIN_MAIN EOL
PD PD SYSTEM_OUT_PRINTLN(isVowel(STRI('a'))); EOL
PD END_MAIN EOL
END EOL
m4_end_indent
NU()In the above code, note the difference between TT(a = b) example:
TT(ch = CLSS(Character).toUpperCase(ch)) and TT(a == b) example: TT(ch ==
STRI('A')). The first is an EM(assignment) that sets TT(a) to be
whatever the value of TT(b) is, while the second is a EM(question)
that says whether or not the two arguments TT(a) and TT(b) are equal.
PP
NU()Note that later on in this tutorial you will learn that this is
EM(not) the way to compare two strings. Also note the use of the
EM(boolean) return type. This means that the return value is either
EM(true) or EM(false).
)
m5_question(BO(Question 2.2:) By copying the pattern established by
the above code, write a function TT(isConsonant) which returns whether
or not the given argument is not a vowel. The easiest way to do this
is to write TT(isVowel(ch) == false) which means: EM(``ch is not a
vowel''). You will also need to ensure that the parameter TT(ch) is
greater than or equal to TT(STRI('A')) and less than or equal to
TT(STRI('Z')). Then test your code by calling TT(isConsonant) from
the MAIN function. )
m5_question(BO(Question 2.3:) By copying the pattern
established in the following code:
m4_begin_indent
NU()FUNCTION INT FUNC(countVowels)(STRING VARI(word)) EOL
BEGIN EOL
PD VAR INT VARI(result) = NUMB(0); EOL
PD FUR (VAR INT VARI(i)=NUMB(0); LT(i,word.length()); i=i+NUMB(1)) EOL
PD BEGIN EOL
PD PD VAR CHAR ch = word.charAt(i); EOL
PD PD IF (isVowel(ch)) THEN result = result + NUMB(1); EOL
PD END EOL
PD RETURN result; EOL
END EOL
m4_end_indent
NU()write a function that counts the number of consonants in a word.
Note the use of the VAR keyword for defining variables that are local
to functions. Local variables are very much like parameters that were
introduced in the previous tutorial. In the above code, note the use
of TT(word.charAt(i)) and TT(word.length()). The first of these
results EM(the character at location in the string TT(word) given by
the value of TT(i)) and the second of these returns EM(the length of
the string TT(word)). In TUTE_LINKS you will learn that these are
called EM(methods) which are different from EM(functions) that
currently know how to write. Until we get to this tutorial and we are
ready to teach you how to write your own methods, you will only call
existing methods such as the above methods of the STRING class.
Then test your code by calling it from the MAIN function.)
m5_question(BO(Question 2.4:) Write a method
TT(simpleScoreWord) that calls TT(countVowels) and TT(countConsonants)
to give a EM(Simple Score) of a word. The Simple Score of a word is
the number of vowels in the word plus the number of consonants in the
word times ten. Then test your code by calling it from the MAIN
function.)
m5_question(BO(Question 2.5:) Write a method
TT(advancedScoreLetter) that returns the EM(Advanced Score) of a
letter. Here is a breakdown of the distribution of letters for the
purpose of the calculation of the Advanced Scores.
m4_undefine([F])
m4_undefine([S])
m4_begin_itemize
m4_item 2 blank tiles (scoring 0 points)
m4_item 1 point: E 12 tiles, A 9 tiles, I 9 tiles, O 8 tiles,
N 6 tiles, R 6 tiles, T 6 tiles, L 4 tiles, S 4 tiles, U 4 tiles
m4_item 2 points: D 4 tiles, G 3 tiles
m4_item 3 points: B 2 tiles, C 2 tiles, M 2 tiles, P 2 tiles
m4_item 4 points: F 2 tiles, H 2 tiles, V 2 tiles, W 2 tiles, Y 2 tiles
m4_item 5 points: K 1 tiles
m4_item 8 points: J 1 tiles, X 1 tiles
m4_item 10 points: Q 1 tiles, Z 1 tiles
m4_end_itemize
m4_define([F],KEYW(f))
m4_define([S],KEYW(s))
Then test your code by calling it from the MAIN function.)
m5_question(BO(Question 2.6:) Write a method TT(advancedScoreWord)
that returns the EM(Advanced Score) of a word. The Advanced Score of
a word is the sum of the Advanced Scores of each letter in the word.
If the word is eight letters long then you should add an extra, say,
50 points to the score. Then test your code by calling it from the
MAIN function.)
m5_question(BO(Question 2.7: Comparing strings.) Ammend the
TT(advancedScoreWord) function so that swear words get a score of
zero. For the purposes of this question you only need to think of
three swear-words to add to the code. In the interests of not
offending anyone, please keep your choice of swear words very tame.
When comparing strings it is a mistake to use TT(==) which you already
know is how you compare the following types that you know of so far:
booleans, chars and ints. Using TT(==) on strings compiles and runs
but gives you the incorrect result. The correct method to compare
strings is to use the TT(equals) method of the string class like so:
TT(word.equals(STRI("bugger"))) which returns true or false, depending
on whether or not the string TT(word) currently holds the value
TT(STRI("bugger")). )
m5_question(BO(Question 2.8:) Change the TT(advancedScoreWord)
function so it works equally well with uppercase words and lowercase
words. You will need write to call either TT(word.toUpperCase()) or
TT(word.toLowerCase()) and store the result in TT(word).)