Programming Languages II

Information about this course and its requirements can be found at course website.

Exercise 11

Solution

Downloading via WebClient class

Use WebClient class to download file content from URL http://feeds.bbci.co.uk/news/health/rss.xml

Using XmlDocument

  • Use XmlDocument class to work with the XML document from the paragraph above
  • Write down all titles containing word VIDEO (or word chosen by you)
  • From XML represented in memory (DOM), delete all items <item>...<item>, which do not contain word VIDEO (or word chosen by you) in a title
  • At the time of deletion, write down an information, what is happening. Use events of XmlDocument class
  • Save result to XML file on your disk

Using XmlTextReader and XmlTextWriter

  • Use XmlTextReader class to work with the XML document from the paragraph above
  • Write down all titles containing word VIDEO (or word chosen by you)
  • Vytvořte na disku XML dokument, který bude obsahovat pouze takové položky <title>...<title>, které začínají slovem VIDEO
  • Create a XML document on your disk, containing only items <item>...<item>, which contain word VIDEO (or word chosen by you) in a title

WebClient: https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx
XmlDocument: https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
XmlTextReader: https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader(v=vs.110).aspx
XmlTextWriter: https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter(v=vs.110).aspx

Exercise 10

Solution

For testing purposes create a class Contact (see exercise 8). The class will contain properties name, age, email, date of birth, weight and flag whether the person is married or not. Implement also method ToString so the class will be easily written to console. Then create a console application containing array of this kind of objects with possibility to save/load to/from text and binary files (see exercise 8).

Xml Serialization

  • Save an array of contacts using XML serialization.
  • Deserialize the array and write it to console.

Binary Serialization

  • Save an array of contacts using binary serialization.
  • Deserialize the array and write it to console.

Events and Serialization

  • Define a new class ContactListener with method corresponding to EventHandler delegate. Add a new StateChanged event to the Contact class.
  • In test class attach the new method (of ContactListener class) to the new event StateChanged and try to serialize/deserialize the object again. Solve the malfunction of the binary serialization by adding NonSerialized attribute to the event.

Factory design pattern

Based on the picture with solution description below, implement previous tasks using Factory design pattern - implement interfaces IContactDAO and IContactDBFactory.

Návrhový vzor Factory

XML Serialization: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
Binary Serialization: https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx

Exercise 9

Solution

IEnumerable and IEnumerator interfaces

Implement enumerator to go through items stored in queue or stack. Both classes will implement IEnumerable interface:

  • On this exercise, implement queue by using yield return statement
  • At home, implement stack by using inner class that implements IEnumerator interface

IComparer interface

Create an array of integers and a class that represents integer comparison by following description. Try to sort the array by created comparers and write the result:

  • Numbers are ordered first by parity (first even numbers, then odd), then by value (for example 2, 4, 6, 8, 1, 3, 5, 7, 9)
  • Numbers are ordered in opposite way than in the task above (for example 9, 7, 3, 5, 1, 8, 6, 4, 2)

Collections

  • Implement generic queue using LinkedList class
  • Implement Translator class, which is able to translate text from one language to another. Use Dictionary class to implement the translation

IEnumerable: https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.110).aspx
IEnumerator: https://msdn.microsoft.com/en-us/library/78dfe2yb(v=vs.110).aspx
IComparer: https://msdn.microsoft.com/en-us/library/8ehhxeaf(v=vs.110).aspx
LinkedList: https://msdn.microsoft.com/en-us/library/he2s3bh7(v=vs.110).aspx
Dictionary: https://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Exercise 8

Solution

Events

Implement design pattern Observer with usage of events in C# language:

Design pattern Observer

Working with files

  • Create class Contact, which defines name, age, email, weight and flag whether the person is alive or not.
  • Save several contacts into a text file. Load saved contacts back and write them to console.
  • Save several contacts into a binary file. Load saved contacts back and write them to console.

Events: https://msdn.microsoft.com/en-us/library/awbftdfh.aspx
FileStream: https://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
StreamWriter: https://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx
BinaryWriter: https://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx

Exercise 7

Solution

Delegates

Create a method reference - delegate - Operation, which has two int parameters and returns int (simple mathematic operation).

Create a class Calculator containing a single method Write, which writes a result of the operation on two operands. This method will have three parameters:

  • First operand
  • Second operand
  • Operation on these two operands (of Operation type)

Write a test application, which creates instance of Calculator class and calls method Write. Create at least two different operations to call.

Events

In class Calculator add properties X and Y and method SetXY, which sets these two properties.

Add an event OnSetXY of type EventHandler, raised in method SetXY. In test application create and add listeners to the event and call method SetXY.

Implement your own class, inheriting from EventArgs, and use it when raising OnSetXY event.

In Windows Forms application, programatically add a listener to a Button's Click event and test its function.

Delegates: https://msdn.microsoft.com/en-us/library/ms173171.aspx
Events: https://msdn.microsoft.com/en-us/library/awbftdfh.aspx
Another example: https://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx

Exercise 6

Solution

Design Patterns

Implement design pattern Observer, whose description is on picture below:

Design pattern Observer

  • Class Subject will be abstract and class Invoice will inherit from it
  • IObserver is an interface, implemented by class SMSGate and PaymentMonitor
  • Use a generic collection List to save Observers

Observer: http://www.oodesign.com/observer-pattern.html.
List: https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx.

Exercise 5 - Test 1

Exercise 4

Solution

Generic types

  • Create a constructor in both collections to set desired size of the collection
  • Change their code to generic - collections will be able to store objects of various types
  • Use the collections to store integers and then to store strings

Properties and Indexers

  • Add a property Size to both collections to get and set the collection size. Attempt to shrink a collection will cause an exception.
  • Implement an indexer, with which will be able to get/set an item on an arbitrary position. In necessary cases increase size of the collection.
  • Test the functionality

Enum types

  • Create an enum MyCollectionState with values Empty, Full and PartiallyFull.
  • Add a property State to IMyCollection interface to get the collection state.
  • In a test application, rewrite method ShowCollectionState(IMyCollection) to show the collection state using switch statement.

Other features of C# language

  • Rewrite (override) the ToString method - in the console output will be distinguished occupied and empty items.
  • Add a method with out parameter to a collection to get sum of the collection array.
  • Add a method with one ref parameter A and one standard parameter B. Value in A will be increased by value B.

Generic types: https://msdn.microsoft.com/en-us/library/512aeb7t.aspx.
Indexers: https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx.
Enum types: https://msdn.microsoft.com/en-us/library/sbbt4032.aspx.
ToString: https://msdn.microsoft.com/en-us/library/system.object.tostring.
Ref parameters: https://msdn.microsoft.com/en-us/library/14akc2c7.aspx.
Out parameters: https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx.

Exercise 3

Solution

Windows Forms basics

Extend the class library (interface IMyCollection) from previous exercise by property int[] Elements. Side by side already created console application create a Windows Forms project, which will also have a reference to the class library. Application will work as follows:

Suggested application design

  • Panel for queue visualization:
    •     Label for TextBox to input a number
    •     TextBox to input a number
    •     Button to add a number into the queue. Value is obtained from TextBox
    •     Button to get a number from the queue
    •     ListBox to show actual content of the queue. There will be displayed all items!
  • Panel for stack visualization:
    •     Label for TextBox to input a number
    •     TextBox to input a number
    •     Button to push a number into the stack. Value is obtained from TextBox
    •     Button to pop a number from the stack
    •     ListBox to show actual content of the stack. There will be displayed all items!
  • Displaying only valid collection items:
    •     Change implementation of property Elements to show only valid collection items

Windows Forms Events

  • Add two Buttons and PictureBox to the application
  • When Mouse_Down event is raised on the first button, draw a rectangle into the PictureBox.
  • When Mouse_Up event is raised on the second button, draw an ellipse into the PictureBox.

Exercise 2

Solution

Create a class library that contains following interfaces:

Knihovna tříd

  • Write classes that implement these interfaces
  • The classes will be implemented using fixed-size array of integers
  • Complexity of Add, Get, Push and Pop operations will be O(1)
  • At this time, please omit cases of insert (retrieve) items into a full (from an empty) collection
  • Create a console application, add a reference to the class library and test functionality of the implemented classes
  • To show a collection state (whether the collection is empty or full), write a universal method
    void ShowCollectionState(IMyCollection collection)

Interface: https://msdn.microsoft.com/en-us/library/87d83y5b.aspx.
Circullar buffer: https://en.wikipedia.org/wiki/Circular_buffer.

Exercise 1

Solution

Information about the course

  • Curriculum
  • Conditions of passing the course
  • Your activity, evaluation

Introduction to C#

  • Create a Class Library, containing class Greeter with method string SayHello(string name)
  • Create a Console application and add a reference to the newly created library
  • Call method SayHello for each command-line argument and write the result to a Console
  • Use the same approach to the user input until the user doesn't write "exit"

Command-line Compilation

Compile the program above from your command line.

Command-line compilation is performed via C# compiler (csc.exe) (directly from Visual Studio Command Prompt or via Command Line - csc.exe can be found in folder C:\Windows\Microsoft.NET\Framework\v4.0.30319):

  • csc File.cs - compiles File.cs and creates File.exe
  • csc /target:library /out:My.dll File.cs - compiles File.cs and creates library My.dll
  • csc /out:Something.exe *.cs - compiles all .cs files in the directory and creates Something.exe

More information at https://msdn.microsoft.com/en-us/library/78f4aasd.aspx.