Last active
August 29, 2015 14:19
-
-
Save PamBWillenz/7d83433f34efe3762be9 to your computer and use it in GitHub Desktop.
Intro To Classes 2 - Bloc Checkpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| INSTRUCTIONS: | |
| Write a Calculator class that is initialized with two arguments (x and y) and performs basic math operations on them. Your class should have the following methods: | |
| A single class method -- description -- which returns the string "Performs basic mathematical operations". | |
| An add instance method which adds the two instance variables together. | |
| A subtract instance method which subtracts the second variable from the first. | |
| A multiply instance method which multiplies them together. | |
| A divide instance method which divides the first variable by the second. | |
| SPEC | |
| describe "Calculator" do | |
| describe "description" do | |
| it "returns a description string" do | |
| Calculator.description.should == "Performs basic mathematical operations" | |
| end | |
| end | |
| describe "instance methods" do | |
| before { @calc = Calculator.new(7, 2) } | |
| describe "initialize" do | |
| it "takes two numbers" do | |
| expect( @calc.x ).to eq(7) | |
| expect( @calc.y ).to eq(2) | |
| end | |
| end | |
| describe "add" do | |
| it "adds the two numbers" do | |
| expect( @calc.add ).to eq(9) | |
| end | |
| end | |
| describe "subtract" do | |
| it "subtracts the second from the first" do | |
| expect( @calc.subtract ).to eq(5) | |
| end | |
| end | |
| describe "multiply" do | |
| it "returns the first number times the second" do | |
| expect( @calc.multiply ).to eq(14) | |
| end | |
| end | |
| describe "divide" do | |
| it "divides the numbers, returning a 'Float' if appropriate" do | |
| expect( @calc.divide ).to eq(3.5) | |
| end | |
| end | |
| end | |
| end | |
| CODE | |
| class Calculator | |
| attr_accessor :x | |
| attr_accessor :y | |
| def self.description | |
| "Performs basic mathematical operations" | |
| end | |
| def initialize(x, y) | |
| @x = x | |
| @y = y | |
| end | |
| def add | |
| @x + @y | |
| end | |
| def subtract | |
| @x - @y | |
| end | |
| def multiply | |
| @x * @y | |
| end | |
| def divide | |
| @x / @y.to_f | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CLASS METHODS | |
| SPEC | |
| describe "Car" do | |
| describe "initialize" do | |
| it "should take make, model and year" do | |
| new_car = Car.new("Acura", "Integra", 1998) | |
| expect( new_car.make ).to eq("Acura") | |
| expect( new_car.model ).to eq("Integra") | |
| expect( new_car.year ).to eq(1998) | |
| end | |
| end | |
| describe "wheels" do | |
| it "should return a standard number of wheels for any car" do | |
| expect( Car.wheels ).to eq(4) | |
| end | |
| end | |
| describe "axles" do | |
| it "should return a standard number of axles for any car" do | |
| expect( Car.axles ).to eq(2) | |
| end | |
| end | |
| end | |
| CODE | |
| class Car | |
| attr_accessor :make | |
| attr_accessor :model | |
| attr_accessor :year | |
| def initialize(make, model, year) | |
| @make, @model, @year = make, model, year | |
| end | |
| def self.wheels | |
| 4 | |
| end | |
| def self.axles | |
| 2 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| INITIALIZE | |
| SPEC | |
| describe "Car" do | |
| describe "initialize" do | |
| it "should take make, model and year" do | |
| new_car = Car.new("Acura", "Integra", 1998) | |
| expect( new_car.make ).to eq("Acura") | |
| expect( new_car.model ).to eq("Integra") | |
| expect( new_car.year ).to eq(1998) | |
| end | |
| end | |
| end | |
| CODE | |
| class Car | |
| attr_accessor :make | |
| attr_accessor :model | |
| attr_accessor :year | |
| def initialize(make, model, year) | |
| @make, @model, @year = make, model, year | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment