-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import math | ||
|
||
|
||
class Moon: | ||
def __init__(self, name, radius, contains_water=False): | ||
self.name = name | ||
self.radius = radius # in kilometers | ||
self.contains_water = contains_water | ||
|
||
def surface_area(self) -> float: | ||
"""Calculate the surface area of the moon assuming a spherical shape.""" | ||
return 4.0 * math.pi * self.radius**2 | ||
|
||
def __repr__(self): | ||
return f"Moon(name={self.name!r}, radius={self.radius}, contains_water={self.contains_water})" | ||
|
||
|
||
europa = Moon(name="Europa", radius=1560.8, contains_water=True) | ||
|
||
print(europa) | ||
print(f"Surface area (km^2) of {europa.name}: {europa.surface_area()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dataclasses import dataclass | ||
import math | ||
|
||
|
||
@dataclass | ||
class Moon: | ||
name: str | ||
radius: float # in kilometers | ||
contains_water: bool = False | ||
|
||
def surface_area(self) -> float: | ||
"""Calculate the surface area of the moon assuming a spherical shape.""" | ||
return 4.0 * math.pi * self.radius**2 | ||
|
||
|
||
europa = Moon(name="Europa", radius=1560.8, contains_water=True) | ||
|
||
print(europa) | ||
print(f"Surface area (km^2) of {europa.name}: {europa.surface_area()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import math | ||
|
||
|
||
def surface_area(radius: float) -> float: | ||
return 4.0 * math.pi * radius**2 | ||
|
||
|
||
europa = {"name": "Europa", "radius": 1560.8, "contains_water": True} | ||
|
||
|
||
print(europa) | ||
print(f"Surface area (km^2) of {europa['name']}: {surface_area(europa['radius'])}") |