Question: Convert the following python code into java code # print header for forwarding database(FDB) def printHeader(outFile: str): outFile.write(------------------------------------------------- ) outFile.write(| Indx | Host | Port

Convert the following python code into java code

# print header for forwarding database(FDB) def printHeader(outFile: str): outFile.write("------------------------------------------------- ") outFile.write("| Indx | Host | Port | ") outFile.write("------------------------------------------------- ")

# print forwarding database(FDB) def printFDB(outFile: str, fdb: dict): index = 1 printHeader(outFile) for port, src_mac in fdb.items(): ..................outFile.write("|{0}|{1}|{2}| ".format(index, src_mac, port)) ..................index += 1 outFile.write(" ")

# number of ports in the bridge number_of_ports = int(input("Number of ports in the bridge: ")) if number_of_ports > 8: ...............print("Number of ports should be less than 9") ...............exit()

fileName = input("Enter input file name: ")

fdb = {} # empty dictionary to store MAC and its incoming port number

with open("fdb.txt", 'w') as outFile:

.............with open("inp.txt", 'r') as file: ...........................frames = file.readlines() # The variable "frames" is a list containing all lines in the file ...........................for frame in frames: .................................frame = frame.strip(' ') .................................try: ........................................src, dest, port = frame.split(" ") .........................................if int(port) > number_of_ports: ..............................................continue .................................except ValueError: .....................................continue

.................................if fdb.get(port) is None: .......................................fdb[port] = src .......................................outFile.write("Action: add to FDB (index {0}); forward to all out ports: ".format(len(fdb))) .................................else: .......................................index = list(fdb.keys()).index(port) .......................................outFile.write("Action: found at index {0}(no update); forward to port: ".format( index + 1))

.................................printFDB(outFile, fdb)

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!