Skip to content
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

Open
wants to merge 1 commit into
base: bmk/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Planet.rb
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)

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

@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)

Choose a reason for hiding this comment

The 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
34 changes: 34 additions & 0 deletions SolarSystem.rb
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)

Choose a reason for hiding this comment

The 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 concat method which will concatenate the whole array so:
@planets.concat(planets_array)

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."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job using the abs to ensure you get a positive value

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
109 changes: 109 additions & 0 deletions solar_system_program.rb
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)

Choose a reason for hiding this comment

The 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()

Choose a reason for hiding this comment

The 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)