Home / Expert Answers / Computer Science / please-complete-the-code-below-main-py-from-instrument-import-instrument-from-stringinstrument-imp-pa825

(Solved): Please complete the code below: main.py from Instrument import Instrument from StringInstrument imp ...



Given the base class Instrument, define the _init _ method (the constructor) for a derived class KeyboardInstrument for keyboComplete the KeyboardInstrument method format_instrument_info(self) \( \rightarrow \) str which returns a formatted str as deComplete the function read_instrument_objects(file_name:str) -> list[Instrument | StringInstrument | Keyboardinstrument ] asComplete the function most_expensive_instrument (instrument_objects: list [Instrument | StringInstrument | KeyboardInstrumentPlease complete the code below:
main.py

from Instrument import Instrument
from StringInstrument import StringInstrument
import globals
import csv

class KeyboardInstrument(Instrument):
########################################################################
# Task 1 - define the constructor __init__
# NOTE: this must call the base class constructor
# parameters name:str, manufacturer:str, year_built:int, cost:int, num_keys:int, keyboard_type:str
########################################################################
def __init__(): # Fix this
#
# your code goes here
#


########################################################################
# Task 2 - complete this method
# NOTE: this must use the format_instrument method of the base class (Instrument)
# format_instrument_info(self) -> str
########################################################################
def format_instrument_info(self):
#
# your code goes here
#
return 'None' # Fix this

# ########################################################################
# Task 3 - complete this function
# read_instrument_objects(file_name: str) -> list[Instrument | StringInstrument | KeyboardInstrument]
########################################################################
def read_instrument_objects(file_name):
#
# your code goes here
#
return [] # Fix this



# ########################################################################
# Task 4 - complete this function
# most_expensive_instrument(instrument_objects: list[Instrument | StringInstrument | KeyboardInstrument])
# -> Instrument | StringInstrument | KeyboardInstrument
########################################################################
def most_expensive_instrument(instrument_objects):
#
# your code goes here
#
return None # Fix this


#######################################################################
# The following code is provided as a starting point for development and testing
# Please modify the code below as you develop your program.
#
# NOTE: you can submit WITH or WITHOUT the following main included
#######################################################################
# if __name__ == "__main__":

# globals.initialise_call_count()

# ins1 = Instrument('Drums','Zildjian',2015,2500)
# ins2 = Instrument('piano','steinway',1800,450000)
# ins3 = StringInstrument('Guitar','Gibson',2002,1200,6,19)
# ins4 = StringInstrument('Ukulele','Kala',2018,50,6,12)
# ins5 = KeyboardInstrument('piano','steinway',1700,500000,88,'grand')
# ins6 = KeyboardInstrument('Piano','yamaha',2010,2000,88,'electric')

#######################################################################
# Test code for Task 1, 2 - class constructor and method
#######################################################################
# print(ins5.format_instrument_info())

#######################################################################
# Test code for Task 3 - function read_instrument_objects
#######################################################################
# instrument_object_list = read_instrument_objects('instrument_info.csv')
# print('INSTRUMENT LIST')
# for ins in instrument_object_list:
# print(ins.format_instrument_info())

#######################################################################
# Test code for Task 4 - most_expensive_instrument
#######################################################################
# most_expensive = most_expensive_instrument([ins1, ins2, ins3, ins4, ins5, ins6])
# print('MOST EXPENSIVE:')
# print(most_expensive.format_instrument_info())

globals.py


def initialise_call_count():
global global_call_count
global_call_count = 0

def get_call_count():
return global_call_count

def increment_call_count():
global global_call_count
global_call_count += 1

instrument.py

import globals

class Instrument:
def __init__(self, name, manufacturer, year_built, cost):
globals.increment_call_count() # DO NOT touch this line

self.name = name
self.manufacturer = manufacturer
self.year_built = year_built
self.cost = cost


def format_instrument_info(self):
globals.increment_call_count() # DO NOT touch this line

info_str = ''
info_str += 'Instrument Information:\n'
info_str += f'Name: {self.name}\n'
info_str += f'Manufacturer: {self.manufacturer}\n'
info_str += f'Year built: {self.year_built}\n'
info_str += f'Cost: {self.cost}\n'

return info_str


StringInstrument.py

from Instrument import Instrument

class StringInstrument(Instrument):
def __init__(self, name, manufacturer, year_built, cost, num_strings, num_frets):
super().__init__(name, manufacturer, year_built, cost)
self.num_strings = num_strings
self.num_frets = num_frets

Given the base class Instrument, define the _init _ method (the constructor) for a derived class KeyboardInstrument for keyboard instruments. Note: this must call the __init__ method (constructor) for the base class The parameters for this method are: - name: str - the name of the type of instrument - manufacturer: str - the name of the manufacturer - year_built: int - the year the instrument was built - cost: int - the cost of the instrument. - num_keys: int - the number of keys on the keyboard - keyboard_type: str - the type of the keyboard Complete the KeyboardInstrument method format_instrument_info(self) str which returns a formatted str as detailed in the examples below. NOTE: this must call the format_instrument_info method of the base class (Instrument) For example, for the following Keyboardinstrument object: Keyboardinstrument ('piano', 'steinway', , 'grand') the method returns the following str: nNumber of Keys: 88\nType of Keyboard: grand Complete the function read_instrument_objects(file_name:str) -> list[Instrument | StringInstrument | Keyboardinstrument ] as follows: - the function takes a str as input, the name of a .csv file to read lines from. The format of the file, is lines, where each line in the file has the following comma separated fields: - for each line in the input, the function determines the appropriate class (either Instrument, KeyboardInstrument or StringInstrument from Field 1), generates an instance of the class and adds it to the list to be returned. - the function returns a list [Instrument | StringInstrument | KeyboardInstrument], a list that can contain Instrument, StringInstrument, and Keyboardinstrument objects For example, for the file instrument_info.csv the function should return: [Instrument ('Drums', 'Zildjian',2015, 2500), Keyboardinstrument ('Piano', 'Steinway', 1800, 750000,88, 'grand'), StringInstrument('Guitar', 'Gibson',2002,1200,6,19), Keyboardinstrument('Piano', 'Yamaha', 1979,10000,88, 'upright'), Instrument ('Clarinet','Yamaha',2017, 1371)] Complete the function most_expensive_instrument (instrument_objects: list [Instrument | StringInstrument | KeyboardInstrument]) -> Instrument| StringInstrument | Keyboardinstrument as follows: - the function takes as input a list of objects (as output by the function in Task 3 detailed above) - the function returns a single object which is the most expensive (has the largest cost). This can be an instance of either the Instrument class, StringInstrument or Keyboardinstrument. NOTE: you can assume your code will be tested with lists that have a single most expensive instrument. For example, for the following input list of objects: [Instrument ('Drums', 'Zildjian', 2015, 2500), Keyboardinstrument('Piano', 'Steinway', 1800,750000, 88, 'grand'), StringInstrument('Guitar', 'Gibson', 2002, 1200, 6, 19), Keyboardinstrument('Piano', 'Yamaha', 1979, 10000,88, 'upright'), Instrument ('Clarinet', 'Yamaha',2017, 1371)] the function would return the following object: Keyboardinstrument ('Piano', 'Steinway', 1800,750000,88, 'grand')


We have an Answer from Expert

View Expert Answer

Expert Answer


Task 1:- Python code to update the constructorfrom Instrument import Instrumentimport globalsclass KeyboardInstrument(Instrument):####################
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe