The aim of this lab is to introduce you to the Java String library and shows many ways to manipulate Strings.
Before you start, go to the String library documentation at and look up the following methods: equals, charAt, compareTo, indexOf, matches, replace, startsWith, toLowerCase, toUpperCase
. Also look up isLetter, isDigit, toLowerCase, toUpperCase()
from the Character
library.
Now using BlueJ create a new project and a Java class called StringsPractice
.
Download the JUnit4 tester before you start coding to test your code as you go. You can simply comment out any tests for code that you haven't written yet using the Java comments /* and */ at the beginning and end of the test method you want to ignore.
Write the code for as many of the following methods as you can. The final exercise (Haiku) is just for fun. That is, it won't be examined. You don't have to finish every one, but you should ensure you understand what is required. String manipulation will be required in the project.
public static int countChars(String s, char c)
public static String ceasarCipher(String s, int n)to convert its argument String sinto using a Ceasar cipher with distance given by argument n.
public static boolean isaDouble(String dtext)Decide if a given string is a valid (floating point) number (e.g. "1.2", "12345", "0.999999") and false otherwise (e.g. "12.invalid", "1 3 4", "2b7.5")
public static String makeSentence(String[] words)
Create a sentence from a given list of words returning a single String with spaces between words and a full stop at the end. You can assume the first word is correctly capitalised.
public static String longest(String[] names)
Returns the longest String from the argument array of Strings. You can use the instance method length()
from the class String
to find out how long each String
is. For example for the array {"which", "word", "is", "the", "longest" }
the method should return "longest". See the tester forspecial cases.
public static int[] tallyChars(String text)
This method reads a string of text and counts the number of times each letter occurs. Only letters are counted and the case of the letters (upper or lower) is ignored. The frequency count of the letters in the given text is returned in an array of 26 elements where position i refers to the i+1-th letter of the alphabet: 0 for 'a', 1 for 'b', 2 for 'c' and 25 for 'z'. A call to tallyChars("abc2 Z AC 3")
should return the array {2,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}
public static String makeHaiku()
A Haiku is a poetic form. Poems have 3 lines and 17 syllables as 5 + 7 + 5. A syllable is "a single uninterrupted sound". An example Haiku is
"How beautifully
That kite soars up to the sky
From the beggar’s hut."
(Issa)
Write a Haiku generator that creates a 3 line poem with the right number of syllables. Include the characters for space ' ' and new line '\n' in your Strings to make your poem readable.You can generate Haikus from a sample dictionary using 2 dimensional array of Strings and selectingwords up to the right number of syllables from that dictionary.The index of the first dimension of the dictionary arraygives the number of syllables (minus 1)and the second dimension is a list of Strings.For example, add the following to your StringsPractice class or (better still)create your own dictionary.public static final String[][] MY_DICTIONARY = {
Construct a string that fits the Haiku form by choosing words at random from collections of one, two, toseven syllable words.Most of your Haikus will not make sense, so now try someexperiments to finda data structure that gives you better control of random word selection.
{"how","that","kite","soars","the","a"},
{"only","happy","longest"},
{"terrible"},
{"beautifully"},
{"congratulations","hippopotamus"},
{"incomprehensible"},
{"infinitesimally","superficiality"}
}