Using Visual Basic, create a project called Student Data. Add a new class “Student.vb” to the project. The Student class should contain four member variables (LastName, FirstName, StudentId, and TestAverage). Next add a constructor to the class that initializes the private member variables. Set up four property procedures for the above four member variables. Set up a read-only property procedure for student grade using the variable name “TestGrade”. The grade will have one of the values “A”, “B”, “C”, “D”, or “F”. Based on the TestAverage, a letter should be assigned to the student grade.
Create an instance of the class “Student” named objStudent”. Write a user-defined sub-procedure that gets data from the text boxes (shown below), and stores it in the object referenced by “objStudent”. Name the sub-procedure “GetData” and define it with only one parameter in the parameter list (objStudent As Student). Within this sub-procedure, you are assigning values from textboxes on the form to the object class members.
Next, you need to save the student’s record to a text file “Students.txt”. Write another sub-procedure (name it SaveRecord) that will save student data to the text file. Remember to write the code to handle any exceptions (ie File Not Found, file is spelled wrong). Write the code for the “Save” button event-handler, to get the data by calling the GetData sub-procedure, and display the grade for the student on the form. Call the “SaveRecord” procedure to save student record. Display a message stating that the student record was saved. Write the appropriate event-handler for the “Exit” button.
Suggested Control Names and Attributes:
|Name Property||Text Property||Control Type||Notes|
|frmStudentData||Student Data||Form||Holds Controls|
|GroupBox1||Student Data||GroupBox||Passive object to contain controls|
|txtLastName||TextBox||Captures last name|
|txtFirstName||TextBox||Captures first name|
|txtIdNumber||TextBox||Captures identification number|
|txtTestAverage||TextBox||Captures test average|
|lblGrade||Label||Displays letter grade|
- Be sure to declare the member variables of the class with a private access specifier.
- The constructor should use the empty string (“”) for the first three class members and 0.0 for TestAverage.
- Use a Select-Case statement to assign the letter grade to TestGrade, based on the value of TestAverage.
- Be sure to write the appropriate code for data validation to handle any exceptions. Use a Try Catch block for this.
- Save the text file in the bin/Debug folder of your project.
Write the Code for frmStudentData:
' Project: Student Data ' Description: Program receives input from the user and assigns the values to an object of type student. ' That data is saved into a text file. Imports System.IO Public Class frmStudentData Dim objStudent As Student ' objStudent is an object of class Student Dim valid As Boolean = False Sub GetData(objStudent As Student) ' Assigns values from the form to the instance of Student valid = False ' initialize valid in case of prior use objStudent.LastName = txtLastName.Text objStudent.FirstName = txtFirstName.Text objStudent.StudentID = txtIdNumber.Text Try objStudent.TestAverage = CDbl(txtTestAverage.Text) If (CDbl(txtTestAverage.Text) <= 100) And (CDbl(txtTestAverage.Text) >= 0) Then valid = True Else MessageBox.Show("Text Average must be a number between 0 and 100.") txtTestAverage.Text = "" End If Catch ex As Exception MessageBox.Show("Please enter a number between 0 and 100 for Test Average.") End Try End Sub Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click ' Obtain input from user, assign it to the Student object and write it to a file. If (txtLastName.Text = "") Or (txtFirstName.Text = "") Or (txtIdNumber.Text = "") Or (txtTestAverage.Text = "") Then MessageBox.Show("One or more fields is blank. Please try again.") Else objStudent = New Student() ' create an instance of Student GetData(objStudent) If valid Then lblGrade.Text = objStudent.TestGrade SaveRecord() MessageBox.Show("Record has been saved.") ClearForm() End If End If End Sub Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click ' Ends the program when the user clicks on the Exit button End End Sub Sub SaveRecord() ' Save the student information to a text file Dim sw As StreamWriter = File.AppendText("Students.txt") sw.WriteLine(objStudent.LastName) sw.WriteLine(objStudent.FirstName) sw.WriteLine(objStudent.StudentID) sw.WriteLine(CStr(objStudent.TestAverage)) sw.WriteLine(objStudent.TestGrade) sw.Close() End Sub Sub ClearForm() ' Clears the form after the input has been saved txtLastName.Text = "" txtFirstName.Text = "" txtIdNumber.Text = "" txtTestAverage.Text = "" lblGrade.Text = "" End Sub End Class
Write the Code for Student class:
' Project: Student Data ' Description: This is the code for the Student class Public Class Student Private m_LastName As String Private m_FirstName As String Private m_StudentID As String Private m_TestAverage As Double Public Sub New() m_LastName = "" m_FirstName = "" m_StudentID = "" m_TestAverage = 0.0 End Sub Public Property LastName As String Get Return m_LastName End Get Set(value As String) m_LastName = value End Set End Property Public Property FirstName As String Get Return m_FirstName End Get Set(value As String) m_FirstName = value End Set End Property Public Property StudentID As String Get Return m_StudentID End Get Set(value As String) m_StudentID = value End Set End Property Public Property TestAverage As Double Get Return m_TestAverage End Get Set(value As Double) m_TestAverage = value End Set End Property ReadOnly Property TestGrade() As String Get Dim grade As String Select Case m_TestAverage Case Is >= 90 grade = "A" Case Is >= 80 grade = "B" Case Is >= 70 grade = "C" Case Is >= 60 grade = "D" Case Else grade = "F" End Select Return grade End Get End Property End Class