Computer Science Division 3rd Level Students- CS
Department of Mathematics Course: COMP 301 (Java)
Faculty of Science Date: November 8th, 2021
Sheet 4
Aims:
At the end of this lab, student should get knowledge the following:
• Dealing with String methods.
Exercise 1:
• Using the predefined methods in class String; write the following methods:
a. secondIndexOf that takes 2 parameters: a strings s and a character c, then returns the position
of the second occurrence of c in s (if found) or ‒1
(if not found)
For example: secondIndexOf (“abcda”,‘a’) = 4,
secondIndexOf (“abcda”,‘b’) = ‒1 , secondIndexOf (“abc”,‘x’) = ‒1
b. countChar that takes 2 parameters: a strings s and a character c, then count how many times c
appears in s.
For example: countChar (“abcda”,‘a’) = 2, countChar (“abcda”,‘z’) = 0
Exercise 2:
• Using the predefined methods in class String; write the following methods:
a. longestMString that takes an array of strings s as a parameter, then returns which string in the
array containing max occurrences of letter ‘m’ within it.
b. trimString that takes a strings s as a parameter, then returns a new string delete any trailing spaces
in it.
For example: trimString (“abcda ”) = “abcda”, trimString (“abc de”) = “abc de”
c. palindrome that takes a strings s as a parameter, then tests whether s
is palindrome or not (i.e. reading it from left to right is the same as reading
if from right to left).
For example: palindrome (“radar”) = true , palindrome (“reader”) = false
d. getFileExtension that takes a strings s as a parameter represents a file name, then returns the
extension of the file name (if found) or “none” (if not found).
For example: getFileExtension (“[Link]”) = “java”, getFileExtension (“xyz”) = “none”
• Write a class Program that contains all of the above methods beside a main method
The main method should read from the user input strings, then test their properties by calling the
above methods.
1|Page
Exercise 3:
• Using the predefined methods in class String; write the following methods:
a) mostFrequentString that takes an array of Strings arr as a parameter then returns the string
which appears more frequent in the array (no matter it is in small or capital letters). If more
than one String have the same frequency then return the first one of them.
For example: String arr = {“Java” , “C++” , “Paython” , “java”,“c”, “pascal”}
mostFrequentString (arr) = “java”
b) longestString that takes an array of strings s as a parameter then returns the string with the
longest length within this array.
c) reverseNumber that takes any number as a parameter then reverses its digits.
For example. reverseNumber (123) = 321, reverseNumber (567.89) = 98.765,
reverseNumber (230) = 32
Exercise 4:
• Using the predefined methods in class String; write the following methods:
a) getFileExtension that takes a string s as a parameter represents a file name, then returns the
extension of the file name (if found) or “none” (if not found).
For example: getFileExtension (“[Link]”) = “java” ,
getFileExtension (“xyz”) = “none”
b) countThisFileExtension that takes two parameters: an array of strings ar (represents an array
of file names) and a string s (represents a specific file extension) then counts and returns how
many of files at ar have the extension s.
For example: countThisFileExtension (ar , “exe”) returns how many of file names at the array
ar have the extension “exe”.
c) replaceFileExtension that takes three parameters: an array of string ar (represents an array of
file names) and 2 strings oldExtension and newExtension then replaces the extension of all the
file names in ar whose extension is oldExtension by the extension newExtension.
For example: replaceFileExtension (ar , “exe” , “java”) changes the extension of all the file
names in ar with extension “exe” to by the extension “java”.
Homework:
Design a class named “SubsCipher” that contains two static methods:
o public static char[] encrypt (char[] plain) that do the following:
The chars in the “plain” parameter are copied into another local defined array
“char [] temp” where throughout the copy:
Each occurrence of letter ‘a’ is replaced by ‘e’.
Each occurrence of letter ‘e’ is replaced by ‘o’.
Each occurrence of letter ‘i’ is replaced by ‘u’.
Each occurrence of letter ‘o’ is replaced by ‘i’.
Each occurrence of letter ‘u’ is replaced by ‘a’.
2|Page
Then the method returns temp
o public static char[] decrypt (char[] cipher) that do the following:
The chars in the parameter cipher are copied into another local defined array
“char [] temp” where throughout coping:
Each occurrence of letter ‘e’ is replaced by ‘a’.
Each occurrence of letter ‘o’ is replaced by ‘e’.
Each occurrence of letter ‘u’ is replaced by ‘i’.
Each occurrence of letter ‘i’ is replaced by ‘o’.
Each occurrence of letter ‘a’ is replaced by ‘u’.
Then the method returns temp.
3|Page