/A-Class-for-Maths

Worksheet 7 OOP Create a class that takes a number in its constructor, and allows us to add, subtract, multiply and divide using that number

Primary LanguagePython

July 2022 Computing programming course by Teesside University

Worksheet 7-Object-Oriented Programming

[ASSESSMENT] Question 2: A Class for Maths (class_maths.py) Let’s now take things up a level by returning some values from our class functions. Let’s create a class that takes a number in its constructor, and allows us to add, subtract, multiply and divide using that number.

  1. Create a new class called MathEngine, with a constructor that takes 1 argument num and stores it at the class level as an attribute (just like we did with name in question 1).
  2. Now, add 4 functions to your class: a. The add() function should take a number as an argument, add num and return the result. b. The sub() function should take a number as an argument, subtract num and return the result. c. The mult() function should take a number as an argument, multiply it by num and return the result. d. The div() function should take a number as an argument, divide it by num and return the result.
  3. Test your class by calling each of its functions and inspecting what they return.
  4. Now, create a simple command-line based user interface for your program. Allow the user to specify a base number and construct an instance of your class. Then allow them to add, multiply, subtract and divide using that number until they type exit. You are allowed to implement this however you like.

Lecturer's comment: Good. Consider how to adapt the class to use varying secondary input