Worksheet on Functions
1.Create a class called myChar which has the following functions in it
public static boolean chkVowel(char ch)
Receives a character and returns true if the parameter is a vowel and false
otherwise.
public static boolean chkSpecial(char ch)
Receives a character and returns true if is a special character and false
otherwise.
2.Create a class called myMath which has the following functions in it.
public static int reverse(int n)
Receives an integer n, reverses it, and returns it
public static int sumofdigits(int n)
Receives an integer n, adds up the digits of the integer and returns the sum
public static long power(int x, int y)
Receives 2 integers and returns x raised to the power of y
public static int factorial(int x)
Receives an integer and returns the factorial of a number
Eg. 4! is 1*2*3*4=24
public static int factors(int x)
Receives an integer and returns the number of factors of the given integer
public static int Hcf(int,int)
Receives 2 integers and returns their HCF.
public static int LCM(int,int)
Receives 2 integers and returns their LCM
Page 1 of 3
3.Create a class myString that has the following public static member
functions.
public static int countchar(String s, char ch)
Receives a String and a character, counts the number of occurrences of the
given character and returns the count.
public static boolean chkname(String s)
Receives a String and checks whether it contains only alphabets and
spaces and returns true; otherwise it returns false.
public static boolean chknum(String s)
Receives a String and checks whether it contains only digits and returns
true; otherwise it returns false.
public static int wrdcnt(String s)
Receives a String, counts the number of words and returns the same.
public static int vowlcnt(String s)
Receives a String, counts the number of vowels and returns the same.
public static String toglcase(String s)
Receives a String and converts the uppercase letters in it to lowercase
and lowercase letters to uppercase. The rest of the characters (special
characters and numbers are not converted. The converted String is
then returned
public static String extraspace(String s)
Receives a String, removes the extra spaces between the words in the
String and returns the String
public static boolean palindrome(String s)
Receives a String, returns true if it is a palindrome and returns false if it
is not.
Page 2 of 3
Page 3 of 3