Answered step by step
Verified Expert Solution
Question
1 Approved Answer
python problem!!! The Problem In previous assignments we have created a Character class for our characters and a Squad class for creating a gang with
python problem!!!
The Problem In previous assignments we have created a Character class for our characters and a Squad class for creating a gang with various characters. In this assignment we want to create a new kind of squad: a defensive squad. Besides the leader and the regular members, a defensive squad will also include a medic (or healer). The medic is a normal character with the distinction that it can't be removed from the squad. In this assignment you must create a class for a defensive squad. Because a defensive squad is a particular case of a regular squad, you must provide an implemen- tation of the DefensiveSquad class that inherits from the Squad class. This inheritance will provide two important advantages: it will allow you to reuse most of the code you already implemented for the Squad class (we are not talking about copy-pasting the previous code) and it will also allow you to treat squads polymorphically. The DefensiveSquad class should have the following: 1. It must inherit from the Squad class. 2. The class constructor will receive the name of the squad and five members, the first member should be the leader, the fifth must become the medic. 3. The class constructor must call the constructor of the super class and then complete any other required operation. 4. You must use name mangling to make all these instance variables inaccessible from outside the class. 5. There must be a getter and a setter for the medic. 6. You must provide a design that guarantees that the medic can't be removed from the squad, without overriding the remove_member method from the Squad class. 7. The class must contain a method squad_size that will return the number of members in the squad (including the leader and the medic). 8. The class must contain a method get_member that will return a member given it's index (the medic should be the last member). Depending on your implementation you may have to overwrite this method to account for the medic, but you can reuse the code of the Squad class by calling it inside this method. 9. You must implement the __str__ method for printing the squad's info in a format similar to the one shown in the example. You must implement this method by reusing (calling the --str__ method from the super class and only add the information about the medic. 10. You must also provide appropriate docstring documentation for classes and methods. 11. Notice that if implemented correctly the DefensiveSquad class will provide the same functionality as the Squad class without having to provide an implementation for the remove_member, add_member and save_to_disk method. You must also add the following method to the Squad and DefensiveSquad classes: 1. A members_names method that must return a list with the names (only the names) of all the members of the squad (including the leader). 2. The DefensiveSquad class must also provide an implementation of this method that will first call the method in the super class and then add the name of the medic to the list. For this assignment you must provide two driver programs: 1. You must provide a driver script that creates five characters and add them to a defensive squad and save the squad to disk (remember the last member you pass in to the constructor will be the medic). Then you should remove all the five characters and save the squad to disk; because the medic can't be removed the squad will have only the medic (the leader should be removed because there is at least one person in the squad, this is a condition from the previous assignment). Finally you should add two characters to the squad, the first one the be added should automatically become the leader; then save the squad to disk and print it to console. 2. You must provide another driver program where you create enough characters to populate two squads, a normal squad and a defensive squad. In this program you must also write a function that given a list of squads will return a list with all the members on these squads. Notice that the squads in the list can be either a regular Squad or a DefensiveSquad. Because both classes implement the members_names method you can treat them polymorphically. Driver Script and Sample Run First Driver As shown in Figure 1, the first driver program creates five characters and add them to a defensive squad. Subsequently all the characters are removed and finally two characters are added to the squad. At each step the squad is saved to disk and the final squad is also printed to console. Figure 2 shows the console output after executing the first driver program. Figure 3 shows the content of the first saved file, with the inclusion of the medic. From this example you should be able to infer the content of the other two files. import * First we create the four initial characters for the squad henry = Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 82) diana = Character("Diana 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) mary - Character("Mary Montgomery", "Magic Hands", "Nurse", 4.4, 18) # We create the squad and save it to disk squad - DefensiveSquad("A gang with a medic", henry, diana, frank, james, mary) the_leader = squad.get_member(e) the leader. Level_up_ain() squad.save_to_disk("squad_day1.txt") and now they are all gone :( squad.remove_memberChenry) squad.remove_member(frank) squad.remove_member(diana) squad.remove_member(James) squad.remove_member(mary) I remember the medie can't be removed squad.save_to_disk("squad_day2.txt") print (f"The gang now has {squad.squad_size} members.) # Johnny joins the squad and James also rejoins johnny = Character("Johnny Silverado", "Fast and Furious", "Town Marshal", 2.9, 98) squad.add_nenber(james) squad.add_member(johnny) squad.save_to_disk("squad_day3.txt") print(squad) Figure 1: First Driver program for testing the DefensiveSquad class. The gang now has 1 members. *** A gang with a medic *** LEADER: James Montgomery The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 0.88 MEMBER 1: Johnny Silverado - Fast and Furious Profession: Town Marshal Draw speed: 2.9 Aim: 0.98 MEDIC: Mary Montgomery - Magic Hands Profession: Nurse Draw speed: 4.4 Aim: 0.18 Process finished with exit code o Figure 2: Output for running the first driver. Second Driver In the second driver program you must create enough characters to populate two squads, a normal squad and a defensive squad. In this program you must also write a function (all_names) that given a list of squads will return a list with all the members on these squads. Notice that the squads in the list can be either a regular Squad or a DefensiveSquad. Because both classes implement the members_names method you can treat them polymorphically. Figure 4 shows the part of the code where the characters are instantiated, then the two squads are created and added to a list; finally the function all_names is called passing in the list with the squads. Figure 5 shows the console output, i.e. the list containing the names of all the characters present in both squads. 5 squad_day 1.txt *** A gang with a medic *** LEADER: Billy the Kid - A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 Aim: 87 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 Draw speed: 4.1 40 MEMBER 3: James Montgomery - The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 88 MEDIC: Mary Montgomery - Magic Hands Profession: Nurse Draw speed: 4.4 Aim: 18 Aim: Figure 3: Output for running the driver. henry : Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 0.820) diana = Character("Diana Gold", "The richest merchant of the west", "Merchant", 5.4. 8.54) frank = Character("Frank the Creep", "Knows his knifes", "Butcher, 4.1, 0.4) james = Character("James Montgomery", "The fastest gun in the west", "Hired Gun", 2.1, 0.88) johnny = Character("Johnny Silverado", "Fast and Furious", "Town Marshal", 2.9, 8.98) mary = Character("Mary Montgomery", "Magic Hands", "Nurse", 4.4, 0.18) chris = Character("Chris McDonald", "The Sheriff in town", "Sheriff", 2.4, 0.78) joe = Character("Joe Holmes", "sloppy gunner", "Gunfighter", 4.4, 0.28) teani = Team("Billy's gang", henry, diana, frank, joe) team1.remove_member(Frank) tean2 = Defensive Team("A gang with a medic", james, johnny, chris, frank, mary) tean_list = [teami, tean2] print(all_names (team_list)) Figure 4: Partial code for the second drive program, the image doesn't show the code for the function all_names function which you must implement. file using this format. 6 ['Billy the Kid', 'Diana Gold', 'Joe Holmes', 'Janes Montgomery', 'Johnny Silverado, "Chris McDonald', 'Frank the Creep', 'Mary Montgomery'] Process finished with exit code @ Figure 5: Output for running the driver. The Problem In previous assignments we have created a Character class for our characters and a Squad class for creating a gang with various characters. In this assignment we want to create a new kind of squad: a defensive squad. Besides the leader and the regular members, a defensive squad will also include a medic (or healer). The medic is a normal character with the distinction that it can't be removed from the squad. In this assignment you must create a class for a defensive squad. Because a defensive squad is a particular case of a regular squad, you must provide an implemen- tation of the DefensiveSquad class that inherits from the Squad class. This inheritance will provide two important advantages: it will allow you to reuse most of the code you already implemented for the Squad class (we are not talking about copy-pasting the previous code) and it will also allow you to treat squads polymorphically. The DefensiveSquad class should have the following: 1. It must inherit from the Squad class. 2. The class constructor will receive the name of the squad and five members, the first member should be the leader, the fifth must become the medic. 3. The class constructor must call the constructor of the super class and then complete any other required operation. 4. You must use name mangling to make all these instance variables inaccessible from outside the class. 5. There must be a getter and a setter for the medic. 6. You must provide a design that guarantees that the medic can't be removed from the squad, without overriding the remove_member method from the Squad class. 7. The class must contain a method squad_size that will return the number of members in the squad (including the leader and the medic). 8. The class must contain a method get_member that will return a member given it's index (the medic should be the last member). Depending on your implementation you may have to overwrite this method to account for the medic, but you can reuse the code of the Squad class by calling it inside this method. 9. You must implement the __str__ method for printing the squad's info in a format similar to the one shown in the example. You must implement this method by reusing (calling the --str__ method from the super class and only add the information about the medic. 10. You must also provide appropriate docstring documentation for classes and methods. 11. Notice that if implemented correctly the DefensiveSquad class will provide the same functionality as the Squad class without having to provide an implementation for the remove_member, add_member and save_to_disk method. You must also add the following method to the Squad and DefensiveSquad classes: 1. A members_names method that must return a list with the names (only the names) of all the members of the squad (including the leader). 2. The DefensiveSquad class must also provide an implementation of this method that will first call the method in the super class and then add the name of the medic to the list. For this assignment you must provide two driver programs: 1. You must provide a driver script that creates five characters and add them to a defensive squad and save the squad to disk (remember the last member you pass in to the constructor will be the medic). Then you should remove all the five characters and save the squad to disk; because the medic can't be removed the squad will have only the medic (the leader should be removed because there is at least one person in the squad, this is a condition from the previous assignment). Finally you should add two characters to the squad, the first one the be added should automatically become the leader; then save the squad to disk and print it to console. 2. You must provide another driver program where you create enough characters to populate two squads, a normal squad and a defensive squad. In this program you must also write a function that given a list of squads will return a list with all the members on these squads. Notice that the squads in the list can be either a regular Squad or a DefensiveSquad. Because both classes implement the members_names method you can treat them polymorphically. Driver Script and Sample Run First Driver As shown in Figure 1, the first driver program creates five characters and add them to a defensive squad. Subsequently all the characters are removed and finally two characters are added to the squad. At each step the squad is saved to disk and the final squad is also printed to console. Figure 2 shows the console output after executing the first driver program. Figure 3 shows the content of the first saved file, with the inclusion of the medic. From this example you should be able to infer the content of the other two files. import * First we create the four initial characters for the squad henry = Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 82) diana = Character("Diana 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) mary - Character("Mary Montgomery", "Magic Hands", "Nurse", 4.4, 18) # We create the squad and save it to disk squad - DefensiveSquad("A gang with a medic", henry, diana, frank, james, mary) the_leader = squad.get_member(e) the leader. Level_up_ain() squad.save_to_disk("squad_day1.txt") and now they are all gone :( squad.remove_memberChenry) squad.remove_member(frank) squad.remove_member(diana) squad.remove_member(James) squad.remove_member(mary) I remember the medie can't be removed squad.save_to_disk("squad_day2.txt") print (f"The gang now has {squad.squad_size} members.) # Johnny joins the squad and James also rejoins johnny = Character("Johnny Silverado", "Fast and Furious", "Town Marshal", 2.9, 98) squad.add_nenber(james) squad.add_member(johnny) squad.save_to_disk("squad_day3.txt") print(squad) Figure 1: First Driver program for testing the DefensiveSquad class. The gang now has 1 members. *** A gang with a medic *** LEADER: James Montgomery The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 0.88 MEMBER 1: Johnny Silverado - Fast and Furious Profession: Town Marshal Draw speed: 2.9 Aim: 0.98 MEDIC: Mary Montgomery - Magic Hands Profession: Nurse Draw speed: 4.4 Aim: 0.18 Process finished with exit code o Figure 2: Output for running the first driver. Second Driver In the second driver program you must create enough characters to populate two squads, a normal squad and a defensive squad. In this program you must also write a function (all_names) that given a list of squads will return a list with all the members on these squads. Notice that the squads in the list can be either a regular Squad or a DefensiveSquad. Because both classes implement the members_names method you can treat them polymorphically. Figure 4 shows the part of the code where the characters are instantiated, then the two squads are created and added to a list; finally the function all_names is called passing in the list with the squads. Figure 5 shows the console output, i.e. the list containing the names of all the characters present in both squads. 5 squad_day 1.txt *** A gang with a medic *** LEADER: Billy the Kid - A dangerous outlaw Profession: Gunfighter Draw speed: 3.2 Aim: 87 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 Draw speed: 4.1 40 MEMBER 3: James Montgomery - The fastest gun in the west Profession: Hired Gun Draw speed: 2.1 Aim: 88 MEDIC: Mary Montgomery - Magic Hands Profession: Nurse Draw speed: 4.4 Aim: 18 Aim: Figure 3: Output for running the driver. henry : Character("Billy the Kid", "A dangerous outlaw", "Gunfighter", 3.2, 0.820) diana = Character("Diana Gold", "The richest merchant of the west", "Merchant", 5.4. 8.54) frank = Character("Frank the Creep", "Knows his knifes", "Butcher, 4.1, 0.4) james = Character("James Montgomery", "The fastest gun in the west", "Hired Gun", 2.1, 0.88) johnny = Character("Johnny Silverado", "Fast and Furious", "Town Marshal", 2.9, 8.98) mary = Character("Mary Montgomery", "Magic Hands", "Nurse", 4.4, 0.18) chris = Character("Chris McDonald", "The Sheriff in town", "Sheriff", 2.4, 0.78) joe = Character("Joe Holmes", "sloppy gunner", "Gunfighter", 4.4, 0.28) teani = Team("Billy's gang", henry, diana, frank, joe) team1.remove_member(Frank) tean2 = Defensive Team("A gang with a medic", james, johnny, chris, frank, mary) tean_list = [teami, tean2] print(all_names (team_list)) Figure 4: Partial code for the second drive program, the image doesn't show the code for the function all_names function which you must implement. file using this format. 6 ['Billy the Kid', 'Diana Gold', 'Joe Holmes', 'Janes Montgomery', 'Johnny Silverado, "Chris McDonald', 'Frank the Creep', 'Mary Montgomery'] Process finished with exit code @ Figure 5: Output for running the driverStep 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