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

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...
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...
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...
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...
In Python, create a program with 2 classes that do the following. HashCreate class, this class...
In Python, create a program with 2 classes that do the following. HashCreate class, this class will accept a directory and hash each file in the directory and store the results in a dictionary. The dictionary will contain the hash value and file name. HashVerify, the second class will accept the dictionary as input and save that in an instance attribute. This class must also contain a method for lookups that require a file path as input. The lookup method...
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...
Using Python 3, Write a class called GolfScore that keeps track of the score for a...
Using Python 3, Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9. Create a class level variable named NUM_OF_HOLES set to 9. Methods to be written: The constructor – sets the score for all holes to -1 addScore (hole, score) – this method...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT