private void btnCalculate_Click(object sender, EventArgs e) { //declare output variables double assignmentMark; double workshopMark; double participationMark; double finalMark; //validate data entered by user try { //check validity // if you don't have a valid value from user, don't execute the following blocks if (IsValidData()) { //increment number of student every time a button is clicked nrStudents++; //get inputs from the user assignmentMark = Convert.ToDouble(txtAssignments.Text); workshopMark = Convert.ToDouble(txtWorkshop.Text); participationMark = Convert.ToDouble(txtParticipation.Text); // create object of new student Student nextStudent = new Student(assignmentMark, workshopMark, participationMark); nextStudent.Name = txtStudentName.Text; //calculate new class average by using student object property FinalMark totalMarks += nextStudent.FinalMark; averageMark = totalMarks / nrStudents; averageMark = Math.Round(averageMark)/100; //calculate how many students got over 50% and passed and how many students failed. //Display results in textboxes if (nextStudent.FinalMark >= 50) { studentsPassed++; txtPassed.Text = studentsPassed.ToString(); } else { studentsFailed++; txtFailed.Text = studentsFailed.ToString(); } students.Add(nextStudent); // add the new student to the list //display student info in the listbox //listbox does not support newlines, so have to split the resulting string into several //since the string deliminator is not one character but several, use Regex.Split ltbStudentInfo.Items.Add(nrStudents + ". "); //this displays number of thestudent as you enter them foreach (string s in Regex.Split(nextStudent.ToString(), "\n")) ltbStudentInfo.Items.Add(s); //display final mark and grade finalMark = nextStudent.FinalMark*0.01;// get finalMark as fraction so it can be displayed as a percent txtStudentMark.Text = finalMark.ToString("P0"); txtStudentGrade.Text = nextStudent.LetterGrade; txtAverageMark.Text = averageMark.ToString("P0"); } } //display if an exception that has not been anticipated is thrown catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + //get description of error ex.GetType().ToString() + "\n" + //get the type of class that was used to create the exception object ex.StackTrace, "Exception"); //stack trace is a list of methods called before exception occurred } }
private void btnCalculate_Click(object sender, EventArgs e) { // declare input variables double assignmentMark; double workshopMark; double participationMark; string studentName; //declare output variables double finalMark; string finalGrade; string listboxOutput; //validate data entered by user try { //check validity // if you don't have a valid value from user, don't execute the following blocks if (IsValidData()) { //increment number of student every time a button is clicked numberOfStudents++; //give an error message if user tries to enter more students than max number allowed if (numberOfStudents > maxNumberOfStudents) { MessageBox.Show("You cannot enter more than " + maxNumberOfStudents + " students."); btnCalculate.Enabled = false; //this will disable the calculate button so that user cannot enter more data return; } //declare arrays to store, an array of student names, marks and letter grades // added one to index to accomodate the fact that there is no student zero, we start at student one //added another one to ensure there is no exception warning for reaching outside of index bounds //there will be an error message that will disable the calculate button if student count reaches maximum string[] studentNames = new string[maxNumberOfStudents+1+1]; double[] studentMarks = new double[maxNumberOfStudents+1+1]; string[] studentGrades = new string[maxNumberOfStudents+1+1]; //get input from user studentName = txtStudentName.Text; //add student name to the studentNames array studentNames[numberOfStudents] = studentName; Student nextStudent = new Student(); // create object of new student nextStudent.Name = txtStudentName.Text; //call calculate method that will determine all marks and grades calculateGradeAndMarks(out assignmentMark, out workshopMark, out participationMark, out finalMark, out finalGrade); //store marks and grades in arrays studentMarks[numberOfStudents] = finalMark*100; //calculated value is a fraction so need to multiply by 100 studentGrades[numberOfStudents] = finalGrade; nextStudent.Mark = finalMark * 100; nextStudent.Grade = finalGrade; //calculate how many students got over 50% and passed and how many students failed. //Display results in textboxes if (finalMark >= 0.5) { studentsPassed++; txtPassed.Text = studentsPassed.ToString(); } else { studentsFailed++; txtFailed.Text = studentsFailed.ToString(); } //add results to listbox describing history so far listboxOutput = "Student " + numberOfStudents + ", " + studentNames[numberOfStudents] + ", Final Mark: " + studentMarks[numberOfStudents] + "%, Final Grade: " + studentGrades[numberOfStudents]; ltbStudentInfo.Items.Add(listboxOutput); } } //display if an exception that has not been anticipated is thrown catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + //get description of error ex.GetType().ToString() + "\n" + //get the type of class that was used to create the exception object ex.StackTrace, "Exception"); //stack trace is a list of methods called before exception occurred } }