-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add solar system files #1
base: bmk/master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Planet | ||
attr_reader :name, :diameter, :weight_multiplier, :num_moons, :rate_solar_rotation, :distance_from_sun | ||
|
||
def initialize(info_hash) | ||
@name = info_hash[:name] | ||
@diameter = info_hash[:diameter] | ||
@weight_multiplier = info_hash[:weight_multiplier] | ||
@num_moons = info_hash[:num_moons] | ||
@rate_solar_rotation = info_hash[:rate_solar_rotation] | ||
@distance_from_sun = info_hash[:distance_from_sun] | ||
end | ||
|
||
#using a default value attribute | ||
def weight_on_planet(my_weight = 100) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job using the default value |
||
if my_weight != 100 | ||
puts "Your weight on #{@name} is #{@weight_multiplier * my_weight}lbs." | ||
else | ||
puts "If your weight was 100lbs on Earth, your weight on #{@name} would be #{@weight_multiplier * my_weight}lbs." | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class SolarSystem | ||
|
||
def initialize(name, formation_year) | ||
@name = name | ||
@planets = [] | ||
@formation_year = formation_year | ||
end | ||
|
||
def add_planet(planet) | ||
@planets.push(planet) | ||
end | ||
|
||
def print_out | ||
@planets.each do |planet| | ||
puts planet.name | ||
end | ||
end | ||
|
||
def add_planets(planets_array) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good use of an iterator to add all of the planets. You could also use the |
||
planets_array.each do |planet| | ||
@planets.push(planet) | ||
end | ||
end | ||
|
||
def distance_between(planet1, planet2) | ||
puts "The distance between #{planet1.name} and #{planet2.name} is #{(planet1.distance_from_sun.to_i - planet2.distance_from_sun.to_i).abs} km." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job using the |
||
end | ||
|
||
#NEED TO FINISH THIS | ||
def local_year(planet) | ||
puts "The local year on #{planet.name} is #{(((Time.now.year - @formation_year) * 365.25)/planet.rate_solar_rotation + @formation_year).to_i}." | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#tests that solar system wave 2 requirements are met | ||
|
||
require "./SolarSystem.rb" | ||
require "./Planet.rb" | ||
|
||
#data | ||
mercury = Planet.new({ | ||
name: "Mercury", | ||
diameter: "4,878 km", | ||
weight_multiplier: 0.38, | ||
num_moons: 0, | ||
rate_solar_rotation: 87.96, | ||
distance_from_sun: 57900000 | ||
}) | ||
venus = Planet.new({ | ||
name: "Venus", | ||
diameter: "12,100 km", | ||
weight_multiplier: 0.88, | ||
num_moons: 0, | ||
rate_solar_rotation: 224.68, | ||
distance_from_sun: 108200000 | ||
}) | ||
|
||
earth = Planet.new({ | ||
name: "Earth", | ||
diameter: "12,756 km", | ||
weight_multiplier: 1, | ||
num_moons: 1, | ||
rate_solar_rotation: 365.25, | ||
distance_from_sun: 149600000 | ||
}) | ||
|
||
mars = Planet.new({ | ||
name: "Mars", | ||
diameter: "6,785 km", | ||
weight_multiplier: 0.38, | ||
num_moons: 2, | ||
rate_solar_rotation: 686.98, | ||
distance_from_sun: 227900000 | ||
}) | ||
|
||
jupiter = Planet.new({ | ||
name: "Jupiter", | ||
diameter: "139,822 km", | ||
weight_multiplier: 2.36, | ||
num_moons: 67, | ||
rate_solar_rotation: 4380, | ||
distance_from_sun: 778300000 | ||
}) | ||
|
||
saturn = Planet.new({ | ||
name: "Saturn", | ||
diameter: "116,464 km", | ||
weight_multiplier: 0.91, | ||
num_moons: 62, | ||
rate_solar_rotation: 10585, | ||
distance_from_sun: 886700000 | ||
}) | ||
|
||
uranus= Planet.new({ | ||
name: "Uranus", | ||
diameter: "51,488 km", | ||
weight_multiplier: 0.89, | ||
num_moons: 27, | ||
rate_solar_rotation: 30660, | ||
distance_from_sun: 1784000000 | ||
}) | ||
|
||
neptune = Planet.new({ | ||
name: "Neptune", | ||
diameter: "49,493 km", | ||
weight_multiplier: 1.13, | ||
num_moons: 14, | ||
rate_solar_rotation: 60225, | ||
distance_from_sun: 4497100000 | ||
}) | ||
|
||
my_planet = Planet.new({ | ||
name: "My Planet", | ||
diameter: "500 miles", | ||
weight_multiplier: 0.76, | ||
num_moons: 900, | ||
rate_solar_rotation: 365, | ||
distance_from_sun: 3089098 | ||
}) | ||
|
||
#test adding planets, first just one, then an array | ||
milky_way = SolarSystem.new("Milky Way", 200) | ||
milky_way.add_planet(my_planet) | ||
milky_way.print_out() | ||
puts "" | ||
planets = [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune] | ||
milky_way.add_planets(planets) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
milky_way.print_out() | ||
puts "" | ||
|
||
#test using a default value attribute | ||
my_planet.weight_on_planet(130) | ||
my_planet.weight_on_planet() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Ruby, when you are not passing any parameters, we generally try not to include the extra |
||
puts "" | ||
|
||
#calculating distance between planets | ||
milky_way.distance_between(my_planet, mercury) | ||
milky_way.distance_between(mercury, my_planet) | ||
milky_way.distance_between(venus, mars) | ||
|
||
#caclulating local year | ||
milky_way.local_year(mercury) | ||
milky_way.local_year(earth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job using the hash here