Giter VIP home page Giter VIP logo

programming-univbasics-3-build-a-calculator-lab-dumbo-web-100719's Introduction

Build a Simple Calculator

Learning Goals

  • Use Ruby math operations to build a calculator
  • Define instance method
  • Use the Ruby Math class to call a method provided by Ruby

Introduction

Calculators can be very useful devices in day-to-day activities. You've likely used a calculator to add up bills for this month or calculate the tip at a restaurant. We're going to take our arithmetic knowledge and put it to the test by writing functions that will do basic math calculations for us, just like we can see in IRB.

Use Ruby Math Operations to Build a Calculator

Fork and clone this repo and open lib/math.rb. You'll find a bunch of empty methods that take numbers as arguments. Build the appropriate behavior for each of the following methods:

  • addition - Build the method addition that adds num1 to num2 and returns the result of this calculation
  • subtraction - Build the method subtraction that subtracts num1 from num2 and returns the result of this calculation
  • multiplication - Build the method multiplication that multiplies num1 from num2 and returns the result of this calculation
  • division - Build the method division that divides num1 into num2 and returns the result of this calculation
  • modulo - Build the method modulo that divides num1 into num2 and gives us the remainder of this calculation
  • square_root -- Build the method square_root that finds the square root of num and returns the result

Use Methods Provided by Ruby

If a few places we've asked specific instances of data to run methods (.class or .to_s) have come up in the Programming as Conversation series. We call those instance methods. We're asking a given number, say 314 for its .class (314.class #=> Integer).

But sometimes Ruby provides standard helpful functions as class methods. A class method is like a utility method that's contained in a special namespace. Let's say you needed to do some trigonometry. Ruby has you covered! You can use Math.sin() to find the sine of an angle. Ruby also provides Math.sqrt as a class method so that you can use Ruby's understanding of squares to help out.

The square_root method this lab requires you to write should use Math.sqrt.

This is scratching the surface of "Object-Oriented Programming." Helpful functions are available to instances and classes to help do work. There's a lot to say about this, but for the time being, we can use some class methods to help do some advanced mathematics.

Conclusion

Submit the lesson once all tests are passing.

Ruby gives us many operators that can be used to perform calculations. This is the tip of the iceberg--we can do so much more than simple arithmetic, however, these operations are the most common that a developer will encounter. Grasping the basics will get you very far!

programming-univbasics-3-build-a-calculator-lab-dumbo-web-100719's People

Contributors

maxwellbenton avatar sgharms avatar yuriyberezskyy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.