Exercises

  • Exercise 1 :
  • Create a class called Point allowing to manipulate a point of a plane (x,y). You will provide:

    • A constructor receiving as arguments the coordinates (double) of a point.

    • A member function "move" performing a translation defined by its two arguments (double).

    • a member function "display" simply displaying the coordinates of the point.

    The coordinates of the point will be private data members.
    We will write separately:

    • A source file constituting the declaration of the class.

    • A small test program (main) declaring a point, displaying it, moving it around and showing it again.



    Solution:

    point.dart

    class Point {

      double _x, _y;

      Point(this._x, this._y);

      void move(double mx, double my) {

        _x += mx;

        _y += my;

      }

     

      void display() {

        print("x : $_x  y : $_y");

      }

    }



    main.dart

    import 'point.dart';

     

    void main() {

      var pt = Point(26.5);

      pt.display();

      pt.move(-23.25);

      pt.display();

    }



    x : 2.0 y : 6.5
    x : 0.0 y : 9.75


  • Exercise 2 :
  • Adapt the class from the previous exercise, so that the member function "display" provides, in addition to the point coordinates, the number of objects of type Point.



    Solution:

    point.dart

    class Point {

      double _x, _y;

      static int nbOfObjets = 0;

      Point(this._x, this._y) {

        nbOfObjets++;

      }

      void move(double mx, double my) {

        _x += mx;

        _y += my;

      }

     

      void display() {

        print("x : $_x  y : $_y");

        print("---We have $nbOfObjets point(s)");

      }

    }



    main.dart

    import 'point.dart';

     

    void main() {

      var pt1 = Point(26.5);

      pt1.display();

      var pt2 = Point(5.35, -5);

      pt2.display();

      var pt3 = Point(01);

      pt3.display();

      var pt4 = Point(1911);

      pt4.display();

    }



    x : 2.0 y : 6.5
    ---We have 1 point(s)
    x : 5.35 y : -5.0
    ---We have 2 point(s)
    x : 0.0 y : 1.0
    ---We have 3 point(s)
    x : 19.0 y : 11.0
    ---We have 4 point(s)

  • Exercise 3 :
  • The class "vector3D" is defined as follows:

    class Vector3D {

      double _x, _y, _z;

      Vector3D(this._x, this._y, this._z);

    }



    1) Add new named constructor "same" that have only one argument and initialize the three variables with it.

    2) Add a member function "coincide" that check if two vectors have the same components.

    3) A small test program (main)



    Solution:

    vector3D.dart

    class Vector3D {

      double _x, _y, _z;

      Vector3D(this._x, this._y, this._z);

      Vector3D.same(double w) {

        _x = w;

        _y = w;

        _z = w;

      }

      bool coincide(Vector3D v) {

        if (v._x == _x && v._y == _y && v._z == _z)

          return true;

        else

          return false;

      }

    }



    main.dart

    import 'vector3D.dart';

     

    void main() {

      var v1 = Vector3D(123);

      var v2 = Vector3D.same(2);

      var v3 = Vector3D(222);

      print(v2.coincide(v1));

      print(v2.coincide(v3));

    }



    false
    true

  • Exercise 4 :
  • 1) Create a class called BankClient that have 3 private attributes :

       a) Id which is generated automatically and represents the order of the client in the bank Data Base (starting from 1 and can't be changed).

       b) Name (can't be changed).

       c) Balance have 3 methods (get, add, subtractIfPossible).

    2)Create a constructor that use "Initializer list" to initialize the name.

    3)Create a static method that print the number of clients and the bank balance.

    4)Create a small test program (main) .

    Note : The balance is initialized at 0 and can't be negative.



    Solution:

    bankClient.dart

    class BankClient {

      int _id;

      double _balance = 0;

      String _name;

      static int numberOfClients = 0;

      static double bankBalance = 0;

      BankClient(String n) : _name = n {

        numberOfClients++;

        _id = numberOfClients;

      }

      String getName() {

        return _name;

      }

     

      double getBalance() {

        return _balance;

      }

     

      void addToBalance(double amount) {

        _balance += amount;

        bankBalance += amount;

      }

     

      void subtractIfPossible(double amount) {

        if (amount > _balance) {

          print("Operation failed !\nThe amount is greater than the balance.");

        } else {

          _balance -= amount;

          bankBalance -= amount;

        }

      }

     

      static void printBankData() {

        print(

            "The number of Clients is $numberOfClients\nThe bank balance is $bankBalance");

      }

    }



    main.dart

    import 'bankClient.dart';

     

    void main() {

      var c1 = BankClient("Mauro Winchenbach"),

          c2 = BankClient("Janetta Sullens"),

          c3 = BankClient("Karma Harari"),

          c4 = BankClient("Alfonzo Cashin");

      print(c1.getName());

      c1.addToBalance(341.5);

      c2.addToBalance(500);

      c3.addToBalance(233);

      print(c3.getBalance());

      print(c4.getBalance());

      BankClient.printBankData();

      c2.subtractIfPossible(100);

      print(c2.getBalance());

      c3.subtractIfPossible(300);

      print(c3.getBalance());

      BankClient.printBankData();

    }



    Mauro Winchenbach
    233.0
    0.0
    The number of Clients is 4
    The bank balance is 1074.5
    400.0
    Operation failed !
    The amount is greater than the balance.
    233.0
    The number of Clients is 4
    The bank balance is 974.5

  • Exercise 5 :
  • The class "Point" is defined as follows:

    class Point {

      int _x, _y;

      Point(this._x, this._y);

      void move(int mx, int my) {

        _x += mx;

        _y += my;

      }

     

      void display() {

        print("x = $_x   y = $_y");

      }

    }



    1) Define a new class "PointCol" to manipulate colored points. This class is a derived from Point

    2) Its constructor initializes the color of the object with a given integer (the color reference).

    3) Add a method "printColor" that print the color.

    4) Create a small test program (main).





    Solution:

    pointCol.dart

     

    class PointCol extends Point {

      int _color;

      PointCol(int x, int y, int c) : super(x, y) {

        _color = c;

      }

      void printColor() {

        print("The Color Reference is $_color");

      }

    }



    main.dart

    import 'pointCol.dart';

     

    void main() {

      var p1 = PointCol(50123);

      p1.display();

      p1.move(-11);

      p1.display();

      p1.printColor();

    }



    x = 5 y = 0
    x = 4 y = 1
    The Color Reference is 123

  • Exercise 6 :
  • 1) Create a class "Client" that have 2 private attributes :

       a) Name (can't be changed).

       b) PurchasesAmount (double) have 2 methods (get, add).

    2)Create a constructor that initialize the name.

    3) Create a class "LoyalClient" that have 1 private attribute :

       a) PurchasesAmount (double) have 1 methods (get).

    4) Create a method "discount" that assign to PurchasesAmount (subclass) the value of PurchasesAmount (superclass) after reduction of 10%.

    5)Create a small test program (main) .

    Note : Use different names for the get methods.





    Solution:

    client.dart


    class Client {

      String _name;

      double _purchasesAmount = 0;

      Client(this._name);

      String getName() {

        return _name;

      }

     

      double getPurchasesAmount() {

        return _purchasesAmount;

      }

     

      void addToPurchasesAmount(double amount) {

        _purchasesAmount += amount;

      }

    }


    LoyalClient.dart


    import 'client.dart';

     

    class LoyalClient extends Client {

      double _purchasesAmount = 0;

      LoyalClient(String name) : super(name);

      double getPurchasesAmountOfLoyalClient() {

        return _purchasesAmount;

      }

     

      void discount() {

        _purchasesAmount = super.getPurchasesAmount() * 0.9;

      }

    }


    main.dart


    import 'client.dart';

    import 'loyalClient.dart';

     

    void main() {

      var c = Client("Daron Petsche");

      print(c.getName());

      c.addToPurchasesAmount(15);

      c.addToPurchasesAmount(50);

      c.addToPurchasesAmount(7);

      print(c.getPurchasesAmount());

     

      var lc = LoyalClient("Lucie Orloff");

      print(lc.getName());

      lc.addToPurchasesAmount(43);

      lc.addToPurchasesAmount(32);

      lc.addToPurchasesAmount(70);

      print(lc.getPurchasesAmount());

      lc.discount();

      print(lc.getPurchasesAmountOfLoyalClient());

    }




    Daron Petsche
    72.0
    Lucie Orloff
    145.0
    130.5