GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Tutorial 12 Tutorial 13 Tutorial 14 Tutorial 15
Tutorial 16 Tutorial 17 Tutorial 18 Using Emacs Download Links


J.T.W. tutorial 2: if statements, for loops and strings


§ 2 Tutorial 2

Question 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 Scrabble 
begin 
   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 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 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=0 to 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 2.4: Write a method 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 2.5: Write a method 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.

Then test your code by calling it from the main function.

Question 2.6: Write a method 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 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 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.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Mon 15 Aug 2022 11:24:51 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2022 Davin Pearson.
Please report any broken links to