Question

PYTHON 3.6 Overview In this assignment we implement a class called TripleString. It consists of a...

PYTHON 3.6

Overview

In this assignment we implement a class called TripleString. It consists of a few instance attribute and a few instance methods to support those attributes. In the next assignment, the TripleString class will help us create a more involved application. Before we do that though, we have to thoroughly test our TripleString implementation.

Specifications

The class TripleString will contain symbolic constants, instance attributes, and instance methods.

▶ Class symbolic constants

This class has 3 symbolic constants, which should be defined as class attributes:

MIN_LEN = 1    # The minimum length for attribute string1, string2, and string3
MAX_LEN = 100  # The maximum length for attribute string1, string2, and string3
DEFAULT_STRING = "(undefined)"   # The default value for the string attributes

▶ Instance attributes

The class has 3 instance attributes:

  • string1
  • string2
  • string3

These strings should be between between MIN_LEN and MAX_LEN characters, inclusive.

Extra credit: Note that the clients of this class are expected to access these attributes only through getters and setters (e.g. triple_str.set_string1("aaa")), and not the attributes directly (e.g. not triple_str.string1 = "aaa"). So these attributes don't have to be 3 physically separate attributes internally, or even have the specified names. For example, as long as the getter/setter pair get_string1()/set_string1() behave as expected, it doesn't matter that the first attribute's name is string1, blah, or something else. So, 10 extra points will be awarded for implementations that:

  1. store the 3 attributes in ways different from what's specified above (simply naming them differently does not count)
  2. correctly meet the requirements for getters/setters/__init__()/all methods below, so client code can work with either the normal or the extra-credit internal representation without any change
  3. allow the setters to share more code than just validate_string(); in this way, each setter should probably have a single line of code, which calls a common method.

Using list/tuple is one possibility; there may be others.

▶ Instance methods

The class has the following methods.

def __init__(self, string1, string2, string3):

This is the initializer that initializes all 3 instance attributes according to the parameters. It should call the setter (see below) for each instance attribute; if the setter of any instance attribute returns False, it should set that instance attribute to DEFAULT_STRING.

def set_string1(self, the_str):

This is the setter for instance attribute string1. It should call method validate_string() (see below) to validate the parameter the_str; if it returns False, no action should be taken (so instance attribute string1 is not changed), and the setter returns False; if it returns True, the instance attribute string1 should be assigned the_str, and the setter returns True.

def get_string1(self):

This is the getter for instance attribute string1. It should simply return the instance attribute string1.

def set_string2(self, the_str):
def get_string2(self):
def set_string3(self, the_str):
def get_string3(self):

Setters and getters for instance attribute string2 and string3. They have the same requirements as set_string1() and get_string1().

def __str__(self):

This method should return a str that contains information about the particular instance of the class. It should have at least the value of the 3 instance attributes.

def validate_string(self, the_str):

This helper method determines if the_str is valid.  the_str is valid if its length is between MIN_LEN and MAX_LEN, inclusive. (This really should be a class method, but it's not been covered and our focus for this assignment is instance methods.)

Testing

Write a global test() function that will test your implementation of TripleString by doing the following:

  1. Instantiate three or more TripleString instances with different values for its attributes; at least one instance should contain some invalid initial value.
  2. Immediately print() all instances; make sure these use TripleString.__str__() method, implicitly (by str(<TripleString instance>) or just <TripleString instance> ) or explicitly; also make sure all the attributes have the correct values.
  3. Pick one of the TripleString instances and do a number of setter tests. Test all 3 setters, passing both valid and invalid arguments to them. For each test, print a message (in test(), not in the setters) telling if the setter succeeds and if that's expected. For example, if <instance>.set_string1("abc") returns True, your test should print something like "set_string1('abc') succeeds, expected"; otherwise, print "set_string1('abc') fails, unexpected". For another example, if <instance>.set_string2("") returns True, your test should print "set_string2('') succeeds, unexpected"; otherwise, print "set_string2('') fails, expected". Also, use the corresponding getter to get the attribute just set by the setter and print it out, and make sure it's what you expect. Hopefully, after rigorously testing and fixing your code, all of your test results are as expected.
  4. Pick another one of the TripleString instances, and test all of its getters by printing each instance attribute out.

Call test() function at the global scope, and copy and paste the output to output05.txt.

Part of your grade depends on how thorough you tests are.

Skeleton code

class TripleString:
   """ Encapsulates a triple-string object """
   
   # Class constants
   MAX_LEN = 100
   ...

   # Initializer
   def __init__(self, string1, string2, string3):
   ...

   # Setters
   ...

   # Getters
   ...

   # Validate string method
   ...

def test():
    """Tests for TripleString class"""
    ts1 = TripleString("a", "b", "c")
    ts2 = TripleString("sun", "moon", "star")
    ...

test()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

class TripleString:

#""" Encapsulates a triple-string object """

# Class constants

MAX_LEN = 100

MIN_LEN=1;

# Initializer

def __init__(self, string1, string2, string3):

if self.validate_string(string1) == True:

self.string1=string1

else :

self.string1=""

if self.validate_string(string2) == True:

self.string2=string2

else :

self.string2=""

if self.validate_string(string3) == True:

self.string3=string3

else :

self.string3=""

# Setters

def set_string1(self, the_str):

if self.validate_string(the_str) == True:

self.string1=the_str

def set_string2(self, the_str):

if self.validate_string(the_str) == True:

self.string2=the_str

def set_string3(self, the_str):

if self.validate_string(the_str) == True:

self.string3=the_str

# Getters

def get_string1(self):

return self.string1

def get_string2(self):

return self.string2

def get_string3(self):

return self.string3

# Validate string method

def validate_string(self, the_str):

if len(the_str) >=self.MIN_LEN and len(the_str) <=self.MAX_LEN:

return True

else:

return False


def test():

"""Tests for TripleString class"""

ts1 = TripleString("a", "b", "c")

ts2 = TripleString("sun", "moon", "star")

#below statement not sets the string1 as it's min length is 0 , which has to be 1

ts1.set_string1("")

#print(ts1.get_string1())

#print(ts1.get_string2())

#print(ts1.get_string3())

#print(ts2.get_string1())

#print(ts2.get_string2())

#print(ts2.get_string3())

#writing to file output05.txt

fp = open("output05.txt","w")

#write three strings of object ts1

fp.write("Strings of ts1 object: ")

fp.write(ts1.get_string1())

fp.write(" ")

fp.write(ts1.get_string2())

fp.write(" ")

fp.write(ts1.get_string3())

fp.write("\n")

#write three strings of object ts2

fp.write("Strings of ts2 object: ")

fp.write(ts2.get_string1())

fp.write(" ")

fp.write(ts2.get_string2())

fp.write(" ")

fp.write(ts2.get_string3())

fp.close()

test()

#################################

#Output

# Check file output05.txt for output

#Content of file output05.txt

Strings of ts1 object: a b c

Strings of ts2 object: sun moon star

#Please use indentation for the program as wehn I post the program it removes indentation ,,

//just giving screen shot of code, just make indentation in same way

Add a comment
Know the answer?
Add Answer to:
PYTHON 3.6 Overview In this assignment we implement a class called TripleString. It consists of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • URGENT! Consider the following code for the definition of a Home object: 1 This class also...

    URGENT! Consider the following code for the definition of a Home object: 1 This class also contains getters, setters and a _str_method for it's owner and address attributes. class Home: def __init__(self, owner, address) self_owner owner, sell_address address Create a (derived) Apartment class that inherits from the Home class, but also has an attribute called rental price. Implement the Apartment class to inherit from the base Home class and contains all appropriate methods that need to be written for this...

  • Using the following code: store.py from abc import ABC,abstractmethod class Store(ABC): #creating a abstract class name=''...

    Using the following code: store.py from abc import ABC,abstractmethod class Store(ABC): #creating a abstract class name='' #declaring attributes in class address='' status='' sales_tax_percentage=0.0 def __init__(self,name,address,status,sales_tax_percentage): #init method to initialize the class attributes self.name=name self.address=address self.status=status self.sales_tax_percentage=sales_tax_percentage def get_name(self): #Getter and setter methods for variables return self.name def set_name(self,name): self.name=name def get_address(self): return self.address def set_address(self,address): self.address=address def set_status(self,status): self.status=status def get_status(self): return self.status def set_sales_tax_percentage(self,sales_tax_percentage): self.sales_tax_percentage=sales_tax_percentage def get_sales_tax_percentage(self): return self.sales_tax_percentage def is_store_open(self): #check store status or availability if self.status=="open": #Return...

  • Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def...

    Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def __init__(self, make, model, year) Methods in BMW parent class (can leave as method stubs for now): startEngine() -must print "The engine is now on." stopEngine() -must print "The engine is now off." Make another class called ThreeSeries -Must inherit BMW Hint: class ThreeSeries(BMW): -Also passes the three attributes of the parent class and invoke parent Hint: BMW.__init__(self, cruiseControlEnabled, make, model, year) -Constructor must have...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes,...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes, title...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • In Java For the following questions, “define a class” means the following: • Create an appropriately-named...

    In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT