5
UNIVERSITY OF THE FREE STATE
DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS
RIS 124
DATE: 27 October 2010
TIME: 180 minutes MARKS: 132
ASSESSORS: Prof. P.J. Blignaut & Mrs. R.D. Wario
MODERATOR: Dr. E. Nel
SECTION A
· Answer the following questions on the answer sheet that will be given to you.
Question 1 (22)
1.1 What namespace must be added to a Visual Studio project to enable usage of the File, FileInfo, Directory and DirectoryInfo classes? (1)
1.2 What is the difference between the File and FileInfo classes? (2)
1.3 Write a segment of code to delete a file from disk. Use an OpenFileDialog to allow the user to select a file. Check that the file exists and confirm that the user wants to delete the file. (8)
Hints:
(i) Use the ShowDialog method of the OpenFileDialog. Compare the user’s feedback with DialogResult.OK.
(ii) When prompting the user for confirmation through a message box, use MessageBoxButtons.YesNo and compare his feedback with DialogResult.Yes.
(iii) Use the methods Exists and Delete in the File class.
(iv) The following pseudocode might help to explain the process.
if (user selected a file)
if (file exists)
if (user really wants to delete the file)
Delete file
1.4 Write a segment of code to read a single person’s name (string) and age (int) from a binary file, NameAge.bin and display it in a message box. (11)
Question 2 (15)
2. An MS Access database, Shop.accdb, keeps track of clients who have accounts at a large clothing shop.
· One table keeps the data of clients. A second table keeps details of stock items with fields for item ID, description, cost price and retail price. A third table keeps track of transactions. For each transaction, the client ID, item ID and date of transaction are saved.
· One of the queries, qryTransactions, lists the transaction date, description and retail price for every transaction.
2.1 Write an SQL query that will list all transaction details along with the respective client initials and surname and item description and retail price in order of transaction date. (6)
2.2 Consider the following form and complete the event handler for the SelectedIndexChanged event handler of the combo box. This event handler will display records according to the query in 2.1 but for the selected client ID only. (9)
2.2 private void cmboClientID_SelectedIndexChanged(object sender, EventArgs e)
{
//Create and open database connection
OleDbConnection cnShop
= new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Shop.accdb");
...;
//Define query and assign to data adapter
string sql = ...;
if (cmboClientID.Text != "All")
sql += ...;
sql += ...;
OleDbDataAdapter daTransactions = new OleDbDataAdapter(sql, cnShop);
//Create object to store data in memory
DataTable ... ;
//Fill data table with data from the database
... ;
//Display data in the grid
... ;
}
SECTION B
· 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 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 question 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 3
Develop a program as in the example that will allow the user to enter a sentence and then count the number of words in the sentence. Note that words may be separated by any number of spaces. You may not use the method Split().
Hint: You can count the words by counting the transitions from space to no-space. (13)
Question 4
In Gymnastics and many other sports where judges award a subjective point for an activity, it is custom to disregard the lowest and highest points and allocate the average of the remaining points to the athlete. Develop a Windows Forms application as in the example that will allow a user to enter the points of five judges and calculate and display the average of the middle three points. Check for invalid input. (22)
Question 5 (60)
Develop an application that will allow the user to enter the personal details of an employee and then produce a report with the employee's monthly payslip. There are two types of employees, namely those that receive a fixed salary at the end of the month and those who work for a commission based on the monthly sales.
5.1 Develop a class, CEmployee. The class must have
(i) properties for the ID, Surname, Name and Date of birth.
(ii) a static field that indicates the percentage of tax that is deducted for all employees.
(iii) a declaration of a virtual method for the net salary that an employee will receive. (10)
5.2 Develop a class for a commissioned employee that is derived from CEmployee. The class must have
(i) a property for the monthly sales of an employee.
(ii) a static field that indicates the commission percentage that applies to all commissioned employees.
(iii) The class must override the method for net salary in the base class and return a percentage of the monthly sales minus the tax. (10)
5.3 Develop a class for a salaried employee.
(i) The class must have a property for the gross salary of an employee.
(ii) The class must override the method for net salary in the base class and return the gross salary minus the percentage of tax that is deducted. (8)
5.4 Implement the classes in a form as in the example. When the user clicks on the Payslip button,
(i) the static field for tax percentage must be assigned a value.
(ii) an employee of the selected type must be instantiated and all properties assigned; if it is a commissioned employee, the static field for commission percentage must be assigned a value;
(iii) a message box must be displayed with the details of the employee and his salary as in the example. (32)
MEMORANDUM
Section A
Question 1 (22)
1.1 System.IO ü
1.2 The File class provides static methodsü for file management while the FileInfo class provides instance methodsü to do the same.
1.3 if (dlgOpen.ShowDialog() == DialogResult.OK) üü
if (File.Exists(dlgOpen.FileName)) üü
if (MessageBox.Show("Sure want to delete file " + dlgOpen.FileName + "?",
"Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
== DialogResult.Yes) üü
File.Delete(dlgOpen.FileName); üü
1.4 FileStream strm = new FileStream(dlgOpen.FileName, FileMode.Open); üü
BinaryReader f = new BinaryReader(strm); üü
string sName = f.ReadString();üü
int iAge = f.ReadInt32();üü
f.Close();ü
strm.Close();ü
MessageBox.Show(sName + "\t" + iAge.ToString());ü
Question 2
2.1 SELECT Transactions.*, Clients.Initials, Clients.Surname,
Items.Description, Items.RetailPrice üü
FROM (Transactions ü
INNER JOIN Clients ON Transactions.ClientID = Clients.ClientID) ü
INNER JOIN Items ON Transactions.ItemID = Items.ItemID ü
ORDER BY TransactionDate ü
2.2 private void FilterData()
{
//Create database connection
OleDbConnection cnShop
= new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Shop.accdb");
cnShop.Open(); ü
//Define query and assign to data adapter
string sql = "SELECT Transactions.*, Clients.Initials, Clients.Surname, “
+ " Items.Description, Items.RetailPrice "
+ "FROM (Transactions "
+ " INNER JOIN Clients ON Transactions.ClientID = Clients.ClientID) "
+ " INNER JOIN Items ON Transactions.ItemID = Items.ItemID ";
if (cmboClientID.Text != "All")
sql += "WHERE Transactions.ClientId = " + cmboClientID.Text + " "; üü
sql += "ORDER BY TransactionDate";
OleDbDataAdapter daTransactions = new OleDbDataAdapter(sql, cnShop);
//Create object to store data in memory
DataTable tblTransactions = new DataTable(); üü
//Fill data table with data from the database
daTransactions.Fill(tblTransactions); üü
//Display data in the grid
dgvTransactions.DataSource = tblTransactions; üü
}
Section B
Question 3 (13)
Form üüü
private void Count1()
{
//Add space to beginning
string s = " " + txtSentence.Text; ü
//Declare variable
int iWords = 0; ü
//Step through characters of the string
for (int i = 1; i < s.Length; i++) üüü
if (s[i - 1] == ' ' & s[i] != ' ') //Start of new word üü
iWords++; ü
//Display output
string sMsg = "Number of words : " + iWords.ToString();ü
MessageBox.Show(sMsg, "WORDS", OK, Info); ü
}
Question 4 (22)
Form üüü
double[] Points; ü
private void btnEnterPoint_Click(object sender, EventArgs e)
{
//Instantiate array
int n = 5;
Points = new double[n]; ü
//Read points
string sPoint; ü
bool isValid = true;
for (int i = 0; i < n; i++)
{
do
{
sPoint = Interaction.InputBox("Point " + (i + 1).ToString() + ": ",
"GYMNASTICS", "0", 200, 200); üü
isValid = double.TryParse(sPoint, out Points[i]); üü
}
while (!isValid); üü
}
btnAverage.Enabled = true;
} //btnEnterPoint_Click
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAverage_Click(object sender, EventArgs e)
{
Array.Sort(Points); üü
int n = 5;
double iSum = 0;
//Add values, starting with second and ending with second last
for (int i = 1; i < Points.Length - 1; i++) üüü
iSum += Points[i]; üü
double dAvg = iSum * 1.0 / (n-2); üü
string sMsg = String.Format("The final point is {0:F2}", dAvg); ü
MessageBox.Show(sMsg, "GYMNASTICS", OK, Info);
}
}
Question 5
5.1 abstract class CEmployee üü
{
public static double TaxPercent; ü
public string ID { get; set; } ü
public string Surname { get; set; } ü
public string Name { get; set; } ü
public DateTime DOB { get; set; } ü
public virtual decimal NetSalary()üü
{
return 0; ü
}
}
5.2 class CCommissionedEmployee : CEmployee üü
{
public static double CommissionPercent; üü
public decimal Sales { get; set; } üü
public override decimal NetSalary()üü
{
decimal decGross = Sales * (decimal)CommissionPercent / 100;
return decGross - (decimal)TaxPercent * decGross/100; üü
}
}
5.3 class CSalariedEmployee : CEmployee üü
{
public decimal GrossSalary {get; set;} üü
public override decimal NetSalary()üü
{
return GrossSalary - (decimal)TaxPercent * GrossSalary/100 ; üü
}
}
5.4 Form üüüü
CEmployee Employee; üü
private void AssignValues()
{
CEmployee.TaxPercent = (double)nudTaxPercent.Value; ü
if (radCommissioned.Checked) ü
{
CCommissionedEmployee.CommissionPercent
= (double)nudCommissionPercentage.Value; ü
Employee = new CCommissionedEmployee();üü
((CCommissionedEmployee)Employee).Sales = decimal.Parse(txtSales.Text); üü
}
else
{
Employee = new CSalariedEmployee();ü
((CSalariedEmployee)Employee).GrossSalary
= decimal.Parse(txtGrossSalary.Text); ü
}
Employee.ID = txtID.Text; ü
Employee.Surname = txtSurname.Text; ü
Employee.Name = txtName.Text; ü
Employee.DOB = dtpDOB.Value; ü
}
private void btnPrint_Click(object sender, EventArgs e)
{
AssignValues();
string sDetails = "ID : " + Employee.ID + "\n"
+ "Surname : " + Employee.Surname + "\n"
+ "Name : " + Employee.Name + "\n"
+ "Date of birth : " + Employee.DOB.ToString("dd MMM yyyy") + "\n";
üü
if (Employee is CSalariedEmployee) üü
{
sDetails += "Employee type : Salaried\n"
+ "Gross salary : "
+ ((CSalariedEmployee)Employee).GrossSalary.ToString("C") + "\n" üü
+ "Net salary : " + Employee.NetSalary().ToString("C"); üü
}
else
{
sDetails += "Employee type : Commissioned\n"
+ "Sales : "
+ ((CCommissionedEmployee)Employee).Sales.ToString("C") + "\n" üü
+ "Net salary : " + Employee.NetSalary().ToString("C"); üü
}
MessageBox.Show(sDetails); ü
}