From b5a95e18896c23ff65c2b70eea480000f2379ef0 Mon Sep 17 00:00:00 2001 From: Brittany Date: Thu, 1 Oct 2015 11:23:19 -0700 Subject: [PATCH] Add solar system files --- Planet.rb | 21 ++++++++ SolarSystem.rb | 34 +++++++++++++ solar_system_program.rb | 109 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 Planet.rb create mode 100644 SolarSystem.rb create mode 100644 solar_system_program.rb diff --git a/Planet.rb b/Planet.rb new file mode 100644 index 00000000..06b5db9a --- /dev/null +++ b/Planet.rb @@ -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) + 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 diff --git a/SolarSystem.rb b/SolarSystem.rb new file mode 100644 index 00000000..5043a631 --- /dev/null +++ b/SolarSystem.rb @@ -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) + 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." + 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 diff --git a/solar_system_program.rb b/solar_system_program.rb new file mode 100644 index 00000000..f9671840 --- /dev/null +++ b/solar_system_program.rb @@ -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) +milky_way.print_out() +puts "" + +#test using a default value attribute +my_planet.weight_on_planet(130) +my_planet.weight_on_planet() +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)