26.03.2024 02.04.2024 1. Properties: setters and getters https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_4.html/ OOP_inherit.txt ... 2.2 Extend class Animal for properties: a) age: not negative integer number; minimal/default value = 0 , b) weight: positive float Proc properties? - encapsulation (nesahej mi na me ... get of my cloud ... rolling stones); security view: control check - odvozeni veliciny (dry code) napr. vime velikost pole _n a z ni odvodime isEmpty, a nemusim si pamatoval dve hodnoty, coz zvysuje komplexitu a muze zpusobit inkonsistenci - idea: "filtr pred vlozenim" 2. Pretezovani operatoru a indexery https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_4.html/ DOPIMPLEMETOVAT 2.1 Polynomials https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/exercises_4.html Design and implement a C# class Polynomial representing a polynomial of a single variable. Your class should include the following: - a constructor that takes a variable number of arguments of type double, yielding a polynomial with those coefficients. For example: new Polynomial(1.0, 4.0, 5.0) should yield the polynomial x2 + 4x + 5. - a property degree that returns the degree of a polynomial. For example, the polynomial above has degree 2. - an overloaded operator + that adds two polynomials - an overloaded operator - that subtracts two polynomials - an overloaded operator * that multiplies two polynomials, or a polynomial and a scalar - a method that evaluates a polynomial for a given value of x - a method that returns the first derivative of a polynomial - a method asString() that yields a string such as "x^2 + 4x + 5" - a static method Parse() that parses a string such as "x^2 + 4x + 5", yielding a polynomial. - a indexer to change polynom - method Equals() https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_6.html/ public override bool Equals(object? o) => o is Point p && x == p.x && y == p.y; The C# standard library contains a generic interface IComparable that is implemented by all ordered built-in types. IComparable means "comparable with objects of type T". For example, the built-in type int implements IComparable, since integers are comparable with integers. IComparable is defined like this: interface IComparable { int CompareTo (T other); } The CompareTo() method returns - a negative number if this object is less than other - 0 if this object equals other - a positive number if this object is greater than other - rozsirte tridu Polynomial o komparator porovnavajici na zaklade stupne polynomu 3. File / std IO https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_3.html/ - StreamReader, StreamWriter from System.IO namespace - reading all lines: while (Console.ReadLine() is string line) { ... do something with line ... } - closing files with 'using': using (StreamWriter sr = new StreamWriter(filename)) { ... use sr here ... } 3.3 Příklad wc: napište obdobu Unix utility wc počítající počet: a) řádků "-l", b) slov "-w", c) písmen "-c", (nedelat: d) není li zadán parametr výpisu, jen jmeno souboru, vytiskne vše) v souboru dle zadaného parametru, obé (parametr, co počítáme i název souboru) zadané z příkazové řádky https://cs.wikipedia.org/wiki/Wc_(Unix) https://www.geeksforgeeks.org/wc-command-linux-examples/ -l : vytiskne počet řádek -c : vytiskne počet bytů -m : vytiskne počet znaků -L : vytiskne délku nejdelší řádky -w : vytiskne počet slov Příklad: wc -l "to_nechces_cist.txt" 4. Generika https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_6.html/ 4.2 Implementujte generický Stack - push, pop, isEmpty, size - konstruktor pro zadanou maximalni velikost - vyuzit property? 5. Interface https://ksvi.mff.cuni.cz/~dingle/2023-4/prog_2/notes_5.html/ Navrhnete a implementujte interface IStack pro Stack a vytvorte tridu ArrayStack implementujici interface IStack 5.1 Generic interfaces An interface may also be generic. For example, here's a generic interface IStack, which is a stack of elements of type T: interface IStack { bool isEmpty { get; } void push(T i); T pop(); } 6. The C# standard library contains a generic interface IComparable that is implemented by all ordered built-in types. IComparable means "comparable with objects of type T". For example, the built-in type int implements IComparable, since integers are comparable with integers. IComparable is defined like this: interface IComparable { int CompareTo (T other); } The CompareTo() method returns - a negative number if this object is less than other - 0 if this object equals other - a positive number if this object is greater than other Rozsirte tridu Animals o komparator porovnavajici na zaklade veku zvirete 8. C# collection classes