In Java, Strings are immutable i.e. cannot be changed and this explains why there exists other kinds of strings in the language, namely StringBuffers (since the beginning) and StringBuilders (since Java 5.0).
The best way to understand the syntax and semantics of strings in Java is to write progressively more complex programs. The work to do is:
- Write a Java program which reads a sentence and a number of repetitions from standard input. The program should then create a string which is the sentence repeated the specified number of times. The string should then be displayed on standard output.
- Write a Java program which reads words from the system dictionary and finds all the words with start with “aba”. Those words followed by their capitalized versions are printed. In Unix, the system dictionary is a normal text file at /usr/share/dict/words
- Write a Java program which reads words from the system dictionary and finds and print all the words with match with a regular expression entered by the user.
Java has a rich class library containing two very important kinds of collections: Lists and Maps. Lists are linear structures which can contain several elements. Typical lists include ArrayLists and LinkedLists. Maps are what are known as associative arrays in other programming languages and, when used properly, give the programmer a lot of power and flexibility. In order to master Lists and Maps, it is important to write actual Java applications. The work to do is:
- Write a Java program that prompts the user for marks obtained by students for one exam and displays on screen the best mark and the average mark.
- Write a Java program that prompts the user for words and displays for each unique word the number of times it was typed.
- Write a program which finds all anagrams from the system dictionary. Two distinct words are anagrams if they contain the same letters.
Do not forget to refer to the Java API Specification when needed.
Leave a Reply