Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

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.

  1. Write a method to count how many times character c occurs in string s.
    public static int countChars(String s, char c) 
  2. The following text is an encoded message using the Caesar Cipher."YMJ KFZQY IJFW KJQQTB QNJX STY NS YM MJFAJSX, GZY NS TZXJQAJX. WJYZWS YT WTRJ"The cipher was used by Julius Caesar to transmit messages that he wished to remain readable only to his trusted friends. The cipher is easy to break because it is simple cyclic substitution cipher: each character is replaced by its n-th successor (converted to upper case if necessary).For example, if s="abc" and n=3 then 'a' is replaced by 'D' and 'b' is replaced by 'E'. For example, ceasarCipher("AbC",3) returns the string "DEF".Ignore case and give all ciphers in uppercase as above.Spaces are copied as in the original. Write a method
    public static String ceasarCipher(String s, int n)
    to convert its argument String sinto using a Ceasar cipher with distance given by argument n.
  3. 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")
  4. 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.

  5. 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.

  6. 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}
  7. 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 = {
    {"how","that","kite","soars","the","a"},
    {"only","happy","longest"},
    {"terrible"},
    {"beautifully"},
    {"congratulations","hippopotamus"},
    {"incomprehensible"},
    {"infinitesimally","superficiality"}
    }
    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.