Question

In: Computer Science

Write a python program using the following requirements: Create a class called Sentence which has a...

Write a python program using the following requirements:

Create a class called Sentence which

  • has a constructor that takes a sentence string as input.
    • The default value for the constructor should be an empty string
    • The sentence must be a private attribute in the class
  • contains the following class methods:
    • get_all_words — Returns all the words in the sentence as a list
    • get_word — Returns only the word at a particular index in the sentence
      • Arguments: index
    • set_word — Changes the word at a given index in sentence to a new word
      • Arguments: index, new_word
      • Does not return anything
    • a str method — Returns the sentence as a string using the built in method

Include a unit test in an if __name__ block to test the class. Use assert to check for errors.

Unit Test Example:

  • Set a sentence variable with a sentence of your choice
  • Instantiate the Sentence object using the variable
  • Test that get_all_words() correctly returns that sentence as a list
  • Test that get_word(#) correctly returns the # word in the sentence
  • Test that replace() actually changes a word in the sentence object
  • Print the sentence before and after the word change using the built in str method

Solutions

Expert Solution

class Sentence:
        def __init__(self, sentence):
                self.__sentence = sentence

        def get_all_words(self):
                return self.__sentence.split()

        def get_word(self, index):
                return self.__sentence.split()[index]

        def replace(self, index, new_word):
                l = self.__sentence.split()
                l[index] = new_word
                self.__sentence = ' '.join(l)

        def __str__(self):
                return str(self.__sentence)


if __name__ == "__main__":
        s = "Hello, this is the sample test case."
        sentence = Sentence(s)

        # Testing get_all_words
        assert ["Hello,", "this", "is", "the", "sample", "test", "case."] == sentence.get_all_words()

        # Testing get_word(index)
        assert "Hello," == sentence.get_word(0) and "is" == sentence.get_word(2) and "test" == sentence.get_word(5)

        # Testing replace(index, new_word) along with print
        print("Before Replace:", sentence)
        sentence.replace(4, "new")
        assert str(sentence) == "Hello, this is the new test case."
        print("After Replace:", sentence)

Related Solutions

Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
Write a Python program in a file called consonants.py, to solve the following problem using a...
Write a Python program in a file called consonants.py, to solve the following problem using a nested loop. For each input word, replace each consonant in the word with a question mark (?). Your program should print the original word and a count of the number of consonants replaced. Assume that the number of words to be processed is not known, hence a sentinel value (maybe "zzz") should be used. Sample input/output: Please enter a word or zzz to quit:...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a program called Teen that takes a sentence and returns a new sentence based on...
Write a program called Teen that takes a sentence and returns a new sentence based on how a teenager might say that. The method that creates the new string should be called teenTalk and should have the signature shown in the starter code. The way to do that is to put the word "like" in between each of the words in the original sentence. For example, teenTalk("That is so funny!") would return "That like is like so like funny!". Sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT