Problem Description:

Write a program that reads in a line of text (String), two indexes (two ints index1 and index2) and a word (String) from a text file, and then outputs that line of text with the substring between index1 and index2 changed to word. For example, a possible sample dialogue might be the following (user input in color):

File name:
data2.txt

Line of text read from file: I love you.

Starting index: 2

Ending index: 5

Word to look for: love
Replacement word: hate

I have rephrased that line to read: I hate you.

You can assume that word1 occurs in the input text. If word1 occurs more than once in the line, your program should replace only the first occurrence of word1.

The program should work for each of the following text files:

Data1.txt:

Java is great.
8
12
Wonderful

Data2.txt:

I love you.
2
5
Hate

Data3.txt:

Welcome to the University of Cincinatti.
29
38
Dayton

Part 1: Problem-Solving Phase

Using the Design Recipe, write each of the following for this problem:

1.Contract
2.Purpose Statement
3.Examples, making sure to include counter-examples
4.Algorithm

Make sure to test your algorithm by hand with the examples to verify it before continuing to Part 2.

Part 2: Implementation Phase

Using Eclipse, write the Java program for the algorithm formulated in Part 1, and test your program with the examples from Part 1. Make sure to incorporate your Contract, Purpose Statement and Examples as one or more comment blocks, and your Algorithm as line comments in your Java source code.

Part 3: Extra Credit

The indexOf method is also defined as part of the String class:

int indexOf(String s)

For example, if String s1 = "Programming in C++"; and String s2 = "C++";, then the expression s1.indexOf(s2) produces the integer 15.

Make a copy of your program from Part 2, and then modify it so that reads in a line of text and two words (word1 and word2) from a text file, and then outputs that line of text with the first occurrence of word1changed to word2. For example, a possible sample dialogue might be the following (user input in color):

File name:
words2.txt
Line of text read: I hate you.

Word to look for: hate

Replacement word: love

I have rephrased that line to read: I love you.

You can assume that word1 occurs in the input text. If word1 occurs more than once in the line, your program should replace only the first occurrence of word1.

The program should work for each of the following text files:

Words1.txt

Java is great.
Great
Wonderful

Words2.txt

I love you.
Love
Hate

Words3.txt

Welcome to the University of Cincinatti.
Cincinatti
Dayton
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.