Assignment #6 - String Exercises

Due: Thurs, July 1

From Deitel, Do exercise 11.9 (page 563), as well as the "Replace" exercise given below.


Filenames should be

Note that these filenames all start with a capital letter. Please make your filenames match mine exactly.

Instructions

For exercise 11.9 -- Sentences.java

Replace Exercise -- (Replace.java)

Write a method with the following signature that returns a new string which is made by replacing all occurrences of oldStr with newStr in the original string str
  public static String replace(String oldStr, String newStr, String str)
You will need to place the MyInput.java file in the directory with your code when you compile (the same MyInput class that we used in the first few assignments). To test your replace method, use the following main() method (include it in the file that you submit):
  public static void main(String[] args)
  {
    String str, sub, rep;
    System.out.println("Programming Exercise: 'Substring Replacing'");

    System.out.print("Input initial string: ");
    str = MyInput.readString();

    System.out.print("Input substring to be replaced: ");
    sub = MyInput.readString();

    System.out.print("Replace with? ");
    rep = MyInput.readString();

    System.out.println("The initial string is '" + str + "'");
    System.out.println("The new string is '" + replace(sub,rep,str) + "'");
  }

Sample Outputs

Sample user input is underlined.

Sample output for exercise 11.9

 Programming Exercise: 'Sentences'

 Any boy jumped to the dog.
 Some dog skipped from the town.
 The town skipped over the car.
 A dog drove under a dog.
 The girl ran from the boy.
 The boy drove over a dog.
 A boy ran from any boy.
 Any boy walked on a boy.
 The boy jumped over one town.
 The girl ran to any town.
 Some boy jumped over any dog.
 The town jumped from one dog.
 The boy drove from the car.
 A girl ran over one boy.
 A boy walked on one car.
 Any dog drove on the boy.
 Any car drove to one dog.
 The town walked under one girl.
 One girl ran to some dog.
 A town ran over some boy.

A few sample outputs for Replace exercise

 Programming Exercise: 'Substring Replacing'
 Input initial string:  The quick brown fox jumped over the quick dog
 Input substring to be replaced:  quick
 Replace with?  hot
 The initial string is 'The quick brown fox jumped over the quick dog'
 The new string is 'The hot brown fox jumped over the hot dog'


 Programming Exercise: 'Substring Replacing'
 Input initial string:  Concatenate the bobcat with the catapult
 Input substring to be replaced:  cat   
 Replace with?  bird
 The initial string is 'Concatenate the bobcat with the catapult'
 The new string is 'Conbirdenate the bobbird with the birdapult'

Submitting:

Pack up your files with jar. For this one, you only need to pack up your source code files (Sentences.java and Replace.java) into one jar file called hw6.jar. This does not need to be a "runnable" jar file (so no manifest addition needed).

E-mail this jar file to me (myers@cs.fsu.edu) by the due date. Your e-mail subject should be HW6-SUBMIT. Include your name and section in the e-mail body. Please only submit ONCE, unless you make a mistake and need to correct it (before the due date) -- and minimize this. Only the last submission will be graded.