Question: I would like to take this code from Python and make it Kotlin import csv class Weather: def __init__(self, date, precipitation, snowfall, snow_depth, max_temperature, min_temperature,

I would like to take this code from Python and make it Kotlin

import csv

class Weather:

def __init__(self, date, precipitation, snowfall, snow_depth, max_temperature, min_temperature, avg_temperature, heating_degree_days, cooling_degree_days, sunrise, sunset, length_of_day):

self.date = date

self.precipitation = precipitation

self.snowfall = snowfall

self.snow_depth = snow_depth

self.max_temperature = max_temperature

self.min_temperature = min_temperature

self.avg_temperature = avg_temperature

self.heating_degree_days = heating_degree_days

self.cooling_degree_days = cooling_degree_days

self.sunrise = sunrise

self.sunset = sunset

self.length_of_day = length_of_day

def readData():

with open('weather.db') as csvfile:

reader = csv.reader(csvfile)

next(reader)

for row in reader:

date = row[0]

precipitation = float(row[1])

snowfall = float(row[2])

snow_depth = float(row[3])

max_temperature = float(row[4])

min_temperature = float(row[5])

avg_temperature = float(row[6])

heating_degree_days = float(row[7])

cooling_degree_days = float(row[8])

sunrise = row[9]

sunset = row[10]

length_of_day = row[11]

key = getKey(date)

weatherDB[key] = Weather(date, precipitation, snowfall, snow_depth, max_temperature, min_temperature, avg_temperature, heating_degree_days, cooling_degree_days, sunrise, sunset, length_of_day)

def getKey(date):

parts = date.split('/')

month = parts[0].rjust(2, '0')

day = parts[1].rjust(2, '0')

return f"{month}/{day}"

def printHeader():

print(f"{'Date':<10}{'Precipitation':<15}{'Snowfall':<10}{'Snow Depth':<10}{'Max Temp.':<10}{'Min Temp.':<10}{'Avg Temp.':<10}{'HDD':<10}{'CDD':<10}{'Sunrise':<10}{'Sunset':<10}{'Length of Day':<15}")

def printAllRecords():

printHeader()

for key in weatherDB:

weather = weatherDB[key]

print(f"{weather.date:<10}{weather.precipitation:<15}{weather.snowfall:<10}{weather.snow_depth:<10}{weather.max_temperature:<10}{weather.min_temperature:<10}{weather.avg_temperature:<10}{weather.heating_degree_days:<10}{weather.cooling_degree_days:<10}{weather.sunrise:<10}{weather.sunset:<10}{weather.length_of_day:<15}")

def printRecordsFromSingleMonth():

month = input("Enter month (MM): ")

printHeader()

for key in weatherDB:

if key.startswith(month):

weather = weatherDB[key]

print(f"{weather.date:<10}{weather.precipitation:<15}{weather.snowfall:<10}{weather.snow_depth:<10}{weather.max_temperature:<10}{weather.min_temperature:<10}{weather.avg_temperature:<10}{weather.heating_degree_days:<10}{weather.cooling_degree_days:<10}{weather.sunrise:<10}{weather.sunset:<10}{weather.length_of_day:<15}")

def printRecordWithHighestTemperature():

"""

Prints the record with the highest recorded temperature in the weather database.

"""

max_temp = -float('inf')

for key in weatherDB:

weather = weatherDB[key]

if weather.max_temperature > max_temp:

max_temp = weather.max_temperature

max_temp_record = weather

printHeader()

print(f"{max_temp_record.date:<10}{max_temp_record.precipitation:<15}{max_temp_record.snowfall:<10}{max_temp_record.snow_depth:<10}{max_temp_record.max_temperature:<10}{max_temp_record.min_temperature:<10}{max_temp_record.avg_temperature:<10}{max_temp_record.heating_degree_days:<10}{max_temp_record.cooling_degree_days:<10}{max_temp_record.sunrise:<10}{max_temp_record.sunset:<10}{max_temp_record.length_of_day:<15}")

def printRecordWithLowestTemperature():

"""

Prints the record with the lowest recorded temperature in the weather database.

"""

min_temp = float('inf')

for key in weatherDB:

weather = weatherDB[key]

if weather.min_temperature < min_temp:

min_temp = weather.min_temperature

min_temp_record = weather

printHeader()

print(f"{min_temp_record.date:<10}{min_temp_record.precipitation:<15}{min_temp_record.snowfall:<10}{min_temp_record.snow_depth:<10}{min_temp_record.max_temperature:<10}{min_temp_record.min_temperature:<10}{min_temp_record.avg_temperature:<10}{min_temp_record.heating_degree_days:<10}{min_temp_record.cooling_degree_days:<10}{min_temp_record.sunrise:<10}{min_temp_record.sunset:<10}{min_temp_record.length_of_day:<15}")

def printRecordWithHighestTemperatureFromSingleMonth():

"""

Prints the record with the highest recorded temperature from a single month in the weather database.

"""

month = input("Enter month (MM): ")

max_temp = -float('inf')

for key in weatherDB:

if key.startswith(month):

weather = weatherDB[key]

if weather.max_temperature > max_temp:

max_temp = weather.max_temperature

max_temp_record = weather

printHeader()

print(f"{max_temp_record.date:<10}{max_temp_record.precipitation:<15}{max_temp_record.snowfall:<10}{max_temp_record.snow_depth:<10}{max_temp_record.max_temperature:<10}{max_temp_record.min_temperature:<10}{max_temp_record.avg_temperature:<10}{max_temp_record.heating_degree_days:<10}{max_temp_record.cooling_degree_days:<10}{max_temp_record.sunrise:<10}{max_temp_record.sunset:<10}{max_temp_record.length_of_day:<15}")

def printRecordWithLowestTemperatureFromSingleMonth():

"""

Prints the record with the lowest recorded temperature from a single month in the weather database.

"""

month = input("Enter month (MM): ")

min_temp = float('inf')

min_temp_record = None

for key in weatherDB:

if key.startswith(month):

weather = weatherDB[key]

if weather.min_temperature < min_temp:

min_temp = weather.min_temperature

min_temp_record = weather

printHeader()

print(f"{min_temp_record.date:<10}{min_temp_record.precipitation:<15}{min_temp_record.snowfall:<10}{min_temp_record.snow_depth:<10}{min_temp_record.max_temperature:<10}{min_temp_record.min_temperature:<10}{min_temp_record.avg_temperature:<10}{min_temp_record.heating_degree_days:<10}{min_temp_record.cooling_degree_days:<10}{min_temp_record.sunrise:<10}{min_temp_record.sunset:<10}{min_temp_record.length_of_day:<15}")

def printAveragePrecipitationFromSingleMonth():

"""

Prints the average of all precipitation from a single month in the weather database.

"""

month = input("Enter month (MM): ")

total_precipitation = 0

count = 0

for key in weatherDB:

if key.startswith(month):

weather = weatherDB[key]

total_precipitation += weather.precipitation

count += 1

if count > 0:

average_precipitation = total_precipitation / count

print(f"Average precipitation for month {month}: {average_precipitation:.2f}")

else:

print(f"No data found for month {month}")

def menu():

"""

Displays the menu and executes the selected option.

"""

while True:

print(" Menu:")

print("1. Print All Records")

print("2. Print Only Records from a single month")

print("3. Print Record with Highest Recorded Temperature")

print("4. Print Record with Lowest Recorded Temperature")

print("5. Print Record with Highest Recorded Temperature from a single month")

print("6. Print Record with Lowest Recorded Temperature from a single month")

print("7. Print Average of all precipitation from a single month")

print("8. Quit")

choice = input("Enter your choice: ")

if choice == '1':

printAllRecords()

elif choice == '2':

printRecordsFromSingleMonth()

elif choice == '3':

printRecordWithHighestTemperature()

elif choice == '4':

printRecordWithLowestTemperature()

elif choice == '5':

printRecordWithHighestTemperatureFromSingleMonth()

elif choice == '6':

printRecordWithLowestTemperatureFromSingleMonth()

elif choice == '7':

printAveragePrecipitationFromSingleMonth()

elif choice == '8':

print("Goodbye!")

break

else:

print("Invalid choice. Please try again.")

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!