BASIC PROGRAMMING EXERCISES (CONDITIONAL STATEMENTS, LOOPS, AND STRINGS)

COMP-202B, Winter 2009, All Sections




Instructions:


Problems:

  1. Write a Java class called Factorial which defines a main() method that asks the user to enter a positive integer n and displays the factorial of n. The factorial of an integer n, denoted n!, is defined as n * (n-1) * (n-2) * ... * 1 (but note that 0! is 1).

  2. Write a Java class called Fibonacci which defines a main() method that asks the user to enter a positive integer n, and computes the nth Fibonacci number. The nth Fibonacci number is defined by this formula:

  3. Write a Java class called CheckPrimality which defines a main() method that asks the user to enter a positive integer n, and displays whether or not n is a prime number. A prime number is an integer which cannot be divided evenly except by 1 and itself.

  4. Write a Java class called Power which defines a main() method that asks the user to enter a double base and a positive integer exponent, and displays base to the power of exponent. You may assume that base and exponent are not both 0. You must write the computation yourself; in other words, you MUST NOT use the Math.pow() method.

  5. Write a Java class called CountDigits which defines a main() method that asks the user to enter a positive integer n, and displays how many digits are in n.

  6. Write a Java class called Contains which defines a main() method that asks the user to enter a String s and a character c, and displays whether or not c occurs in s. You MUST NOT use any methods defined in the String class other than chartAt() and length().

  7. Write a Java class called CountOccurrences which defines a main() method that asks the user to enter a String s and a character c, and displays the number of times c occurs in s.

  8. Write a Java class called CheckDuplicates, which defines a main() method that asks the user to enter a String s, and displays whether or not any character in s occurs more than once.

  9. Write a Java class called MostOftenOccurring which takes a String s and displays the character which occurs most often in String s. If there is a tie, display the character which occurs first in s among those which occur most often.

  10. Write a Java class called ToUpperCase which defines a main() method that asks the user to enter String s and displays a new String which contains exactly the same characters as s in the same order, except that all lower-case letters occurring in s are replaced by their upper-case equivalents in the result String. You MUST NOT use any methods defined in the String class other than charAt() and length().

  11. Write a Java class called IsAlphabetPermutation, which defines a main() method that asks the user to enter a String s and displays whether or not s is a permutation of the upper case letters in the alphabet.

  12. Write a Java class called OccursHere, which defines a main() method that asks the user to enter two Strings, superString, and subString, as well as a positive integer position, and displays whether or not subString occurs in superString at position position. You may assume that position is within the range [0, superString.length() - 1]. You MUST NOT use any methods defined in the String class other than charAt() and length().

  13. Write a Java class called OccursAnywhere, which defines a main() method that asks the user to enter two Strings, superString and subString, and displays whether or not subString occurs anywhere in superString. You MUST NOT use any method defined in the String class other than chartAt() and length().

  14. Write a Java class called Substring, which defines a main() method that asks the user to enter a String s, a positive integer position, and a positive integer number, and displays a new String formed by taking the number characters of s starting at position position. If there are less than number characters between position and the end of s, your main() method should display a new String formed by these characters. You MUST NOT use any methods defined in the String class other than charAt() and length().

  15. Write a Java class called RemoveAll, which defines a main() method that asks the user to enter a String s and a character c, and displays a new String consisting of all the characters in s in the order in which they appear in s, but with all occurrences of c removed. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  16. Write a Java class called NumberConsecutiveOccurrences, which defines a main() that asks the user to enter a String s, a character c, and an integer position, and displays the number of occurrences of c starting at position position in s. For example, if s is "abbbbbaaa", c is 'b', and position is 1, then your main() method will display 5, as there are 5 consecutive occurrences of 'b' starting at position 1. You may assume that position is within the range [0, s.length() - 1]. You MUST NOT use any methods defined in the String class other than charAt() and length().

  17. Write a Java class called MaxConsecutiveOccurrences, which defines a main() method that asks the user to enter a String s and a character c, and displays the position in s at which the longest series of consecutive occurrences of c occurs. If there is a tie between two positions, you should return the lowest position. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  18. Write a Java class called Reverse, which defines a main() method that asks the user to enter a String s, and displays a new String consisting of the characters in s in the reverse order. You MUST NOT use any methods defined in the String class other than charAt() and length().

  19. Write a Java class called IsPalindrome, which defines a main() method that asks the user to enter a String s, and displays whether or not s is a palindrome. A palindrome is a String which reads the same forwards and backwards, such as "laval" or "stressed desserts".

  20. Write a Java class called KillWhiteSpace, which defines a main() method that asks the user to enter a String s, and displays a new String consisting of all the characters in s, but with the leading and trailing white space removed. In addition, every sequence of spaces within the String is reduced to one space only. You MUST NOT use any methods defined in the String class other than charAt() and length().

  21. Write a Java class called Equals, which defines a main() method that asks the user to enter two Strings s1 and s2, and displays whether or not s1 and s2 are equal. Two Strings are equal if they are of the same length and the same characters appear in the same positions. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  22. Write a Java class called DestroyAllOccurrences, which defines a main() method that asks the user to enter two Strings superString and subString, and displays a new String consisting of all characters in superString with all occurrences of subString deleted. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  23. Write a Java class called SubSet, which defines a main() method that asks the user to enter two Strings superString and subString, and displays whether or not all the characters of subString occur anywhere in superString. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  24. Write a Java class called AppearInOrder(), which defines a main() method that asks the user to enter two Strings subString and superString, and displays whether or not all the characters of subString appear in order in superString. You MUST NOT use any of the methods defined in the String class other than charAt() and length().

  25. Write a Java class called CountWords, which defines a main() method that asks the user to enter a String s, and displays the number of words in s. A word is delimited by one or many space characters. You MUST NOT not use any of the methods defined in the String class other than charAt() and length(). In addition, you MUST NOT use the StringTokenizer class.