You are on page 1of 7

UNIVERSITY OF THE FREE STATE

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS

CSIS1624 AEGROTAT TEST

DATE: 21 November 2016 MARKS: 117


ASSESSOR: Mr. D.J. Wium TIME: 180 minutes
MODERATOR: Prof. P.J. Blignaut

 Remember to add a comment block containing your student number, name and
surname, today's date and the name of the question at the top of every class. Marks
will be deducted for each file where the comment block is missing.
 Adhere to all programming and naming conventions as taught and ensure that your
code is aligned properly.
 This is not an open-book test.

Question 1 (13)
Create a C# Console application using Visual Studio and name your project Question1.
The application should help the user to build a sandwich by requesting which toppings should
be added and by calculating the final price of the sandwich. See the screenshot below for an
example of what is expected.

1
Add a class named CSandwich.cs to your project. This class should contain the following:
 A property named Toppings that returns all the toppings on the sandwich. It should be
impossible to change the value of this property from outside the class.
 A read-only property named Price that returns the price of the sandwich. The base
price for a sandwich is R 10.00 and R 5.00 is added for every topping.
 A constructor that takes no parameters. The constructor should initialise the data
structure of the Toppings property.
 A public method named Details() that returns a string. This string should list the
toppings on the sandwich and display its price, as in the screenshot.
 A public method named AddTopping() that accepts the topping to be added as a
parameter and adds it to the data structure of the Toppings property.

The Main() method is provided. You may either re-type the code below or download it from
Blackboard, where it is available from the Aegrotat Test link.

static void Main(string[] args)


{
CSandwich sandwich = new CSandwich();
Console.WriteLine("Build your own sandwich\n=======================");
bool isAnother = true;
do
{
Console.Write("\nWhich topping would you like to add? ");
sandwich.AddTopping(Console.ReadLine());
Console.Write("Add another topping (Y/N)? ");
if (Console.ReadKey().KeyChar.ToString().ToUpper() != "Y")
isAnother = false;
}
while (isAnother);
Console.WriteLine("\n\n" + sandwich.Details() + "\n");
Console.Write("Press any key to exit...");
Console.ReadKey();
}

2
Question 2 (50)
Create a C# Windows Forms application using Visual Studio and name your project
Question2.
The application should allow the user to add batsmen, bowlers and wicketkeepers to a list of
cricketers, as shown in the screenshot below.

Add a class named CCricketer.cs to your project, of which no object can be instantiated. This
class should contain the following:
 Properties for the player’s surname, batting average and caps. These properties must
be readable from anywhere, but one should only be able to write to them from the
CCricketer.cs class or any derived classes.
 A read-only property named Details that returns the output of the GetDetails() method.
 An abstract method named GetDetails() that returns a string.

Add three classes named CBatsmen.cs, CBowler.cs and CWicketKeeper.cs that inherit from
CCricketer.cs. These classes should contain the following:
 The CBowler.cs class should contain a property for a bowler’s bowling average. It
should be impossible to write to this property from outside its class.
 The CWicketKeeper.cs class should contain a property for the number of stumpings
that a wicket-keeper has performed. The access modifiers for this property should be
the same as for the property for a bowler’s bowling average.
 Each of the three classes must contain a constructor that accepts parameters for all
the properties of an object of that class. The constructor should assign the values of
the parameters to the properties. If any of the numeric parameters are less than zero,
a value of zero should be assigned instead.

3
 The GetDetails() method should be implemented to display the details of the player on
a single line, as in the list box on the right-hand side of the screenshot.

Implement the form class according to the following instructions:


 When a different radio button is selected in the “Add cricketers” group box, the text
property of all textboxes should be cleared and only the labels and textboxes relevant
to the selected type of cricketer should be enabled (see the screenshot for an
example).
 When the “Add player” button is clicked, the following should happen:
o If a player with the surname entered in the textbox already exists, the user
should be notified of this fact and no further action should be taken.
o If the correct data types have been added in all the textboxes, a cricketer should
be instantiated and added to a list containing all cricketers.
o When a cricketer is added, the display in the list box should be updated to
potentially include this player.
o When a cricketer is added, all textboxes should be cleared.
o When a cricketer is added, the user should receive a confirmation message.
o If invalid data was entered, the user must be notified through a suitable
message.
 When a different radio button is selected in the “Display cricketers” group box, the list
box should only display all players of that type, for instance all batsmen or all bowlers.

Hint
One way to update the display in the list box is by doing the following:
 Set the DisplayMember property of the list box to “Details”.
 Whenever another radio button is checked, change the DataSource property of the list
box to a list containing only the relevant players.
 The lists mentioned in the previous step can be generated with the help of LINQ. The
following snippet of code might help: (i => i is CBatsman)

4
Question 3 (54)
Create a C# Windows Forms application using Visual Studio and name your project
Question3.
The application should allow the user to add music albums to a library and to load and save
such libraries. See the screenshot below for an example of what is expected.

Add a class named CAlbum.cs to your project. This class should contain the following:
 Properties for the name of the album, the name of the artist, the year in which the
album was released and the number of tracks on the album. These properties should
be readable everywhere, but it should be impossible to write to them from outside the
class.
 A constructor that accepts values for all the properties of the class as parameters and
assigns them to the properties.

Add a class named CLibrary.cs to your project. This class should contain the following:
 A property named Albums that is a list of all the albums in the library. This property
should be readable everywhere, but one should not be able to write to it from outside
the class.
 A constructor that accepts no parameters. The constructor should initialise the Albums
property.
 A method named Display(). This method should return a string. The string that is
returned should display the details of all albums in the library. In the screenshot, all the
text displayed in the rich text box is a string that was returned by this method.

5
 A method named Save(). This method accepts a string which denotes the path to a
library file and saves the library to that file after serializing the library.
 A static method called Read(), that returns an object of type CLibrary. This method
accepts a string which denotes the path to a library file and then deserializes that file.

Implement your form class according to the following instructions:


 When the button in the “Select library” group box is clicked, the following should
happen:
o The user should be allowed to select a library by using a suitable dialog. If the
user selects a file that does not exist, one should be created. Hint: When using
File.Create(), a FileStream to the created file is automatically opened, which
might cause problems. This can be avoided by using File.Create().Close().
o The text box in the “Select library” group box should display the file name of
the selected library.
o The selected library should be read and the details of the albums contained in
the library should be displayed in the rich text box.
 When the user clicks the “Add album” button, the following should happen:
o If no library is selected, the user should be notified that he has to select a library
first.
o If the library already contains an album with the specified combination of artist
name and album name, he should be notified that he cannot add a duplicate
album (Note that he may add an album by the same artist, or an album with the
same name by another artist).
o If a library is selected and the album to be added is not a duplicate, then the
album should be added to the library. Additionally, the updated library should
be saved and the text in the rich text box should be updated to include the new
album. If any of these steps, the user should be notified through a suitable error
message.
o Finally, the text boxes for the album name and artist name should be cleared,
the date time picker should be reset to the current date and the value of the
numeric-up-down should be reset to 1.
 Ensure that no invalid values are available in the numeric-up-down control (an album
must contain at least one track).

6
Hints
The following code will cause the date time picker to display the year only and to contain up-
down arrows:

dtpYear.Format = DateTimePickerFormat.Custom;
dtpYear.CustomFormat = "yyyy";
dtpYear.ShowUpDown = true;

By setting a suitable dialog’s CheckFileExists property to false, it will accept a filename of a


file that does not yet exist.

Submission Procedure
When you are ready to submit, add all of your project folders to a single folder and rename
that folder to your student number. Submit a .zip archive of the folder on Blackboard.

You might also like