MainExamination 2016

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

UNIVERSITY OF THE FREE STATE

UNIVERSITEIT VAN DIE VRYSTAAT

BLOEMFONTEIN CAMPUS/KAMPUS

CSIS1624
DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS
DEPARTEMENT REKENAARWETENSKAP EN INFORMATIKA
CONTACT NUMBER/KONTAKNOMMER: 072 967 7092

EXAMINATION/EKSAMEN: MAIN END-OF-YEAR/


HOOF JAAREINDEKSAMEN 2016
PAPER/VRAESTEL 1 (CLOSED-BOOK EXAMINATION/
TOEBOEKEKSAMEN)

ASSESSOR(S)/ 1. Mr D.J. Wium


ASSESSOR(E):

MODERATOR/ 1. Prof P.J. Blignaut


MODERATOR:

TIME/TYD: 4 hours/ure MARKS/ PUNTE: 140

 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.

1
Question 1 (39)
Create a C# Windows Forms application using Visual Studio and name your project
Question1. This application should allow the user to enter details of books and should list
these, as in the screenshot below. The user must be able to decide whether the books are
listed alphabetically according to title or author, or in order of increasing length.

Add a class named CBook.cs to your project. This class should contain the following:
 Two auto-implemented properties for the title and author. It should not be possible to
change their values from outside the class.
 A private data member with a corresponding property for the number of pages that the
book has. The property should prohibit a book from being instantiated with less than
one page.
 A constructor that assigns values to all properties of the class.

The form class should be implemented according to the following instructions:


 Ensure that the numeric-up-down allows all values from 0 to 1000.
 When the user clicks the “Add” button, the following should happen:
o If a book with the same author and title has already been added, the user
should be notified and the duplicate should not be added.
o If the new book is not a duplicate, it should be added to a list containing all the
books, the text boxes should be cleared, the number of pages should be reset
to 1 and focus should return to the “Title” text box. The display in the rich text

2
box on the right should be updated to include the new book. The display should
remain sorted according to the user’s choice.
o If the previous step fails at any point, a run-time error should be prevented and
the user should be notified.
 Whenever the user checks a different radio button, the list of books should immediately
be sorted and the display updated to reflect the newly sorted list.
 Do not worry too much about aligning the fields in the rich text box. Any reasonable
attempt at aligning the fields will receive full marks. Changing the font of the rich text
box to a non-proportional font will make the fields easier to align.

Hints
To determine whether a specific book is already in the list, the following code might be helpful:
books.Any();
In order to sort the books, you might want to consider the following snippet of code:
books = books.OrderBy().ToList();
Remember to add missing using directives using the “Refactor” option in the right-click menu
of unrecognised method calls.

Question 2 (50)
Create a C# Console application using Visual Studio and name your project Question2.
This application will simulate a duel (fight) between two warriors, namely a swordsman and
an archer. The archer gets to shoot one arrow before the swordsman can reach him. Once
the swordsman reaches the archer, they start trading blows until one or both has no health
left. The screenshots below illustrate what is expected.

3
Add a class named CUnit.cs to your form, of which no object can be instantiated. This class
will act as a base class for the CArcher.cs and CSwordsman.cs classes and should contain
the following:
 Two auto-implemented properties, of which the values can only be changed inside this
class or derived classes, for the attack and defence skills of the unit.
 A private data member with a corresponding property for the health of the unit. The
property should prevent the unit from ever having a health less than zero.
 A method, Stab(), which may be overridden in derived classes. This method should
return the damage that a unit does to the opponent during hand-to-hand combat. The
opponent, also of class CUnit, is passed to the method as a parameter, The method
must return an integer, calculated according to the following formula where r is a
Random object.
Attack / _opponent.Defence * r.NextDouble() * 10

Add the CArcher.cs and CSwordsman.cs classes and ensure that they contain the following:
 The CSwordsman.cs class must override the Stab() method and return 2 times the
output from the Stab() method in the base class. This represents a bonus for the
swordsman because he is more adept at stabbing than the archer.
 The CArcher.cs class must contain a property, Archery, for the skill of the archer, of
which the value can only be assigned inside the class.
 The CArcher.cs class must contain a method named Shoot(), which returns an integer
representing the damage that the archer does when shooting an arrow. This integer
should be calculated using the following formula, where r is a Random object.
r.NextDouble() * 3 * Archery
 Both classes should contain constructors that assign values to all the properties of the
classes.

The Main() method as well as the Duel() method, which simulates the duel, is provided. You
can download them from the link to this examination on Blackboard. The Main() method and
the signature of the Duel() method are shown on the next page.

4
static void Main(string[] args)
{
CSwordsman swordsman = (CSwordsman)GetUnit(true);
CArcher archer = (CArcher)GetUnit(false);

Duel(swordsman, archer);

Console.Write("Press any key to exit...");


Console.ReadKey();
}
static void Duel(CSwordsman _swordsman, CArcher _archer)

You are required to implement two methods, namely the GetUnit() method that is called from
the Main() method, along with a predicate method named IsValid().
 GetUnit() has the following method signature: CUnit GetUnit(bool _isSwordsman)
o It must prompt the user for the properties of a unit as in the first screenshot.
The properties are entered as a comma-separated string and the program
should then use the Split method to separate them.
o Next, it should call the IsValid() method to determine whether the input is valid.
o If the input turns out to be valid, a unit must be instantiated and returned.
o If the input is not valid, the user should be notified and be prompted for the
properties again.
o Note that the _isSwordsman parameter indicates whether a swordsman is
requested or not (in which case an archer must be returned).
 IsValid() accepts a unit’s properties (health, attack, defence and potentially archery)
as parameters and determines whether they are valid according to the following
conditions:
o The sum of all properties has to be less than or equal to 100.
o No property is allowed to have a negative value.

Question 3 (51)
Create a C# Windows Forms application using Visual Studio and name your project
Question3. This application should serve as a birthday calendar. The user must be able to add
new persons to the calendar and when the calendar is opened, it must display everyone who
celebrates their birthday on that day. Additionally, if one of these “birthday-boys or –girls” are
selected, their phone number and age must be displayed in order for the user to phone them
with an appropriate birthday wish. The screenshot on the next page illustrates what is
expected.

5
Add a class named CPerson.cs to your project. This class should contain the following:
 Auto-implemented properties for the birthdate, surname, name and age of the person.
Ensure that the values of these properties can only be changed from within the class.
 An additional auto-implemented property named NameSurname, which combines the
person’s name and surname for the purpose of displaying it in a list box, as in the
screenshot. The access modifiers for the accessors of this property should be the
same as those of the other properties.
 A constructor that accepts values for all the properties of the class except for the
person’s age and NameSurname. The constructor should assign values to all the
properties of the class, including Age and NameSurname.
 A method named Details() that returns a string containing a person’s phone number
and age, as in the screenshot.

Implement your form class according to the following instructions:


 By setting the DisplayMember property of the list box to “NameSurname”, you can add
objects of type CPerson to its Items and have their names and surnames displayed as
in the screenshot.
 A file named “Birthdays.txt”, located in the folder of the executable, will be used to store
and read data.
 Declare an array of type CPerson[] named persons on form level. This array will
contain all the entries in the “Birthdays.txt” file. It will be instantiated and populated
when the form is instantiated and will be updated whenever a new person is added.

6
 It was mentioned before that persons must be instantiated when the application loads
and must be updated whenever a new person is added. For this purpose, create a
method, LoadRegister, that does the following:
o First, assign array of length 0 to persons.
o If “Birthdays.txt” does not yet exist, create it. Use File.Create().Close(); to
prevent errors due to an open file stream.
o If the file does exist, read it, instantiate objects of type CPerson using the read
data and add them to persons. You will have to use the static Resize() method
to resize the array before adding to it.
o Finally, when the whole file has been read, create a list containing everyone in
persons who are celebrating birthdays on the present day and assign that list
to the DataSource property of the list box. The following code snippet might
help.
persons.Where(...).ToList();
o Call LoadRegister from the form's constructor.

 When the user clicks the “Add” button, the following should happen:
o The validity of the data entered in the checkboxes and selected in the date time
picker should be determined. Use a predicate method named CheckValidity().
Invalid data includes:
 Any empty text box.
 A duplicate person (one with the same name, surname and birthdate
as an existing person).
 An invalid phone number. Phone numbers must have ten characters
after spaces have been removed and must start with a zero. Allow any
characters in the remaining positions.
o If the data is found to be valid, a new person should be added to the
“Birthdays.txt” file, the user should be notified that the addition was successful,
all text boxes should be cleared and the date time picker’s value should be
reset to the present. Finally, call LoadRegister() again to update persons to
include the new person. Note that you must decide on the format in which the
data is written to the text file.
o If the data is found to be invalid, the user must be notified with a suitable
message informing him why the data he entered is invalid.

 Whenever another person is selected in the list box, his or her details should be
displayed as in the screenshot by calling the object’s Details() method.

7
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