Next: Tutorial 3 superfor loops and for loops, Previous: Tutorial 1 Your first program, Up: J.T.W. Tutorials [Contents][Index]
Question 4.2.1: The following code returns whether or not the current
parameter ch is a vowel. The parameter 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
Character.toUpperCase
function
to convert chars into uppercase chars
so that the code works equally well for isVowel('a')
and
isVowel('A')
. Study, compile and run the following code. Does it print
what you expected it to? If not, then fix the bug.
class
Scrabblebegin
function
boolean isVowel(char ch)begin
ch = Character.toUpperCase(ch);if
((ch == 'A')or
(ch == 'E')or
(ch == 'I')or
(ch == 'O')or
(ch == 'U'))then
return
true
;else
return
false
;end
beginMain
System.out.println(isVowel('a'));endMain
end
In the above code, note the difference between a = b
example:
ch = Character.toUpperCase(ch)
and a == b
example: ch ==
'A'
. The first is an assignment that sets a to be whatever the value
of b is, while the second is a question that says whether or not the
two arguments a and b are equal.
Note that later on in this tutorial you will learn that this is not
the way to compare two strings. Also note the use of the boolean
return type. This means that the return value is either true
or false
.
Question 4.2.2: By copying the pattern established by the above code,
write a function
isConsonant
which returns whether or not the given
argument is not a vowel. The easiest way to do this is to write
isVowel(ch) == false
which means: “ch is not a vowel”. You will also
need to ensure that the parameter ch is greater than or equal to ’A’
and less than or equal to ’Z’. Then test your code by calling
isConsonant
from the main
function
.
Question 4.2.3: By copying the pattern established in the following code:
function
int
countVowels(String word)begin
var
int result = 0;superfor
(var
int i=0to
i<word.length()-1)begin
var
char ch = word.charAt(i);if
(isVowel(ch))then
result = result + 1;end
return
result;end
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 word.charAt(i)
and word.length()
. The first of these
results the character at location in the string word given by the
value of i and the second of these returns the length of the string
word. In Tutorial 11 you will learn that these are called methods
which are different from 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
.
Question 4.2.4: Write a function
simpleScoreWord
that calls
countVowels
and countConsonants
to give a 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
.
Question 4.2.5: Write a function
advancedScoreLetter
that
returns the Advanced Score of a letter. Here is a breakdown of the
distribution of letters for the purpose of the calculation of the
Advanced Scores.
2 blank tiles (scoring 0 points) 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 2 points: D 4 tiles, G 3 tiles 3 points: B 2 tiles, C 2 tiles, M 2 tiles, P 2 tiles 4 points: F 2 tiles, H 2 tiles, V 2 tiles, W 2 tiles, Y 2 tiles 5 points: K 1 tiles 8 points: J 1 tiles, X 1 tiles 10 points: Q 1 tiles, Z 1 tiles
Then test your code by calling it from the main
function
.
Question 4.2.6: Write a function
advancedScoreWord
that
returns the 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
.
Question 4.2.7: Comparing strings. Amend the 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 == which you already know is how you compare the
following types that you know of so far: booleans, chars and
ints. Using == on strings compiles and runs but gives you the
incorrect result. The correct method to compare strings is to use the
equals method of the string class like so: word.equals("bugger")
which returns true
or false
, depending on whether or not the string
word currently holds the value "bugger".
Question 4.2.8: Change the advancedScoreWord
function
so it works
equally well with uppercase words and lowercase words. You will need
write to call either word.toUpperCase()
or word.toLowerCase()
and store the result in word.
Next: Tutorial 3 superfor loops and for loops, Previous: Tutorial 1 Your first program, Up: J.T.W. Tutorials [Contents][Index]