Question
PYTHON A Simple Class: TripleString Understand the Application The assignment is to first create a class called TripleString . TripleString will consist of three instance
PYTHON
A Simple Class: TripleString
Understand the Application
The assignment is to first create a class called TripleString. TripleString will consist of three instance 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. In a later assignment, the TripleString class will help us create a more involved application.
TripleString will contain three member strings as its main data: string1, string2, and string3. We will also add a few class/ static members such as const int MAX_LEN and MIN_LEN. These represents the maximum and minimum length that our class will allow any of its strings to be set to. We can use these static members in the TripleString method whose job it is to test for valid strings (see below).
The Program Spec
Class TripleString Spec
Instance Members:
(string) string1
(string) string2
(string) string3
All legal strings should be between 1 and 50 characters, inclusive.
As stated in the modules, we never want to see a literal in our methods. So the class should have static members to hold values for the limits described above, as well as default values for any field that is construct-ed using illegal arguments from the client. These are defined and initialized in the static section.
Class Static Intended Constants:
MIN_LEN = 1
MAX_LEN = 50
DEFAULT_STRING = "(undefined) "
Instance Methods
Default Constructor
TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.
Combination Default/Paramteter-Taking Constructor
def __init__(self, string1 = DEFAULT_STRING, string2 = DEFAULT_STRING, string3 = DEFAULT_STRING):-- a constructor that initializes all members according to the passed parameters. It has to to be sure each string satisfies the class requirement for a member string. It does this, as in the modules, by calling the mutators and taking possible action if a return value is false. If any passed parameter does not pass the test, a default string should be stored in that member.
Mutators/Accessor
set()s and get()s for these members. Mutators in our course are named using the convention as follows: set_string1( ... ), set_string3( ... ), etc. Likewise with accessors: get_string2(). We need an accessor and mutator for each individual string member, so three pairs of methods in this category. Mutators make use of the helper method valid_string(). When a mutator detects an invalid string, no action should be taken. In that case, mutator returns False and the existing string stored in that member prior to the call remains in that member, not a new default string.
string to_string() - a method that returns a string which contains all the information (three strings) of the TripleString object. This string can be in any format as long as it is understandable and clearly formatted.
Helper Methods
def valid_string(self, the_str): -- a helper function that the mutators can use to determine whether a string is legal. This method returns True if the string's length is between MIN_LEN and MAX_LEN (inclusive). It returns False, otherwise. This would normally be defined as a class member method, but since we are just starting to learn about static and class methods, we'll allow it to be an instance method this week.
Where it All Goes
There are now a variety of program elements, so let's review the order in which things appear in your .cpp file:
class definition(s)
global-scope function definition(s) [You may not need them for this assignment.]
main program
In other words, OurAssignment.py will look like this:
# ---------------- SOURCE ---------------------------------------- class TripleString: """ encapsulates a 3-string object """ # intended class constants ------------------------------------ MAX_LEN = 50 ... # constructor method ------------------------------------ def __init__(self, string1 = DEFAULT_STRING, string2 = DEFAULT_STRING, string3 = DEFAULT_STRING): ... # mutator ("set") methods ------------------------------- ... # accessor ("get") methods ------------------------------- ... # helper methods for entire class ----------------- ... # ------------- CLIENT -------------------------------------------------- # Create 4 TripleString objects triple_string_num_1 = TripleString() ...
The main program
Instantiate four or more TripleString objects, some of them using the default values, some using arguments.
Immediately display all objects.
Mutate one or more members of every object.
Display all objects a second time.
Do two explicit mutator tests. For each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.
Make two accessor calls to demonstrate that they work.
More:
Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences in your mutator/accessor tests.
No user input should be done in this program.
I am not supplying a sample output this week -- the above description is adequate.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started