Skip to content
Open
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
146 changes: 97 additions & 49 deletions assignments/assignment08.rb
Original file line number Diff line number Diff line change
@@ -1,72 +1,120 @@
# ========================================================================================
# Assignment 8
# Assignment 7
# ========================================================================================

# ========================================================================================
# Problem 1 - Roman Numerals
# Problem 1 - Observable

# implement conversion between integers and roman numerals
# implement Observable
# validate using MiniTest unit tests

module Assignment08
class RomanNumeral
def initialize(i)
# your implementation here
module Assignment07
module Observable
def add_observer(obj)
if defined? @observers
@observers << obj
else
@observers = [obj]
end
end

def to_s
# your implementation here
def delete_observer(obj)
if defined? @observers
@observers.delete obj
end
end

def to_i
# your implementation here
def delete_observers
if defined? @observers
@observers = []
end
end
def changed(new_state=true)
@state = new_state
end

# bonus: create from Roman Numeral
def self.from_string
# your implementation here
# returns a new instance
def changed?
if @state.nil?
@state = false
else
@state
end

# if changed state is true, invoke the update method in each associated observer. then changed state becomes false.
def notify_observers(*args) #splat operator - multipel comma-separated params at end
if @state == true
@observers.each do |observer|
observer.send ("update", args)
end
end
@state = false
end
end
end
end

# expected results:
RomanNumeral.new(1).to_s # => 'I'
RomanNumeral.new(2).to_s # => 'II'
RomanNumeral.new(3).to_s # => 'III'
RomanNumeral.new(4).to_s # => 'IV'
RomanNumeral.new(5).to_s # => 'V'
RomanNumeral.new(6).to_s # => 'VI'
RomanNumeral.new(9).to_s # => 'IX'
RomanNumeral.new(10).to_s # => 'X'
RomanNumeral.new(19).to_s # => 'XIX'
RomanNumeral.new(32).to_s # => 'XXXII'
RomanNumeral.new(51).to_s # => 'LI'

# bonus:
RomanNumeral.from_string('III').to_i # => 3
RomanNumeral.from_string('IX').to_i # => 9
RomanNumeral.from_string('IX').to_i # => 9
require 'minitest/autorun'

# given any arbitrary integer n:
n == RomanNumeral.from_string(RomanNumeral.new(n).to_s).to_i
class TestObservable < MiniTest::Test

class Thermometer
include Assignment07::Observable
def initialize(temp)
@temp = temp
end

def up
@temp += 1
notify_observers @temp
end

def down
@temp -= 1
notify_observers @temp
end
end

# ========================================================================================
# Problem 2: Golden Ratio
class ThermometerControl
def initialize(thermometer)
@thermometer = thermometer
end

# Golden Ratio is ratio between consecutive Fibonacci numbers
# calculate the golden ratio up to specified precision
# validate using MiniTest unit tests
def click_up
@thermometer.up
end

module Assignment08
def golden_ratio(precision)
# your implementation here
def click_down
@thermometer.docn
end
end

class ThermometerDisplay
def initialize(thermometer)
thermometer.add_observer self
end

def render(temp)
puts temp
end

def update(temp)
render temp
end
end
end

# expected results:
golden_ratio(2) # => 1.62
golden_ratio(5) # => 1.61803
golden_ratio(8) # => 1.61803399
def test_observable
t = Thermometer.new 68
tc = ThermometerControl.new t
td = ThermometerDisplay.new t
tc.click_up
assert_equal 69, td.render
tc.click_down
assert_equal 68, td.render
t2 = Thermometer.new 5
tc2 = ThermometerControl.new t2
td2 = ThermometerDisplay.new t2
tc2.click_up
assert_equal 6, td2.render
tc2.clicp_down
assert_equal 5, td2.render
end

end