CSci 1211 Problem set 3. Due Friday, Feb. 6th at 5pm

Resources: APIs: String and StringBuffer. Also, use the examples from the January 26-28th classes (see resources web page).

Please remember to write your program in good programming style: choose meaningful names of variables and don't forget comments! Also, please read the problems carefully and implement them exactly.

Problem 1 (7 points)

A simple way of semi-secret communication is to insert a specific word after every syllable in a phrase. Write a program that deciphers a simple "aha-code": the speaker (or, in this case, the user) inserts "aha" after every syllable. Your program should read a line of input, print out the deciphered version, and then print out the original text. Here is a sample run of the program (user input shown in green):

Please say something secret: Itaha isaha coldaha inaha Moaharrisaha!
Aha! You meant:
It is cold in Morris!
When you said:
Itaha isaha coldaha inaha Moaharrisaha!
You may assume that aha is always lowercase.

Problem 2 (7 points)

Write a program that reads an input string from the user and prints out the same string, but with all first half in upper-case letters and the second half in lower-case letters. If the string has an odd number of characters, the case of the middle character doesn't matter. Examples (user input in green):

Please type in a string: ThIS is My STrinG
You typed: THIS IS my string

Please type in a string: AppLE
You typed: APple

Please type in a string: AppLE!
You typed: APPle!

Problem 3 (16 points)

Many of us played with "Mad Libs" as kids. It's a game for two (or more) people in which one person makes up a story with some words missing and asks the other player(s) questions to fill in the missing words, for instance: "Please name a person", "Please give me an adjective" and so on. Since the people who filled in the blanks had no idea what the story was, the result can be very funny. Here are some online examples that I've stumbled upon.

Your task is to write a program that asks the user to enter some inputs and then prints out a story in which the missing words are filled in with the user's input. To see a sample program, please download the class file MadLib.class to the folder with your java programs and run it by typing Java MadLib at the command prompt. Of course, you have to make up your own story.

Important! At least one of the user's inputs must appear in your story twice (or more times). Your program must take at least three inputs. The story should be two-three sentences long. Don't worry about articles mismatching nouns, such as "a apple".

Also important: If you'd like, I can make the .class file of your program available on the class web page so that others can try it, too. The funniest story (or two) might even get a small prize :-)

Helpful: When writing a long string, make sure to open and close quotes on one line. For instance, the following does not work in Java:


   String longstr = "My very very very very very very very very very 
       very very very very very very very long string";
Instead break the string into smaller substrings, each surrounded by quotes, and connect them with a + operator:

   String longstr = "My very very very very very very very very very" + 
      " very very very very very very very long string";

Problem 4 (paper and pencil problem, 10 points)

For the following program draw the diagram of all the variables and objects (as we did in class). If an object is going to be garbage-collected, don't erase it, just put letters GC next to it.

// PS3 question on Strings and StringBuffers

public class PS3 {

    public static void main (String [] args) {
	String str1 = new String("Java ");
	StringBuffer sb1 = new StringBuffer(" fun");
	sb1.insert(0,str1);
	StringBuffer sb2 = new StringBuffer("String");
	str1 = sb2.substring(0,1);
	str1 = str1.toLowerCase();
	int n = sb2.indexOf("ring");
	sb2.delete(0,1);
	String str2 = sb2.substring(2,3);
	str1 = str2.concat(str1);
        int m = sb1.indexOf("fun");
	sb2 = sb1;
	sb2.insert(m-1,str1);
    }
}
You may, if you'd like, test the program by putting print statements in it. In fact, it may be a good idea to do it. However, your work will be graded based on the diagram only.

Problem 5 (extra credit, 5 points)

Write a program that replaces the second occurrence of the word "red" in an input string by the word "blue". Your program should work for lower-case "red" and "blue" only, it should ignore "Red" or "RED". You may assume that there are at least two occurrences of the word "red" in the input string. If you make any other assumptions about the input string, please state them clearly.

Important: do not use if/else statement or any other Java features that we haven't covered in class yet.


This page is a part of the course CSci 1211 at UMM.