Menu
Coddy logo textTech

Recap - Simple Calculator

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 9 of 110.

challenge icon

Challenge

Medium

Build a complete Calculator class in calculator.dart that brings together everything you have learned. The driver.dart file will import and test your class.

Your Calculator class must demonstrate all topics covered in this module:

  1. External Files: calculator.dart will be imported by driver.dart using import 'calculator.dart';
  2. Libraries & Imports: driver.dart uses import 'dart:io';
  3. Introduction to OOP: Define class Calculator and create objects with Calculator()
  4. Classes vs Objects: Each test creates a fresh Calculator object with its own independent state
  5. The this Keyword: Use this.result inside all methods to access the instance variable
  6. Methods: Implement add, subtract, multiply, divide, clear, and getResult
  7. Instance Variables: Declare int result = 0 as an instance variable
  8. Constructor Basics: Add a Calculator() constructor that sets result to 0 and prints 'Calculator ready'

Method requirements:

  • int add(int number) — adds number to result, returns new result
  • int subtract(int number) — subtracts number from result, returns new result
  • int multiply(int number) — multiplies result by number, returns new result
  • int divide(int number) — divides result by number using integer division (~/=), returns new result. If number is 0, print exactly 'Error: Division by zero' and return the unchanged result
  • void clear() — resets result to 0
  • int getResult() — returns the current result

Example usage:

Calculator calc = Calculator(); // prints: Calculator ready
calc.add(10);                  // result = 10
calc.multiply(3);              // result = 30
calc.subtract(5);              // result = 25
calc.divide(5);                // result = 5
calc.divide(0);                // prints: Error: Division by zero
calc.clear();                  // result = 0

Try it yourself

import 'calculator.dart';
import 'dart:io';

void main() {
  String testCase = stdin.readLineSync()!;

  if (testCase == 'constructor_test') {
    Calculator calc = Calculator();
    print('Initial result: ${calc.getResult()}');
    print('Calculator created successfully');

  } else if (testCase == 'addition_test') {
    Calculator calc = Calculator();
    int r1 = calc.add(10);
    print('After adding 10: $r1');
    int r2 = calc.add(5);
    print('After adding 5: $r2');
    print('Final result: ${calc.getResult()}');

  } else if (testCase == 'subtraction_test') {
    Calculator calc = Calculator();
    calc.add(20);
    int r1 = calc.subtract(8);
    print('After subtracting 8 from 20: $r1');
    int r2 = calc.subtract(2);
    print('After subtracting 2: $r2');
    print('Final result: ${calc.getResult()}');

  } else if (testCase == 'multiplication_test') {
    Calculator calc = Calculator();
    calc.add(5);
    int r1 = calc.multiply(4);
    print('After multiplying by 4: $r1');
    int r2 = calc.multiply(2);
    print('After multiplying by 2: $r2');
    print('Final result: ${calc.getResult()}');

  } else if (testCase == 'division_test') {
    Calculator calc = Calculator();
    calc.add(100);
    int r1 = calc.divide(4);
    print('After dividing by 4: $r1');
    int r2 = calc.divide(5);
    print('After dividing by 5: $r2');
    print('Final result: ${calc.getResult()}');

  } else if (testCase == 'division_by_zero_test') {
    Calculator calc = Calculator();
    calc.add(50);
    print('Initial value: ${calc.getResult()}');
    int r = calc.divide(0);
    print('Result after division by zero: $r');
    print('Value unchanged: ${calc.getResult()}');

  } else if (testCase == 'clear_test') {
    Calculator calc = Calculator();
    calc.add(25);
    calc.multiply(3);
    print('Before clear: ${calc.getResult()}');
    calc.clear();
    print('After clear: ${calc.getResult()}');
    print('Current result: ${calc.getResult()}');

  } else if (testCase == 'comprehensive_test') {
    Calculator calc = Calculator();
    calc.add(10);
    calc.multiply(3);
    calc.subtract(5);
    calc.divide(5);
    print('After sequence of operations: ${calc.getResult()}');
    calc.divide(0);
    print('After division by zero attempt: ${calc.getResult()}');
    calc.clear();
    calc.add(100);
    print('After clear and add 100: ${calc.getResult()}');

  } else {
    print('Unknown test case');
  }
}

All lessons in Object Oriented Programming