Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

programming language: Python Data for Character class $1920_Assignment2.pdf 0 Q Q Search Assignment Objectives By completing this assignment you are demonstrating your ability to: Designing

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
programming language: Python
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Data for Character class
$1920_Assignment2.pdf 0 Q Q Search Assignment Objectives By completing this assignment you are demonstrating your ability to: Designing your own related set of classes in the Python programming language. Create new classes using class composition. Implement accessor and mutator methods that allow access to your class data Create a dunder methods for your class. Save data to disk. . Scenario In this course you will have 4 assignments, all of which are going to be related to the same topic: creating characters with various skills and squads with those characters for a game or story. 1 The Problem In storytelling, whether it is in mythic tales, novels, movies or video games, it is important that characters have a set of common attributes that work within the setting of the story. Most of the times characters don't exist in isolation but as part of larger groups. In this assignment you will create a squad (or gang) of such characters. Because a squad will be conformed by different characters, in this assignment you must reuse the Character class you created for Assignment 1. Specifically, the Squad class will be composed by four instances of the Character class. There will be one leader and three members. The Squad class should have the following: 1. A name for the squad. 2. Four different characters (one of which should be the leader). 3. The class constructor will receive the name of the squad and the four members, the first member should be the leader. 4. You must use name mangling to make all these instance variables inaccessible from outside the class. 5. There must be a getter for the name of the squad, but no setter. The name can't be changed after creating a squad. 6. The class must contain a method squad_size that will return the number of members in the squad (including the leader, thus after creation it should be 4). 7. There must be a mutator method add_member that receives a member and adds it to the squad. 8. There must be a mutator method remove_member that receives a member and removes it from the squad. If the leader of the squad is removed then the first member becomes the new leader. If a squad has only one remaining member then that member can't be removed. Thus, if this method receives as input the only remaining member, or a character that doesn't belong to the squad, then the method should do nothing. 9. You must implement a get_member method that receives an index and returns the correspond- ing member, index 0 will refer to the leader, index 1 to the first member, and so on. 10. You must also implement the --str-- method for printing the squad's info in a format similar to the one shown in the example. You should implement this method by reusing (calling) the _str__method from the Character class. 11. You must implement a save_to_disk method that prints the squad's info to a file in text format. The info saved to disk should be the exact same info returned by the __str_method from the Squad class. 12. You must also provide appropriate docstring documentation for classes and methods. 13. You must provide a driver script that creates four characters and add them to a squad and save the squad to disk. Then you should create a new character, add this new character to the squad and save the new squad to disk. Finally you should remove two characters from the squad. one of which should be the leader; then save the squad to disk and print it to console. Assignment2.pdf Q D y Q Search 11. You must implement a save_to_disk method that prints the squad's info to a file in te format. The info saved to disk should be the exact same info returned by the __str__metho from the Squad class. 12. You must also provide appropriate docstring documentation for classes and methods. 13. You must provide a driver script that creates four characters and add them to a squad and save the squad to disk. Then you should create a new character, add this new character to the squad and save the new squad to disk. Finally you should remove two characters from the squad, one of which should be the leader; then save the squad to disk and print it to console. Driver Script and Sample Run Continuing with our setting of the Wild West, we start by creating four different characters as shown in Figure 1: Billy the Kid, Diana Gold, Frank the Creep and James Montgomery. Then we create our squad, which we named as A weird gang, and save it to disk. After that we create a new character for Johnny Silverado, add him to the squad, and save the squad to a different file. The final part of the driter removes two characters from the squad: Frank the Creep and the leader Billy the Kid. After removing the squad leader, the first member should become the new leader. We finally save the squad to a different file and print it to console. from character import Character fron squad import Squad First we create the four initial characters for the squad henry = Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 82) diana = Character("Dana Gold", "The richest merchant of the west", "Merchant", 5.4, 54) frank = Character("Frank the Creep", "Knows his knifes", "Butcher", 4.1, 40) james = Character("James Montgomery", "The fastest gun in the west", "Hired Gun", 2.1, 88) He create the squad and save it to disk squad = Squad("A weird gang", henry, diana, frank, James) squad.save_to_disk(*squad_day1.txt") Johnny joins the squad johnny = Character("Johnny Silverado", "Fast and Furious", "Town Marshal", 2.9, 98) squad.add_member(Johnny) squad.save_to_disk("squad_day2.txt") and we lost the Billy and Frank the Butcher :( squad.remove_member(frank) squad.remove_senberChenry) #Diana should now become the leader squad.save_todisk("squad day3.txt") print(squad) Figure 1: Driver program for testing the Squad class. TARE Q Q Search squad Teeme henry squad, save_to_disk("squad_day3.txt") point(squad) Figure 1: Driver program for testing the Squad class. Figure 2 shows the output of running the driver program. You can see that after removing Billy and Frank the squad has now Diana as the leader and two more members. You can also see the format used to print the squad of the squad, and the information for each character. Notice that the character information is printed in the same format as in the Character class. Figures 3, 4 and 5 show the contents for the three text files saved by the driver program. Very important note: you are not supposed to create the exact same squad and characters as in this example. You must create your own class with your own attributes, mutators and valid ranges. In a setting other than the Wild West. *** A weird gang *** LEADER: Diana Gold The richest merchant of the west Profession: Merchant Draw speed: 5.4 Aim: 54 MEMBER 1: James Montgomery The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 88 MEMBER 2: Johnny Silverado Fast and Furious Profession: Town Marshal Draw speed: 2.9 Aim: 98 Process finished with exit code o Figure 2: Output for running the driver. squad_day1.txt *** A weird gang *** LEADER: Billy the Kid - A dangerous outlaw ...n BOGE squad_day1.txt X *** A weird gang *** LEADER: Billy the Kid A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 5 Aim: 82 6 MEMBER 1: Diana Gold - The richest merchant of the west Profession: Merchant Draw speed: 5.4 Aim: 54 MEMBER 2: Frank the Creep Knows his knifes 11 Profession: Butcher 12 Draw speed: 4.1 Aim: 40 MEMBER 3: James Montgomery The fastest gun in the west 15 Profession: Hired Gun Draw speed: 2.1 Aim: 88 Figure 3: Content of the first file. & squad_day2.txt *** A weird gang *** LEADER: Billy the Kid A dangerous outlaw 3 Profession: Gunfighter Draw speed: 3.2 Aim: 82 MEMBER 1: Diana Gold The richest merchant of the west Profession: Merchant Draw speed: 5.4 Aim: 54 MEMBER 2: Frank the Creep Knows his knifes Profession: Butcher 12 Draw speed: 4.1 13 Aim: 40 MEMBER 3: James Montgomery The fastest gun in the west 15 Profession: Hired Gun Draw speed: 2.1 Aim: 88 MEMBER 4: Johnny Silverado Fast and Furious Profession: Town Marshal Draw speed: 2.9 21 Aim: 98 Figure 4: Content of the second file. 35-01-01 19 ar squad_day3.txt *** A weird gang *** LEADER: Diana Gold The richest merchant of the west 3 Profession: Merchant Draw speed: 5.4 Aim: 54 MEMBER 1: James Montgomery - The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 88 MEMBER 2: Johnny Silverado - Fast and Furious Profession: Town Marshal Draw speed: 2.9 Aim: 98 Figure 5: Content of the third file. folder and Python file should be named as problem# .py where the # is the problem number. 35-01-01 1985 Jan im Assignment Objectives By completing this assignment you are demonstrating your ability to: . Design your own class in the Python programming language. Implement accessor and mutator methods that allow access to your class data Create a dunder methods for your class. Employ your creativity to design a class that is unique to you. Scenario In this course you will have 4 assignments, all of which are going to be related to the same topic: creating characters with various skills and squads with those characters for a game or story. The Problem In storytelling, whether it is in mythic tales, novels, movies or video games, it is important that characters have a set of common attributes that work within the setting of the story In this assignment you will create a class called Character which creates a character in a setting of your choosing. The class should have the following: 1. A name for the character. 2. A brief description (less than 100 characters long). 3. Exactly three attributes of type string, float and integer. 4. The class constructor will receive the name, description and the values for the different attributes and use them to create an instance of the class. 5. You must use name mangling to make all these attributes inaccessible from outside the class (including the name and description). Search CS1920_Assignment 1.pdf 6. There must be getters for all these attributes, and a setter for the description. The name however, can't be changed after creating a Character. 7. The float attribute should have a setter that checks whether the provided value is in a given interval and only updates the attribute if that's the case. 8. There must be a mutator method for increasing the integer attribute in some amount (in the our example we use a level_up_aim() method that increases the aim by 5%). 9. You must implement the __str__method for printing the character's info in a format similar to the one shown in the example. 10. You must also provide appropriate docstring documentation for classes and methods. 11. You must provide a driver script that creates an instance of a character, modifies and prints it, as shown in the example. Example Setting If we were creating a class for an Old West setting we could define characters with attributes such as profession (which would be a string attribute). aim (an integer) and draw speed (a float). The aim could refer to the effectiveness (percent) of hitting a target when firing a gun. The draw speed would be the speed at which the character can draw their gun in a gun tight, measured in seconds. The profession could be something like "Outlaw", "Town Marshal", "Merchant", "Banker". "Farmer", etc. In such a setting one of our characters could be Billy the Kid, whose birth name was actually Henry McCarty. His attributes could be the following: Name: Billy the Kid Description: A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 seconds . Aim: 82% I . Driver Script and Sample Run Driver Script and Sample Run In the driver script you must create a character of your own and then print its attributes. Afterwards you should call the mutator method that levels up the integer attribute (in our case we increase the aim by 5%); and also call the setter of the attribute that checks for a range. As you can see in the driver script in Figure 1, this attribute is the draw speed. Which in our setting has a valid range of (0.5,5), thus the second call to the setter is ignored by the Character class and no update is performed. Finally, you can also see in Figure 1 that the we print a message indicating how the attributes have changed, and we do so making use of the getter methods. henry = Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 82) And print 123 Stats print (henry) # Billy the Kid has been training Nisam nas improved and he draws his gun faster henry.level_up_ain() henry.set_draw_speed(3.0) # this is the new valid attribute value henry.set_draw_speed(-7.8) this is an Invalid value, thus the attribute won't change Let's print that print(" 81lly the Kid has been training...") print(f"His aim now is (henry.get_aino effective, f"and he draws his gun in {henry.get_draw_speed() seconds") Figure 1: Driver program for testing the Character class. Figure 2 shows the result of running the driver program. You can see the format used to print the name, description and attributes of the character. You should follow a similar format for your class. You can also see how the call to the setter of the draw speed (with the parameter-7) had no effect in changing the attribute value (which was effectively changed to 3 afterwards) Very important note: you are not supposed to create the exact same class and character as in this example. You must create your own class with your own attributes, mutators and valid ranges. In a setting other than the Wild West. Billy the Kid A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 Aim: 82 Figure 1: Driver program for testing the Character class. Figure 2 shows the result of running the driver program. You can see the format used to print the name, description and attributes of the character. You should follow a similar format for your class. You can also see how the call to the setter of the draw speed (with the parameter -7) had no effect in changing the attribute value (which was effectively changed to 3 afterwards) Very important note: you are not supposed to create the exact same class and character as in this example. You must create your own class with your own attributes, mutators and valid ranges. In a setting other than the Wild West. Billy the Kid A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 Aim: 82 Billy the Kid has been training... His aim now is 87% effective, and he draws his gun in 3.0 seconds Process finished with exit code o Figure 2: Output for running the driver. Submitting your assignment Your submission will consist of one zip file containing solutions to all problems (see below). This file will be submitted via a link provided on our Moodle page just below the assignment description. This file must be uploaded by 5pm (Moodle time) on the due date in order to be accepted. You can submit your solution any number of times prior to the cutoff time (each upload overwrites the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions