diff --git a/wave1.rb b/wave1.rb new file mode 100644 index 00000000..f61dbeb6 --- /dev/null +++ b/wave1.rb @@ -0,0 +1,55 @@ +#local variables will point to the planet objects +Mercury = Planet.new("Mercury", "4879", "0.39", "88days", "No" ) +Venus = Planet.new("Venus", "12104", "0.73", "225days", "No") +Earth = Planet.new("Earth", "12742", "1", "365.25days", "Yes") +Mars = Planet.new("Mars", "6779", "1.38", "1.9years", "No") +Jupiter = Planet.new("Jupiter", "139822", "5.20AU", "11.9years", "No") + +# puts Mercury.diameter +# puts Venus.diameter +# puts Mars.distance_from_the_sun +#puts Mercury.human_being_alive + +continue = true +while continue +planets = [Mercury, Venus, Earth, Mars, Jupiter] +puts "input numbers accoring to planet that you are interested in 1-Mercury, 2-Venus, 3-Earth, 4-Mars, 5-Jupiter!" + +user_input = gets.chomp.to_i +planet = planets[user_input-1] #user input is saved in planet +puts "The name of planet is #{planet.name}, diameter of #{planet.diameter}km, #{planet.distance_from_the_sun}AU, with #{planet.orbit_period}days of orbit period. Can we live there?:#{planet.human_being_alive} " + +puts "Would you like to explore anothe planet(y/n)?" +ans = gets.chomp +if ans == "n" + continue = false +end +end +#set up array of planets +#add a planet method + +class SolarSystem + + attr_reader :planets + + def initialize + @planets = [] #store planets within an array + end + + def add(planet) #in parameter no data type needed + @planets.push(planet) + end + + def print_names + @planets.each do |planet| + puts planet.name + end + end +end + +solar_system = SolarSystem.new #add planet into system local variable +solar_system.add(Venus) +solar_system.add(Mercury) +solar_system.add(Mars) +# print solar_system.planets #to view the element of @planets array instane varaible +solar_system.print_names diff --git a/wave2.rb b/wave2.rb new file mode 100644 index 00000000..5d875419 --- /dev/null +++ b/wave2.rb @@ -0,0 +1,45 @@ +#by Sofia Kim +#Create a Planet class with a name attribute. +#Allow these attributes to be set using a hash in initialize. +#You should be able to instantiate a new Planet object with an associated name. + + +class Planet + #getters/setters + attr_reader :name, :diameter, :distance_from_the_sun, :orbit_period, :human_being_alive + + #when I call .new this will be initialized as a package + def initialize(planet) + @name = planet[:name] + @diameter = planet[:diameter] + @distance_from_the_sun = planet[:distance_from_the_sun] + @orbit_period = planet[:orbit_period] + @human_being_alive = planet[:human_being_alive] #boolean + end + +end + +#add a planet method + +class SolarSystem + + attr_reader :planets + + def initialize + @planets = [] #store planets within an array + end + + def add(planet) #in parameter no data type needed + @planets.push(planet) + end + +end + +solar_system = SolarSystem.new #add planet into system local variable +solar_system.add(Planet.new(name:"Mercury", diameter:"4879", distance_from_the_sun:"0.39", orbit_period:"88days", human_being_alive:"No" )) +solar_system.add(Planet.new(name:"Venus", diameter:"12104", distance_from_the_sun:"0.73", orbit_period:"225days", human_being_alive:"No")) +solar_system.add(Planet.new(name:"Earth", diameter:"12742", distance_from_the_sun:"1", orbit_period:"365.25days", human_being_alive:"Yes")) +solar_system.add(Planet.new(name:"Mars", diameter:"6779", distance_from_the_sun:"1.38", orbit_period:"1.9years", human_being_alive:"No")) +solar_system.add(Planet.new(name:"Jupiter", diameter:"139822", distance_from_the_sun:"5.20AU", orbit_period:"11.9years", human_being_alive:"No")) + +print solar_system.planets