Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual Basic 2012 How to program (Sixth Edition) (Enhanced Class Average App)The

ID: 3801337 • Letter: V

Question

Visual Basic 2012 How to program (Sixth Edition)

(Enhanced Class Average App)The class average application of fig.4.12allowed the user to enter a set of grades then calculate anddisplay the total of the grades and the class average. Create anapp that enables the user to select a file in which to store grades.The app should allow the user to write any number of grades intothat file and it should write one grade per line. Next, modify theapp ofFig. 4.12to allow the user to specify the location and nameof a ±le containing grades. The app should read the grades fromthe file, then display the total of the grades and the classaverage

Explanation / Answer


Enhanced_Class_Average

Public Class Grades

Public Property Grades As String 'Strings grades entered

'constructor initializes grades

Public Sub New(ByVal grade As String)

Grades = grade ' calls Grades property Set accessor

End Sub

Public Property Grades() As Char

'initialize array of grades

Dim grades() As Grades = {

New Grade(

'displays grades entered

Dim Grades =

outputGradesEntered.AppendText(String>Format("{0}Grades only:{0}", vbCrLf


part 2

Imports System.IO ' using classes from this namespace

Public Class CreateGradesFolder

Dim fileWriter As StreamWriter ' writes grades to text files

' Creat a new file in which grades can be stored

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

' ensure that any prior file is closed

Dim result As DialogResult ' Stores result of Save dialog

Dim fileName As String ' Name of file to save data

Dim NumberOfGradesEntered As String 'Number of Grades to saved file

'display dialog so uer can choose the name of the file to save

Using fileChooser As New SaveFileDialog()

result = fileChooser.ShowDialog()

fileName = fileChooser.FileName ' get specified file name

End Using ' automatic call to fileChooser.Dispose() occurs here

' If user did not click Cancel

If result <> Windows.Forms.DialogResult.Cancel Then

Try

' open or create file for writing

fileWriter = New StreamWriter(fileName, True)
'Enable Contols

CloseToolStripMenuItem.Enabled = True

addFileName.Enabled = True

GradesTextBox.Enabled = True

NumberOfGradesEntered = True
Catch ex As IOEception

MessageBox.Show("Error Opening File", Error", MessageBoxButoons.Ok, MessageBoxIcon.Error)

End Try

End If
End Sub ' New ToolStripMenuItem_Click

' Add A File

Private Sub addFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addFileButton.Click

'determines whether TextBox Grade field is empty

If GradeTextBox.Text <> String.Empty Then
'Try to store record to file

If GradeTextBox.Text <> String.Empty Then

Try

'gets file name

Dim FileName As Char

Convert.ToInt32(GradeTextBox.Text)

If GradeTextBox > 0 Then 'enter file name

'write file recor data to file separating filds by commas
fileWriter.WriteLine(accountNumber & "," &
GradesTextBox.Text

Convert.ToInt(NumberOfGradesEntered)
End If

Catch ex As Exception

End Try

End If

End If

End Sub

End Class