CSharpExercises 20170426
CSharpExercises 20170426
C# Programming
Exercises
(used in conjunction with Object-Oriented Programming With C#)
 By Per Laursen
 26-04-2017
Content
ProFun.0 ...................................................................................................................... 4
ProFun.1 ...................................................................................................................... 5
ProFun.2 ...................................................................................................................... 6
ProFun.3 ...................................................................................................................... 7
OOPFun.1..................................................................................................................... 8
OOPFun.2..................................................................................................................... 9
OOPFun.3................................................................................................................... 10
OOPFun.4................................................................................................................... 11
OOPFun.5................................................................................................................... 12
OOPFun.6................................................................................................................... 13
ProNex.1 .................................................................................................................... 14
ProNex.2 .................................................................................................................... 15
ProNex.3 .................................................................................................................... 16
ProNex.4 .................................................................................................................... 17
ProNex.5 .................................................................................................................... 18
ProNex.6 .................................................................................................................... 19
ProNex.7 .................................................................................................................... 20
ProNex.8 .................................................................................................................... 21
ProNex.8a .................................................................................................................. 22
ProNex.9 .................................................................................................................... 23
ProNex.10 .................................................................................................................. 24
ProNex.11 .................................................................................................................. 25
ProNex.12 .................................................................................................................. 26
DRY.1 ......................................................................................................................... 27
DRY.2 ......................................................................................................................... 28
OOPNex.1 .................................................................................................................. 29
OOPNex.2 .................................................................................................................. 30
OOPNex.3 .................................................................................................................. 31
OOPNex.4 .................................................................................................................. 32
OOPNex.5 .................................................................................................................. 33
OOPNex.6 .................................................................................................................. 34
DBI.0 .......................................................................................................................... 35
DBI.1 .......................................................................................................................... 36
DBI.2 .......................................................................................................................... 37
DBI.3 .......................................................................................................................... 38
DBI.4 .......................................................................................................................... 39
DBI.5 .......................................................................................................................... 40
DBI.6 .......................................................................................................................... 41
DBI.7 .......................................................................................................................... 42
DBI.8 .......................................................................................................................... 43
MVVM.0..................................................................................................................... 44
MVVM.1..................................................................................................................... 45
MVVM.2..................................................................................................................... 46
MVVM.3..................................................................................................................... 47
Files.1......................................................................................................................... 48
Excep.1 ...................................................................................................................... 49
MVVMStarter.1.......................................................................................................... 50
How to use this exercise set
This set of exercises is intended to be used in conjunction with the note Object-
Oriented Programming with C#. However, they are as such self-contained.
    Exercise: Identifier for the exercise. The first part of the identifier is an
     acronymed reference to the corresponding chapter in the notes.
    Project: A C# project used in the exercise. The specific details of how the
     project is made available (.zip file, GitHub repository, etc.) may vary from
     course to course. The projects are self-contained.
 Purpose: What aspect of the learning process does this exercise concern.
    Description: The “setup” for the exercise, typically some sort of simplified
     domain-specific context.
    Steps: Specific steps in the exercise. The steps often become increasingly
     difficult. Some steps are marked in red. These steps are considered quite
     difficult.
To get around some technicalities with C# projects that are irrelevant for the begin-
ner, all projects contain an extra C# file called InsertCodeHere.cs. In that file, an area
is delimited by two comments
(sandbox area)
This area is referred to as the “sandbox area” in several exercises. If you are required
to put some code in the “sandbox area”, this is the place.
Exercise      ProFun.0
Project Sandbox
Description   The Sandbox project is as simple as it gets – we will just use it to verify
              that your installation of Visual Studio is up and running
Project MovieManagerV05
Description   We imagine this project to be the very first steps in creating an appli-
              cation for movie management. The application could be used to keep
              track of relevant information for movies, e.g. a private collection of
              movies on DVD/Blu-ray (yes, some people still watch movies on
              physical media ).
Project WebShopV05
Description   Part of the business logic in a web shop involves calculating the total
              cost of an order. The logic for calculating the total cost is as follows:
                 1. An item has a net price
                 2. You pay a 10 % tax on top of the net price
                 3. Shipping costs 49 kr., no matter the number of items
                 4. There is a credit card fee of 2 % on top of the entire cost,
                    including tax and shipping.
Steps            1. Load and open the project – you will see that some variables for
                    the net prices and number of items in order have already been
                    included. Also, the order details are printed on the screen.
                 2. The variable totalPrice is supposed to contain the total price for
                    the order. You must add the calculations needed to do this,
                    given the logic in the description.
                 3. Test your solution by varying the number of books, DVDs and
                    games in the order (you do this by assigning new values to the
                    noOf… variables)
                 4. The web shop decides to offer a discount, based on the total
                    number of items you buy:
                     If you buy at least 15 items, you get a 5 % discount
                     If you buy at least 30 items, you get a 10 % discount
                    The discount is applied to the total price – update the code to
                    include this discount.
Extra info Some test examples you can use to verify your solution:
Project WebShopV06
Description   Another part of the business logic in a web shop involves deciding if a
              customer qualifies for certain special offers, based on the order. The
              shop has four special offers. The logic for qualifying for each offer is:
                 1. The net total price (no taxes, etc.) is more than 1.000 kr.
                 2. You have ordered more books than games
                 3. You have ordered at least 10 items of one kind
                 4. You have ordered between 10 and 20 DVDs, or at least 5 games
Steps            1. Load and open the project – again, some variables are already
                    present. Note the boolean variables receiveSpecialOffer…
                 2. For each of these variables, you must specify a logical expres-
                    sion, corresponding to the logic given in the description.
                 3. Test your solution by varying the number of books, DVDs and
                    games in your order.
                 4. The web shop decides to offer an extra special offer. You qualify
                    for the extra offer, if you qualify for exactly two of the previous
                    offers. Update your code to include this extra offer.
Extra info    Some test examples you can use to verify your solution (SO#1 means
              “special offer 1”, and so on):
Project MovieManagerV10
Description   In this version of the movie manager, a class called Movie has been
              added (in the file Movie.cs). It contains an absolute minimum of
              information about a specific movie. The class is put to use in the
              sandbox area, where some Movie objects are created and used.
Steps            1. Load the project, and go directly to the sandbox area. You will
                    see that some code is already present. See if you can figure out
                    what goes on in each line of code. If you hover the mouse
                    cursor over a specific element, you should see some useful
                    information pop up. Make sure you understand where
                        Objects are created
                        Parameters to the constructors are specified
                        Properties are used
                        Methods are called
                        Return values are used
                 2. Feel free to create additional Movie objects, and exercise them
                    a bit (call methods, use properties, etc.)
Exercise      OOPFun.2
Project BankV05
Description   The project contains a minimal BankAccount class. The class is put to
              use in the sandbox area, where a BankAccount objects is created and
              used.
Steps            1. Load the project, and take a look at the BankAccount class.
                    Make sure you understand the elements it contains. Then take
                    a look at how the class is used in the sandbox area.
                 2. We now want to add an extra property to the BankAccount
                    class: the name of the account holder. Add this feature to the
                    class. This will probably involve:
                        a. Adding an instance field
                        b. Adding a property
                        c. Updating the constructor
                 3. Once the class has been updated, make sure to test the new
                    feature by updating the code in the sandbox area.
Exercise      OOPFun.3
Project RolePlayV10
Steps            1. Start out by taking a look at the Warrior class. Make sure you
                    understand the elements it contains. Then take a look at how
                    the class is used in the sandbox area.
                 2. We must now extend the class with a “level” feature. Details of
                    this feature are:
                        a. All warriors start at level 1.
                        b. The level can be retrieved freely, but not changed freely.
                        c. It must be possible to increase the level by one.
                 3. Implement this feature in the Warrior class. You will need to
                    consider if
                        a. An extra instance field is needed
                        b. An additional property is needed (if so, do we need both
                            the get and the set?)
                        c. The constructor needs to be updated.
                        d. A method for increasing the level is needed.
                 4. We must now extend the class with a “hit points” feature.
                    Details of this feature are:
                        a. Hit points are set individually when a warrior is created
                        b. Hit points can be retrieved freely, but not changed freely.
                        c. It must be possible to decrease hit points by a specified
                            amount.
                 5. Implement this feature in the Warrior class, going through the
                    same considerations as for the level feature
                 6. Implement a property called Dead, which returns a boolean
                    value. The property should return true if hit points are below
                    zero.
Exercise      OOPFun.4
Project ClockV10
Description   This project contains an empty class definition Clock. Your job is to
              implement the class, given the below requirements:
                 1. The clock should keep track of hours and minutes.
                 2. The clock should use the 24-hour system.
                 3. It must be possible to set the clock to a specific time.
                 4. It must be possible to retrieve the current time from the clock.
                 5. It must be possible to advance the clock by a single minute.
Steps            1. Implement requirements 1-4. This will involve figuring out what
                    instance fields, constructor, properties and methods you need
                    for this. Remember to include code for testing the class.
                 2. Implement requirement 5. In this case, it becomes quite
                    important to choose relevant test cases.
Exercise      OOPFun.5
Project DiceGame
Description   This project contains two classes: Die and DiceCup. The Die class
              represents a 6-sided die, and is completed. The DiceCup class needs a
              bit of work to be complete. The DiceCup class uses the Die class.
Steps            1. Take a look at the Die class. It is complete, and fairly simple.
                    Note that we use another class in the Die class, called Random.
                    This class is from the .NET class library.
                 2. Open the DiceCup class. Note how the class contains two
                    instance fields of type Die. Also note the constructor – what
                    happens there?
                 3. The DiceCup class is not complete. Implement the Shake
                    method and the TotalValue property, as specified in the
                    comments in the code. Test that your code works as expected,
                    by creating and using a DiceCup object in the sandbox area.
                 4. How much would we need to change in order to have a dice
                    cup with three dice?
                 5. When we create a DiceCup object, we would also like to be able
                    to specify the number of sides the dice should have. Implement
                    the necessary changes in Die and DiceCup needed to enable
                    this feature.
Exercise      OOPFun.6
Project StaticExamples
Purpose Defining and using static classes, methods and instance variables.
Description   The project contains the class ListMethods, which defines two
              methods FindSmallestNumber and FindAverage. The names should
              hopefully indicate what they do. Code that tests the class is included
              in the sandbox area. The class is tested in the traditional way; create
              an object, and call methods on the object.
Project BankV10
Description   This project contains a minimal BankAccount class, that sort of works.
              However, it has some problems…
Project WTF
Steps            1. By reading the code for ThreeNumbers, try to figure out what it
                    does. Write some test code to see if you are right.
                 2. Write and test a new method TwoNumbers, that does the same
                    thing as ThreeNumbers, but now only for two numbers.
                 3. Write and test a new method FourNumbers, that does the
                    same thing as ThreeNumbers, but now for four numbers (tip –
                    you can probably use the method TwoNumbers to make the
                    code fairly short and easy).
                 4. Rewrite ThreeNumbers to use the TwoNumbers method. What
                    code do you like best – the original code or the new code?
Exercise      ProNex.3
Project WeatherStationV10
               Pressure                 WeatherDescription
               Below 980                Stormy
               980-1000                 Rainy
               1000-1020                Changing
               1020-1040                Fair
               Above 1040               Very dry
Project WhileLoopsBaseCamp
Steps            1. In the sandbox area, four while-loops (Case 1-4) are given. Try
                    to figure out what the output from each loop will be. When
                    ready, uncomment the line in each loop that prints the current
                    value of the counter variable, and see if you were right.
                 2. Next follows Case 5-8. Here you must implement a while-loop
                    yourself, to produce the number sequence given in the
                    comment for each case.
Exercise      ProNex.5
Project CorrectChangeAutomat
Description   This exercise is about calculating the correct change when a customer
              pays a due amount with too much cash (yes, some people still pay
              with cash…).
              Example: A customer has to pay 266 kr., but pays 500 kr.. The custo-
              mer must then receive 234 kr. in change. The tricky part is to figure
              out how to pay this amount using ordinary bills and coins, and paying
              back as few bills and coins as possible. In this example, the correct
              way to pay back correct change would be:
                  One 200-kr bill
                  One 20-kr coin
                  One 10-kr coin
                  Two 2-kr coins
Steps            1. Implement code to calculate and print out the correct change.
                    To keeps things simple, we assume that you only use 100-kr
                    bills, 10-kr coins and 1-kr coins. Remember to test your code
                    with some different values for change. You can just add the
                    code in the sandbox area.
                 2. Once the above problem is solved, include some more bills and
                    coins, like 50-kr bills, 5-kr coins, etc..
                 3. If you used while-loops for solving the problem: Try to solve the
                    problem without using loops.
Exercise      ProNex.6
Project RolePlayV20
Description   The project is supposed to model a very simple game, where a hero
              can battle against a beast, until either beast or hero is dead! The
              project contains four classes, which are described in general terms
              here – see the code for more details:
                  The NumberGenerator class, with the method Next. This is a
                    helper class for generating random numbers.
                  The BattleLog class, where individual strings can be “saved”,
                    and later on printed out on the screen.
                  The Hero class, which models a game character. It is a very
                    simple model, since it just has a number of hit points.
                  The Beast class, which also models a game character, in a way
                    similar to the Hero class.
              Even though this is a very simple setup, it does include fundamental
              game mechanics from many popular role-playing games.
Steps            1. Study the classes in details, so you are sure of what they can do
                    and how they work. Note how the Hero and Beast classes make
                    use of the NumberGenerator and BattleLog classes.
                 2. See if you can figure out how to code a battle between a Hero
                    and a Beast (until the death!). A bit of code is present in the
                    sandbox area, but it obviously needs to be extended.
                 3. When you can make the two objects battle each other, there
                    are a number of things to consider afterwards:
                       a. It seems like the Hero wins most of the time (depending of
                            course on how you coded the battle…). Why is that? How
                            could we make the battle more fair?
                       b.The damage dealt by the Hero is always between 10 to 30
                            points. How could we change that? Could we even let the
                            creator of the Hero object decide this interval? Could this
                            also be done for the number of initial hit points?
                       c. Do we really need separate classes for Hero and Beast?
Exercise      ProNex.7
Project DrawShapes
Description   This exercise is about trying to draw some simple shapes on the
              screen, using for-loops to get the job done. A very simple class
              DrawingTool is provided to help with this.
Steps            1. Study the class DrawingTool. As you can see, it is very simple.
                    Why is the class (and the methods) static?
                 2. Using for-loops and the DrawingTool class, see if you can create
                    code to draw the shapes A to E, as defined in the comments in
                    the sandbox area. NOTE: The shapes get increasingly hard to
                    draw…
Exercise      ProNex.8
Project ListBaseCamp
Description   This exercise is about predicting the result of applying some methods
              of the List class to a List object, and also about writing some code to
              use a List object
Steps            1. In the sandbox area, a List object is created, and some elements
                    are added and removed. At four points in the code (Case 1-4),
                    you must predict the outcome of the WriteLine statement.
                    When ready, you can uncomment the WriteLine statement,
                    and see if your prediction was correct.
                 2. Following the cases above, four more cases are given (Case 5-
                    8), where you must write code that use the List object, to
                    retrieve various information about the elements in the list.
                    Details for each case are given as comments in the code.
Exercise      ProNex.8a
Project RolePlayV21
Description   This exercise picks up where ProNex.6 let off. Now the Hero must face
              a greater challenge! (or maybe he’s just farming..).
Project LibraryV10
              The project contains the simple domain class Book (we consider the
              isbn number to be a “key” for Book, i.e. no two Book objects can have
              the same isbn number). Also, it contains the (incomplete) catalog class
              BookCatalog. The three public methods in BookCatalog allow the user
              to store and use Book objects in a simple way (see the comments in
              the code for more details about each method).
Steps            1. Study the test written in the sandbox area, and figure out what
                    you expect the test to output.
                 2. Complete the three methods in the BookCatalog class.
                 3. Run the application, and see if the output of the test matches
                    your expectations (if not, you will have to examine the test and
                    your code once again…).
                 4. Is there anything in the code that prevents a user from adding
                    two Book objects with the same isbn value?
                 5. How could you prevent that Book objects with the same isbn
                    value are added to the catalog?
Exercise      ProNex.10
Project LibraryV11
              The project contains the simple domain class Book (we consider the
              isbn number to be a “key” for Book, i.e. no two Book objects can have
              the same isbn number). Also, it contains the (incomplete) catalog class
              BookCatalog. The three public methods in BookCatalog allow the user
              to store and use Book objects in a simple way (see the comments in
              the code for more details about each method).
Steps            1. Study the test written in the sandbox area, and figure out what
                    you expect the test to output.
                 2. Complete the three methods in the BookCatalog class.
                 3. Run the application, and see if the output of the test matches
                    your expectations (if not, you will have to examine the test and
                    your code once again…).
                 4. Is there anything in the code that prevents a user from adding
                    two Book objects with the same isbn value?
                 5. How could you prevent that Book objects with the same isbn
                    value are added to the catalog?
Exercise      ProNex.11
Project SchoolAdministrationV10
Purpose       Use the Dictionary class. Work with an application containing several
              classes.
Description   The project contains the class Student. This is a simple representation
              of a student, with three instance fields; id, name and test scores. The
              first two are simple, but the “test scores” field is a Dictionary, holding
              key-value pairs of course names (string) and scores (int).
              The project also contains the class StudentCatalog. This class is sup-
              posed to be able to retrieve various information about the students;
              for this purpose, an instance field _students of type Dictionary is used
              to hold key-value pairs consisting of ids and Student objects (since a
              student is uniquely identified by an id)
Steps            1. The class Student is complete, and you need not change
                    anything in it. However, take a good look at the Student class
                    anyway, and make sure you understand how the methods
                    work. Pay particular attention to the property ScoreAverage.
                 2. Look in the class definition of StudentCatalog. It contains five
                    properties/methods (Count, AddStudent, GetStudent,
                    GetAverageForStudent, GetTotalAverage) that are not
                    completed. Add code to complete these methods, according to
                    the specification given in the comments in the code.
                 3. Code that tests the StudentCatalog class has been added in the
                    sandbox area,. Run the application, and check that the Student-
                    Catalog class behaves as expected.
Exercise      ProNex.12
Project Flinter
Description   Flinter is supposed to be the start of a new dating app. You can create
              profiles for those you are interested in meeting.
              In the project, the class Profile has been included. The class contains
              instance fields for gender, eye color, hair color, and height. You can
              thus create a Profile object by specifying values for each of these four
              fields in the class constructor. Furthermore, you can get a text
              description of a Profile object by using the property GetDescription.
Steps            1. Code that tests the Profile class is as always included in sandbox
                    area. Examine the code in the Profile class definition, and see if
                    you can predict the outcome of running the test.
                 2. Running the test reveals some problems. In two cases, we have
                    specified hair color where we should have specified eye color,
                    and vice versa (unless you really want a partner with white eyes
                    and blue hair…), and in one case, we have specified a height
                    category that doesn’t exist. Change the Profile class definition
                    by adding enumerated types for gender, eye color, hair color
                    and height category. Use these new types for the four instance
                    variables. The constructor needs some changes as well. Also
                    consider if you still need the properties GenderDescription and
                    HeightDescription.
                 3. Change the code in the sandbox area , so it is compatible with
                    the redesigned Profile class. Observe how it is now only
                    possible to specify legal values for each type.
                 4. Reflect a bit on the changes. Is there anything in the new code
                    that is more complicated than it was in the original code? Was
                    it always relevant to use an enumerated type?
Exercise      DRY.1
Project CalculationSimulation
Steps            1. The code is set up to do calculations in a 5x5 table (that is, x and
                    y can be numbers between 0 and 4, both included). How many
                    places in the project would you have to change something, if
                    you want to do calculations in a 10x10 table instead?
                 2. Change the code, such that you get rid of all the instances of
                    the number 5 in the methods. This could be done by using
                    constants, instance fields and parameters.
                 3. It seems like -1 means “no value”. Change the code, such that
                    the value -1 does not occur in the methods.
                 4. Are there other values that are candidates for being replaced
                    with constants or parameters? If so, make the necessary
                    updates to the code.
Exercise      DRY.2
Project WebShopV10
Project EmployeeV10
Description   The project contains two existing classes Teacher and ITSupporter.
              They have quite a lot in common, so there is a lot of code duplication
              to get rid of.
Project RolePlayV23
Description   The project contains a working role-play system. Any character in the
              game is represented by an object of the class Character.
Project SimpleGeometry
Description   The project contains the (abstract) class Shape, with an abstract
              property Area. The class also contains a static method FindTotalArea,
              that should calculate the total area of a list of shapes
Steps            1. Create two classes Circle and Rectangle. Both classes should
                    inherit from Shape, and therefore implement the abstract
                    property Area. You also need to figure out what instance fields,
                    etc. the two classes need (if you need the value of π (pi), you
                    can get it by writing Math.PI).
                 2. Implement the FindTotalArea method properly, such that it
                    finds the total area of a list of shapes.
                 3. In the sandbox area, fill in some shapes in the given list, and see
                    if your implementation works as expected
Exercise      OOPNex.4
Project FilteringV10
Description   The project contains a class Filter, with a FilterValues method. The
              method filters out values higher than 10 from a list of integers. The
              project also contains an interface IFilterCondition.
Steps            1. Figure out how you can use the interface IFilterCondition to
                    change the FilterValues method, into a method that can filter a
                    list of integers according to any condition. That is, the condition
                    itself has to somehow become a parameter to the method. Try
                    out your solution with a couple of conditions.
                 2. Figure out how you can apply several filter conditions to a list in
                    a single method call.
                 3. Filtering is a very generic operation. Maybe some of the .NET
                    collection classes already support filtering…?
Exercise      OOPNex.5
Project CarDealershipV05
Description   The project contains a simple class Car, which contains a few proper-
              ties. In the sandbox area, we attempt to print out Car objects, and
              perform some comparisons between Car objects.
Steps            1. Run the program as-is, and observe the result. Can you figure
                    out when the comparisons return true?
                 2. In the Car class, uncomment the Equals method (only that
                    method), and run the program again. What has changed?
                 3. Uncomment the rest of the code in the Car class, and run the
                    program again. What has changed?
                 4. The printing of Car objects is still not very satisfying. In the Car
                    class, override the ToString method, so that it returns a string
                    giving a reasonable description of the Car object. Run the
                    program again, and see what difference it makes.
Exercise      OOPNex.6
Project CompanyV10
Description   The project contains very little from the start. There is an Employee
              class with a Name property, and some abstract properties. Concerning
              salary calculation, only some very general rules exist:
                  Part of the salary is a fixed amount
                  Part of the salary is a bonus amount
                  The bonus amount is paid if a certain condition is met
              Specific definitions of the rules should be defined in classes that
              inherit from Employee
Steps            1. Create a Worker class. The class should inherit from Employee.
                    For a worker, the below rules apply:
                     A worker works a fixed amount of hours per month
                     A worker is paid a fixed amount per hour
                     A worker does not receive any sort of bonus
Project ExamAdmV10
Description   The project contains a simple GUI for an exam administration system.
              In this version, you can just type in a name, a subject and a test score
              for an exam. The data is entered through two text boxes and a slider
              control.
Steps            1. Open the project, and open the MainPage.xaml file. Even
                    though the file contains quite a bit of XAML, we only need to
                    focus on the three named controls, with the names student-
                    Name, subject and score (two TextBox controls and a Slider
                    control). Make sure you can find these three controls in the
                    XAML code.
                 2. We want to bind three TextBlock controls to the value of the
                    three named controls. The three TextBlock controls are all part
                    of the top line of the GUI, which consists of a total of six
                    TextBlock controls. For each of the three relevant TextBlock
                    controls, figure out which specific named control to bind to.
                 3. Now create the actual bindings, using the syntax described in
                    the notes (for a Slider, you bind to the Value property; for a
                    TextBox, you bind to the Text property).
                 4. Test that your bindings work as expected
Exercise      DBI.1
Project ExamAdmV11
Purpose Create data bindings between GUI controls and a domain object
Description   The project is identical to the project from ExamAdmV10, except that
              a class Student has been added. Right now, the constructor in Student
              just sets the properties to some fixed values.
Steps            1. Open the MainPage.xaml file, and add a data context to the
                    Page control, specifying Student as the data context. See the
                    notes for the syntax for adding a data context.
                 2. Bind the three relevant TextBlock controls (the same as in the
                    previous exercise) to the corresponding properties on the
                    Student class. Again, see the notes if you cannot remember the
                    syntax for this.
                 3. Also create bindings for the three named controls, such that
                    each control – or more precisely; the relevant property in each
                    control – is bound to the corresponding Student property.
                 4. Run the application, and check that the bindings work as
                    expected. Try to change the values as well. Are the changes
                    reflected in the text line at the top?
Exercise      DBI.2
Project ExamAdmV12
Purpose       Create two-way data bindings between GUI controls and a domain
              object
Description   The project starts off where the previous exercise left off. The project
              does contain data bindings, but changes in the values are still not
              reflected in the rest of the GUI.
Steps            1. Open and run the application. Confirm that changes in the
                    values are not reflected in the top text line.
                 2. Open the Student class. All three properties now have a set-
                    part as well. Now let Student inherit from the INotifyProperty-
                    Changed interface, and implement the OnPropertyChanged
                    method (if ReSharper if installed, Visual Studio can generate the
                    code for you. If not, you can simply copy-paste the code from
                    the notes).
                 3. Run the application again – are value changes now reflected in
                    the text line?
                 4. For each property in Student, add a call to OnPropertyChanged
                    to the set-part of the property, after the value has been set.
                 5. Run the application again – are value changes now reflected in
                    the text line?
                 6. For each of the three bindings for the named controls (not the
                    TextBlock control), update the binding mode to TwoWay.
                 7. Run the application again – are value changes now reflected in
                    the text line?
                 8. Why don’t we need to update the three TextBlock bindings to
                    being TwoWay?
Exercise      DBI.3
Project ExamAdmV13
Steps            1. Open the StudentCollection class, and make sure you under-
                    stand the instance fields and properties it contains (except the
                    NewSubject property)
                 2. Open the MainPage.xaml file. The bindings are now a bit more
                    complex, since the data context is now StudentCollection. Most
                    properties are now bound to the corresponding property on the
                    SelectedStudent property (i.e. Student object). Make sure you
                    understand the new bindings.
                 3. Run the application (ignoring the two extra lines beneath
                    “Score”). The application does work, since updates to name,
                    score and subject are reflected in the top text line (try it!).
                 4. In the “New subject” line, the intention is that when a new
                    subject is entered, it should show up in the Subject combo-box.
                    Confirm that this is not the case right now (remember that you
                    must leave the text box, before the update is triggered).
                 5. The “No. of subjects” field tells how many entries the _subjects
                    list in StudentCollection contains. Right now, the number stays
                    at 5. Figure out how to create a binding for the text box next to
                    the “New subject” text, such that a new entry is indeed added
                    to _subjects (Hint: Take a look at the NewSubject property in
                    StudentCollection).
                 6. Once this binding works, the number should increase every
                    time a new subject is added. Still, the new subjects do not show
                    up in the combo-box. Figure out why this is the case, and fix it.
                    (Hint: Are we using the correct collection class for _subjects?)
Exercise      DBI.4
Project ExamAdmV14
Description   The application main view contains a ListView control, where the
              ItemsSource property is bound to the Students property on the
              StudentCollection class. The list contains five students. However, the
              presentation of the students in the list view is not optimal.
Project ExamAdmV15
Description   The application main view again contains a ListView control, where
              the ItemsSource property is bound to the Students property on the
              StudentCollection class. The list contains five students, and a reason-
              able data template has been provided for presentation. The Student
              class has however been extended with several additional properties.
Project ExamAdmV16
Steps         The steps needed to create deletion functionality are very similar to
              the steps described in the notes. Almost all changes are done in the
              StudentCollection class. It can be assumed that the Name property
              can be used as a key for Student objects.
                 10.Check that you can now delete students from the view!
Exercise      DBI.7
Project ExamAdmV17
Project ExamAdmV18
Description   MEMO: Finish the Show/Hide details feature in the Student view
              From: Maurice Fischer (StudentSoft A/S CTO)
              TO: EASJ Intern (can’t remember the name…)
              Hi,
              Unfortunately, our main developer on the Exam Administration
              application died yesterday, due to an unfortunate incident involving a
              hamster, three small oranges and a large piece of brown cardboard.
              We would therefore like you to finish up the Show/Hide Details
              feature he was working on in the Students view. I think it was
              something about being able to toggle the visibility of parts of the
              Details view on and off, using a ToggleSwitch or something… Anyway,
              you can probably figure it out by looking in the C# project, as he said
              he was “almost done” with it, and he always puts…uhh, used to put
              comments in the code. I would like a demo of it later today, as we are
              shipping a new version of the application tomorrow.
Regards,
M. Fischer
Project ExamAdmV20
Description   The project initially contains a class Student, which acts both as a
              domain class and a “provider” to the main view (via data bindings in
              MainPage.xaml).
Steps         Add a new class StudentViewModel to the project, which will acts a the
              ViewModel in an MVVM architecture. This involves:
                 1. Create the class StudentViewModel.
                 2. Add an instance field _domainObject of type Student to StudentView-
                     Model, and initialise it to refer to a new Student object in the constructor.
                 3. Let StudentViewModel inherit from INotifyPropertyChanged, and
                     generate the code needed (Tip: click the lightbulb ). If the includes are
                     not generated automatically, add to the top of the file:
                           using System.ComponentModel;
                           using System.Runtime.CompilerServices;
                 4. Add properties Name, Subject and Score to StudentViewModel, in the
                    style described in the notes.
                 5. Clean up the Student class, such that it no longer inherits from
                    INotifyPropertyChanged
                 6. Change the data context in MainPage.xaml, and check that the new
                    bindings work as expected.
                 7. Now create a new property TopLineText in StudentViewModel, of type
                    string. The intention is that this property should provide enough
                    information to enable you to delete the six TextBlocks in the top line of
                    the GUI, and replace them with a single TextBlock, that binds to
                    TopLineText.
                 8. Delete the six TextBlocks, replace them with a single TextBlock, and bind
                    the new TextBlock to TopLineText. Are changes to the data reflected in
                    the top text line?
                 9. Add extra calls of OnPropertyChanged to the Name, Subject and Score
                    properties, in the style described in the notes. Check that changes are now
                    reflected in the top text line
Exercise      MVVM.1
Project ExamAdmV21
Project ExamAdmV22
              In MainPage.xaml
                  5. Add a Delete button just after the ListView control, and bind its Command
                     property to DeletionCommand
                  6. Build and run the application. Does the Delete button work as it should
                     (probably not…)
Project ExamAdmV23
Description   The project contains a working Master view with delete functionality.
              All domain-specific classes are located in the folder DomainClasses.
              A number of base classes are available in the folder BaseClasses, but
              are not used yet.
Steps            1. Let the Student class inherit from DomainClassBase<string>. This will
                    require that you override the get-part of the property Key. The property
                    should just return _name (name acts as key for a Student object).
                 2. Let the StudentModel class inherit from ModelBase<Student, string>. You
                    can then delete everything else from the StudentModel class except the
                    constructor. In the constructor, change the calls of _students.Add to use
                    the base class method Add.
                 3. Let the StudentItemViewModel class inherit from ItemViewModel-
                    Base<Student>. The constructor must then call the base class constructor
                    with obj as parameter. Also delete the instance field _domainObject, and
                    replace the use of _domainObject with DomainObject in the properties.
                 4. Let the StudentMasterViewModel class inherit from MasterViewModel-
                    Base<Student, string>. You can then delete the method GetStudentItem-
                    ViewModelCollection from the class.
                 5. Open the StudentViewModelFactory.cs file, and uncomment the class
                    StudentViewModelFactory. (tip: select all of the code, and press Ctrl+K+U)
                 6. Let the StudentMasterDetailsViewModel class inherit from
                    MasterDetailsViewModelBase<Student, string>. Then delete everything
                    (yes, everything) from the class…
                 7. Implement the constructor for StudentMasterDetailsViewModel like this:
                          public StudentMasterDetailsViewModel()
                          : base(new StudentViewModelFactory(), new StudentModel())
                          {}
                 8. In MainPage.xaml, change the binding of ItemsSource to ItemView-
                    ModelCollection, and the binding of SelectedItem to ItemViewModel-
                    Selected
                 9. Make sure all files are saved, then build and run the application
Exercise      Files.1
Project NoteBookV10
Description   The given application contains a very simple system for creating
              notes. A note consists of a title and some content. It is not allowed to
              have two notes with the same title. However, the application does not
              support saving and loading of notes yet.
                 8. Rebuild the application, and see if you can now load and save notes.
                    Create some notes, click Save, close the application, start it again, click
                    Load, and see if the saved notes reappear.
Exercise      Excep.1
Project NoteBookV20
Description   The given application checks that notes cannot have the same title
              (try it!), but the implementation is quite a mess… The handling is all
              done in the set-part of the Title property in NoteDetailsViewModel,
              with several calls to the model and the master-details view model
Steps         Our aim is to clean up the error handling. This involves using exceptions for error
              signaling and handling, and also to distribute various responsibilities to the proper
              classes. An exception class TitleExistsException is included in the project.
                 1. In the NoteModel class, add checks to the methods Add and UpdateTitle,
                    such that a TitleExistsException is thrown if the new title exists
                 2. In the NoteMasterDetailsViewModel class, uncomment the method
                    UpdateTitle. See if you understand why the method is structured in this
                    particular way.
                 3. In the NoteDetailsViewModel class, go to the set-part of the Title
                    property. Remove ALL the code in the set-part, and replace it with a single
                    line of code:
_masterDetailsViewModel.UpdateTitle(value);
Project MVVMStarterStudent
Description   A domain class Student has been added to the MVVMStarter frame-
              work application – now the subsequent steps described in the guide
              need to be performed.