Static Variables for Grade Counting Program [modified] - SOLVED
-
My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...
Public Class Form1
Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click Static intA As Integer = 0 intA = intA + 1 lblGradea.Text = intA End Sub Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click Static intB As Integer = 0 intB = intB + 1 lblGradeb.Text = intB End Sub Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click Static intC As Integer = 0 intC = intC + 1 lblGradec.Text = intC End Sub Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click Static intD As Integer = 0 intD = intD + 1 lblGraded.Text = intD End Sub Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click Static intF As Integer = 0 intF = intF + 1 lblGradef.Text = intF End Sub
End Class
I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D
modified on Tuesday, March 30, 2010 10:18 AM
-
My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...
Public Class Form1
Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click Static intA As Integer = 0 intA = intA + 1 lblGradea.Text = intA End Sub Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click Static intB As Integer = 0 intB = intB + 1 lblGradeb.Text = intB End Sub Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click Static intC As Integer = 0 intC = intC + 1 lblGradec.Text = intC End Sub Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click Static intD As Integer = 0 intD = intD + 1 lblGraded.Text = intD End Sub Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click Static intF As Integer = 0 intF = intF + 1 lblGradef.Text = intF End Sub
End Class
I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D
modified on Tuesday, March 30, 2010 10:18 AM
-
My teacher want's me to design a program that does this. "Design a program that has five buttons, one for each grade. Each time you press a button it increments that grade by one. At the same time it updates a label that displays the current percent of students that passed the exam (grade higher than F)." This is what I have so far...
Public Class Form1
Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click Static intA As Integer = 0 intA = intA + 1 lblGradea.Text = intA End Sub Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click Static intB As Integer = 0 intB = intB + 1 lblGradeb.Text = intB End Sub Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click Static intC As Integer = 0 intC = intC + 1 lblGradec.Text = intC End Sub Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click Static intD As Integer = 0 intD = intD + 1 lblGraded.Text = intD End Sub Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click Static intF As Integer = 0 intF = intF + 1 lblGradef.Text = intF End Sub
End Class
I need help on that part that displays what percentage of students are passing. I assume you have to add together the toal number of grades and then divide by the total number above F and display that in a label. But I cant seem to figure that out...Thanks :-D
modified on Tuesday, March 30, 2010 10:18 AM
I don't normally do student's homework, but seeing as you have attempted to solve the problem here is some pointers... Remove the lines that declare the
STATIC integer
and make thePRIVATE member
variables e.g.Public Class Form1
Private intA As Integer
Private intB as Integer
Private intC as Integer
Private intD as Integer
Private intF as IntegerIn the
Form_Load
Event handler, initiate the starting values e.g.intA = 0
intB = 0Now all you need is a
Function
to return the averages.Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
I don't normally do student's homework, but seeing as you have attempted to solve the problem here is some pointers... Remove the lines that declare the
STATIC integer
and make thePRIVATE member
variables e.g.Public Class Form1
Private intA As Integer
Private intB as Integer
Private intC as Integer
Private intD as Integer
Private intF as IntegerIn the
Form_Load
Event handler, initiate the starting values e.g.intA = 0
intB = 0Now all you need is a
Function
to return the averages.Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
I figured it out :) Thank you both for steering me in the right direction. Not sure if its the most effcient way of making the program but it does what it's supposed to. Here is the final code...
'Adam Wike
'Computer Programming 1
'Grade Counter
'Created on March 28, 2010
'Last Modified on March 30, 2010Public Class Form1
'Declared all variables Private intA As Integer Private intB As Integer Private intC As Integer Private intD As Integer Private intF As Integer Private intE As Integer 'Sets the variables to 0 Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load intA = 0 intB = 0 intC = 0 intD = 0 intF = 0 End Sub 'Sets up the counter and calculates the percentage for each button Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click intA = intA + 1 lblGradea.Text = intA intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click intB = intB + 1 lblGradeb.Text = intB intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click intC = intC + 1 lblGradec.Text = intC intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click intD = intD + 1 lblGraded.Text = intD intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click intF = intF + 1 lblGradef.Text = intF intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub
End Class
-
I figured it out :) Thank you both for steering me in the right direction. Not sure if its the most effcient way of making the program but it does what it's supposed to. Here is the final code...
'Adam Wike
'Computer Programming 1
'Grade Counter
'Created on March 28, 2010
'Last Modified on March 30, 2010Public Class Form1
'Declared all variables Private intA As Integer Private intB As Integer Private intC As Integer Private intD As Integer Private intF As Integer Private intE As Integer 'Sets the variables to 0 Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load intA = 0 intB = 0 intC = 0 intD = 0 intF = 0 End Sub 'Sets up the counter and calculates the percentage for each button Private Sub btnA\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click intA = intA + 1 lblGradea.Text = intA intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnB\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click intB = intB + 1 lblGradeb.Text = intB intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnC\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click intC = intC + 1 lblGradec.Text = intC intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnD\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click intD = intD + 1 lblGraded.Text = intD intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub Private Sub btnF\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click intF = intF + 1 lblGradef.Text = intF intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) \* 100 lblPassing.Text = Math.Round(intE, 2) & "%" End Sub
End Class
adamwike wrote:
intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100 lblPassing.Text = Math.Round(intE, 2) & "%"
The above should not be repeated in every button click event. Consider making in a sub-routine for example :
Private Sub CalcIntE()
intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100
lblPassing.Text = Math.Round(intE, 2) & "%"
End SubThen replace the necessary code in each button click event with
CalcIntE
for examplePrivate Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnA.Click
intA = intA + 1
lblGradea.Text = intA
CalcIntE
End SubSteve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
adamwike wrote:
intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100 lblPassing.Text = Math.Round(intE, 2) & "%"
The above should not be repeated in every button click event. Consider making in a sub-routine for example :
Private Sub CalcIntE()
intE = ((intA + intB + intC + intD) / (intA + intB + intC + intD + intF)) * 100
lblPassing.Text = Math.Round(intE, 2) & "%"
End SubThen replace the necessary code in each button click event with
CalcIntE
for examplePrivate Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnA.Click
intA = intA + 1
lblGradea.Text = intA
CalcIntE
End SubSteve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.
-
Your right, that is a lot less confusing and makes the program look less cluttered. Thanks!
adamwike wrote:
that is a lot less confusing and makes the program look less cluttered
Not only that, but more importantly, it is more maintainable.
Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.