Question: I am having issues getting my python program to work. This is what I have so far. #define the Instrument base class class Instrument: #initialize

I am having issues getting my python program to work. This is what I have so far.

#define the Instrument base class class Instrument:

#initialize the Instrument class def __init__(self, name, manufacturer, year_built, cost): self.__name = name self.__manufacurer = manufacturer self.__year_built = year_built self.__cost = cost

#mutators for the Instrument class def set_name(self, name): self.__name = name def set_manufacturer(self, manufacturer): self.__manufacturer = manufacturer def set_year_built(self, year_built): self.__year_built = year_built def set_cost(self, cost): self.__cost = cost

#accessor for the Instrument class def get_name(self): return self.__name def get_manufacturer(self): return self.__manufacturer def get_year_built(self): return self.__year_built def get_cost(self): return self.__cost

#define the String class class String(Instrument): #initialize the String attributes def __init__(self, name, manufacturer, year_built, cost, num_strings, num_frets, is_bowed): #call the superclass Instrument.__init__(self, name, manufacturer, year_built, cost) #String specific attributes self.__num_strings = num_strings self.__num_frets = num_frets self.__is_bowed = is_bowed

#mutators for the String class def set_num_strings(self, num_strings): self.__num_strings = num_strings def set_num_frets(self, num_frets): self.__num_frets = num_frets def set_is_bowed(self, is_bowed): self.__is_bowed = is_bowed

#accessor for the String class def get_num_strings(self): return self.__num_strings def get_num_frets(self): return self.__num_frets def get_is_bowed(self): return self.__is_bowed

#display information def display_strings(self): print(" Instrument Information:") print(" Name: ", self.__name) print(" Manufacturer: ", self.__manufacturer) print(" Year Built: ", self.__year_built) print(" Cost: $", self.__cost) print(" Number of strings: ", self.__num_strings) print(" Number of frets: ", self.__num_frets) print(" Is it bowed? ", self.__is_bowed)

#define the Wind class class Wind(Instrument): #initialize the Wind attributes def __init__(self, name, manufacturer, year_built, cost, surface_type, num_reeds, num_holes): #call the superclass Instrument.__init__(self, name, manufacturer, year_built, cost) #wind specific attributes self.__surface_type = surface_type self.__num_reeds = num_reeds self.__num_holes = num_holes

#mutators for the Wind class def set_surface_type(self, surface_type): self.__surface_type = surface_type def set_num_reeds(self, num_reeds): self.__num_reeds = num_reeds def set_num_holes(self, num_holes): self.__num_holes = num_holes

#accessor for the Wind class def get_surface_type(self): return self.__surface_type def get_num_strings(self): return self.__num_reeds def get_num_holes(self): return self.__num_holes

#display information def display_wind(self): print(" Instrument Information:") print(" Name: ", self.__name) print(" Manufacturer: ", self.__manufacturer) print(" Year Built: ", self.__year_built) print(" Cost: $", self.__cost) print(" Surface: ", self.__surface_type) print(" Number of reeds: ", self.__num_reeds) print(" Number of holes: ", self.__num_holes)

#define the Percussion class class Percussion(Instrument):

#initialize the Percussion attributes def __init__(self, name, manufacturer, year_built, cost, num_drumheads, shell_type, is_striking): Instrument.__init__(self, name, manufacturer, year_built, cost) #percussion specific attributes self.__num_drumheads = num_drumheads self.__shell_type = shell_type self.__is_striking = is_striking

#mutators for Percussion class def set_num_drumheads(self, num_drumheads): self.__num_drumheads = num_drumheads def set_shell_type(self, shell_type): self.__shell_type = shell_type def set_is_striking(self, is_striking): self.__is_striking = is_striking

#accessor for the Percussion class def get_num_drumheads(self): return self.__num_drumheads def get_shell_type(self): return slef.__shell_type def get_is_striking(self): return self.__is_striking

#display information def display_percussion(self): print(" Instrument Information:") print(" Name: ", self.__name) print(" Manufacturer: ", self.__manufacturer) print(" Year Built: ", self.__year_built) print(" Cost: $", self.__cost) print(" Number of drumheads: ", self.__num_drumheads) print(" Shell type: ", self.__shell_type) print(" Striking implement: ", self.__is_striking)

#define the main function def main():

#variables instruments = []

try:

with open('Instruments.txt', 'r') as f: for line in f: info = line.strip(' ') instrument_type = info[0] name = info[1] manufacturer = info[2] year_built = int(info[3]) cost = int(info[4])

#String instrument if instrument_type == 'S': num_strings = int(info[5]) num_frets = int(info[6]) is_bowed = info[7].lower() == 'true' instruments.append(String(name, manufacturer, year_built, cost, num_strings, num_frets, is_bowed))

#call the string display function String.display_string()

#Wind instrument elif instrument_type == 'W': surface_type = info[5] num_reeds = int(info[6]) num_holes = int(info[7]) instruments.append(Wind(name, manufacturer, year_built, cost, surface_type, num_reeds, num_holes))

#call the wind display function Wind.display_wind()

#Percussion instrument elif instrument_type == 'P': num_drumheads = int(info[5]) shell_type = info[6] is_striking = info[7].lower() == 'true' instruments.append(Percussion(name, manufacturer, year_built, cost, num_drumheads, shell_type, is_striking))

#call the percussion display function Percussion.display_percussion()

f.close()

except IOError: print("Instruments.txt file could not be located")

return instruments

#call the main function main()

The assignment is

Create a Python program using good object-oriented skills that demonstrates the use of inheritance within multiple classes of a project.

Create a base class named Instrument. This class will hold the info for all types of instruments:

Name (type of instrument: guitar, drums, etc...)

Manufacturer

Year built

Cost

Next, create these classes:

Strings

Wind

Percussion

String instruments will have these items:

number of strings

number of frets

is it bowed? (true or false)

Wind instruments will have these items:

surface type

number of reeds

number of holes

Percussion instruments will have these items:

number of drumheads (membranes)

shell type

striking instrument? (true or false)

Each of these three classes (String, Wind, and Percussion) should be inherited from the Instrument base class.

Populate the values according to the input data. In the input file, named Instruments.txt, a new instrument will begin with a 'P' for percussion, 'S' for string, and 'W' for wind.

Be sure to:

read until end of file

create a base class named Instrument

comment throughout the code

create 3 classes (String, Wind, Percussion) that will inherit from the Instrument class

use functions throughout the program. Each function should be modular and completes one task only.

add a header and closing statement

use classes and objects appropriately

the Instruments.txt file contains:

P Drums Zildjian 2015 2500 4 Wood True S Guitar Gibson 2002 1200 6 19 False W Flute Yahama 2020 1550 Silver 0 16

The output has to be:

I am having issues getting my python program to work. This is

I am receiving an index error and I am not sure if my data will display properly. Please help! Thank you!

Thonntwntwnond hen

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!