UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 124 DATE: 25 October 2010 TIME: 150 minutes MARKS: 100 ASSESSORS: Prof. P.J. Blignaut & Mrs. R.D. Wario MODERATOR: Dr. E. Nel

 This is an open-book test. You may use your text book of this course but no other material is permissible. That means that no saved examples or other electronic material may be used.

SECTION A (45)

 Answer the following questions on the answer sheet that will be given to you.

Question 1

Consider a system that will allow the user to enter plant details.

1.1 Develop a class, CPlant. The class will be used as a base class for all types of plants and no objects of CPlant will be instantiated. (10) (i) Provide a read/write property for the scientific name of the plant. (ii) Provide a protected data member for the size of the plant which will always be an integer. For a tree it will refer to its typical height and for a flower it will refer to its typical diameter. Provide a read only property for this data member. (iii) Provide a public method, SetSize(), that will return no value and could be overridden by sub-classes. The method should have one parameter that will be used to assign a value to the size of the plant.

1.2 Develop a sub-class for CPlant, namely CTree. (11) (i) Provide two overloaded constructors, one that will hard-code the scientific name as "Unknown" and one that will allow the class user to provide the scientific name when an object is instantiated. (ii) Provide a public method, SetSize(), that overrides the method of the same name in the base class and assign the size of the tree. The method should enforce a business rule that the size of the tree should at least be 1 m and not bigger than 20 m. The method should display a message if the size does not adhere to these restrictions.

1.3 Write the code to instantiate an object, Pine, with a scientific name of "Pinus sylvestris". Set the size of the pine tree to 4 m. (4) [25] 2 Question 2

Consider the following screen print and then complete the code for the Browse button to list all files in the selected folder together with creation date and attributes: [13]

private void btnBrowse_Click(object sender, EventArgs e) { //Set font to proportional font to enable proper alignment lstFiles.Font = ...... ; //2.1 (1) //Clear list box ...... ; //2.2 (1) //Set initial folder dlgFolder.SelectedPath = Application. ...; //2.3 (1) //Open FolderBrowserDialog and check user’s selection if (...... ) //2.4 (2) { //Display selected folder txtFolder.Text = dlgFolder. ...; //2.5 (1) //Instantiate DirectoryInfo object DirectoryInfo dir = ...... ; //2.6 (2) //Step through files in the selected folder and add to list box foreach (...... ) //2.7 (2) { ...... ; //2.8 (3) } } }

Question 3

Consider the following table from a database, Contacts:

3.1 Write an SQL statement that will retrieve all fields from all records in the table, sorted according to the surname. (4) 3.2 Write an SQL statement that will display all details of a person with the surname “Smith”. (3) [7] 3 SECTION B (60)

 Answer the following questions by developing the solutions in C#.  Make sure that you enter your name, student number and question number for every question, every form and every class in a comment block at the top of the code window. You will not get marks if you do it, but you will you lose 3 marks for every class if you don’t do it. You might even get zero for the entire test of you don’t do it!  Make sure that you give appropriate names to all controls and variables.

Question 4

Consider the following scenario: You will be provided with a text file with entries in two fields that are separated by a tab. The first field contains an Afrikaans keyword and the second field contains the equivalent English word or words. Develop a program that will allow the user to enter an Afrikaans keyword in the text box as in the example and then display the English equivalent. Display an appropriate message if the Afrikaans word does not appear in the dictionary.

[30] Question 5

A file with the database of question 3 will be provided to you.

5.1 Develop a Windows forms application as in the screen print. (5)

5.2 Write a method that will display all records from tblContacts in a grid. Call the method from the form’s Load() event handler. (14)

5.3 Write a method that will save any changes to the grid to the database. Call the method from the form’s FormClosing() event handler. (8)

5.4 When a user enters a surname in the text box and clicks on the Search button, the grid must display only users with that surname. If the surname does not exist in the database, the grid must be empty. If the text box is empty, the grid must display all records. You may use the same method that was done in 5.2 and change it a bit. (4) [30]

Submission instructions

Step 1: Call a demmie. Step 2: Open Internet Explorer, go to the URL that will be provided during the test. Browse for your Studentnumber_Surname folder on the T drive and click the Submit button. Step 3: Copy the entire Studentnumber_Surname folder to the demmie’s flash drive. Step 4: Sign the demmie’s list to confirm that you have saved all your work as indicated above. 4 MEMORANDUM

Section A

Question 1

1.1 abstract class CPlant { public string ScientificName { get; set; } 

protected int iSize;  public int Size { get { return iSize; } } 

public virtual void SetSize(int _Size)  { } }

1.2 class CTree : CPlant { public CTree() { ScientificName = "Unknown";  }

public CTree(string _ScientificName)  { ScientificName = _ScientificName;  }

public override void SetSize(int _Size)  { if((_Size >= 1) && (_Size <= 20))  iSize = _Size;  else MessageBox.Show("Invalid size");  } }

1.3 CTree Pine = new CTree("Pinus sylvestris");  Pine.SetSize(4); 

2. private void btnBrowse_Click(object sender, EventArgs e) { lstFiles.Font = new Font("Courier New", 8);  lstFiles.Items.Clear(); dlgFolder.SelectedPath = Application.StartupPath;  if (dlgFolder.ShowDialog() == DialogResult.OK)  { txtFolder.Text = dlgFolder.SelectedPath;  DirectoryInfo dir = new DirectoryInfo(txtFolder.Text);  foreach (FileInfo sFile in dir.GetFiles("*.*")) { lstFiles.Items.Add(sFile.Name.PadRight(40) + sFile.CreationTime.ToString().PadRight(20) + sFile.Attributes.ToString()); } } }

3.1 SELECT *  FROM tblContacts  ORDER BY Surname;  3.2 SELECT * FROM tblContacts  5 WHERE Surname = “Smith”;  6 Section B

Question 4

Form  (file name and form name) Afrikaans  (Naming not necessary) txtAfrikaans  lblEnglish  btnFind  btnClose  private void btnFind_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; StreamReader f = new StreamReader("Dictionary.txt");  bool isFound = false;  string sLine = "";  string[] Fields;  while (!f.EndOfStream && !isFound)  { sLine = f.ReadLine(); Fields = sLine.Split('\t');  if (Fields[0] == txtAfrikaans.Text)  { lblEnglish.Text = Fields[1];  isFound = true;  } } if (!isFound)  MessageBox.Show("\"" + txtAfrikaans.Text + "\" not found.");  f.Close(); Cursor.Current = Cursors.Default; } //btnFind_Click 7 Question 5

5.1 Form Named  Controls (All named except labels) 

5.2 cnContacts  Named  ConnectionString  (“Provider=Microsoft.ACE.OLEDB.12.0;Data Source="...\Contacts.accdb"); daContacts  Named 

private void Fill() { string sql = "SELECT * FROM tblContacts ";  if (txtSurname.Text != "")  sql += "WHERE Surname = '" + txtSurname.Text + "' ";  sql += "ORDER BY Surname";

daContacts.SelectCommand.CommandText = sql;  daContacts.SelectCommand.Connection = cnContacts;  DataTable tblContacts = new DataTable(); daContacts.Fill(tblContacts);  dgvContacts.DataSource = tblContacts;  } //Fill

private void CfrmContacts_Load(object sender, EventArgs e) { Fill(); }

5.3 private void Update() { OleDbCommandBuilder cmdbldr = new OleDbCommandBuilder(daContacts);  daContacts.InsertCommand = cmdbldr.GetInsertCommand(); daContacts.UpdateCommand = cmdbldr.GetUpdateCommand(); daContacts.DeleteCommand = cmdbldr.GetDeleteCommand();

daContacts.Update((DataTable)dgvContacts.DataSource);  } //Update

private void CfrmContacts_FormClosing(object sender, FormClosingEventArgs e) { Update(); }

5.4 private void btnSearch_Click(object sender, EventArgs e) { Fill(); }