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...
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...
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...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT