Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores
Letter grade based on test scores Letter grade based on test scores
Letter grade based on test scores
Go Back  Xtreme Visual Basic Talk > > > Letter grade based on test scores


Reply
 
Thread Tools Display Modes
  #1  
Old 11-19-2010, 04:40 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default Letter grade based on test scores


I need someone with patience and can go through a solution with me from the Clearly Visual Basic book. I'm good with computers and can usually learn things relatively fast on my own time and in my own way, but I'm going to school for computer programming and, with certain things, I learn better with things explained to me rather than reading from a book. If someone has the time to go over this solution I'm doing and explain it to me in a way I'd have a better chance of understanding, I'd be truly grateful. I need a tutor, for lack of a better word.

The lab I'm doing is from the Clearly Visual Basic book and the instructions are:

Code an application that displays a letter grade based on the averages of three test scores. Code the application using a function to determine and return the letter grade. If the average is at least 90, the grade is A. If the average is at least 80 but less than 90, the grade is B. If the average is at least 70 but less than 80 the grade is C. If the average is at least 60 but less than 70, the grade is D. If the average is below 60, the grade is F.


The instructions I'm supposed to follow to code this program are:
b. You will be developing the code for the function on your own. Below the Public Class frmMain line, insert this function header:
Private Function CalculateGrade _
(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
ByVal testScore3 As Decimal) As String

c. In the function, declare variables for the average score and the letter grade. Calculate the average, use If/ElseIf statements to determine the letter grade, and return the letter grade.
d. Create a subprocedure for the Click event for the btnDisplay button. It should call the CalculateGrade function with three arguments (the three test scores from the text boxes), and display the returned value in the lblGrade box.


Now the code I have so far is this:
Code:
Public Class frmMain
    Private Function CalculateGrade _
            (ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
      ByVal testScore3 As Decimal) As String

        Dim AverageScore As String
        Dim strLetterGrade As String
        Dim intNum1 As Integer
        Dim intNum2 As Integer
        Dim intNum3 As Integer
        Dim intNum4 As Integer

        AverageScore = testScore1 + testScore2 + testScore3 / 3
        strLetterGrade = {"A", "B","C", "D", "F"}
        intNum1 = 90
        intNum2 = 80
        intNum3 = 70
        intNum4 = 60

        If AverageScore >= 90 Then
            Return strLetterGrade("A")
        Else
            If AverageScore >= 80 Then
                Return strLetterGrade("B")
            Else
                If AverageScore >= 70 Then
                    Return strLetterGrade("C")
                Else
                    If AverageScore >= 60 Then
                        Return strLetterGrade("D")
                    Else
                        If AverageScore < 60 Then
                            Return strLetterGrade("F")
                        End If
                    End If
                End If
            End If
        End If
        Return strLetterGrade
    End Function


    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtTest1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest1.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtTest2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest2.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtTest3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest3.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtTest1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest1.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub txtTest2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest2.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub txtTest3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest3.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

    End Sub
End Class

I'm probably wrong somewhere, but I'm begging someone to have patience with me and help me figure out where I'm wrong and why and to understand what I'm supposed to do in simpler terms so I can learn how to do this properly.

Last edited by Flyguy; 11-20-2010 at 12:06 AM. Reason: Put source code between [code] [/code] tags
Reply With Quote
  #2  
Old 11-19-2010, 06:23 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Just as a quick point, rather than using a series of nested If statements you could just use the If ... Else If ... Else syntax instead. e.g. the start of your If block would read more like
Code:
If AverageScore >= 90 Then
    Return strLetterGrade("A")
Else If AverageScore >= 80 Then
    Return strLetterGrade("B")

'....
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 11-19-2010, 06:38 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

Thank you, that helps. Do you have any other suggestions?
Reply With Quote
  #4  
Old 11-19-2010, 06:43 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

Also, in this portion of the code


AverageScore = testScore1 + testScore2 + testScore3 / 3
strLetterGrade = {"A", "B","C", "D", "F"}
intNum1 = 90
intNum2 = 80
intNum3 = 70
intNum4 = 60

It says that the first { isn't valid, that an expression is expected. Why is that, I thought those were the proper marks for doing something like that?
Reply With Quote
  #5  
Old 11-19-2010, 08:15 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

strLetterGrade is declared as a string, the syntax you are using is to initalise an array.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #6  
Old 11-20-2010, 06:58 AM
Banjo's Avatar
BanjoLetter grade based on test scores Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

Quote:
Originally Posted by PlausiblyDamp View Post
Just as a quick point, rather than using a series of nested If statements you could just use the If ... Else If ... Else syntax instead. e.g. the start of your If block would read more like
Code:
If AverageScore >= 90 Then
    Return strLetterGrade("A")
Else If AverageScore >= 80 Then
    Return strLetterGrade("B")

'....
In my opinion, this is a cleaner way:

Code:
Select Case AverageScore Case Is >90 strLetterGrade = "A" Case Is >80 strLetterGrade = "B" .....
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #7  
Old 11-20-2010, 01:57 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

Would that way also help with the strLetterGrade declared as a string or do I need to declare it another way besides strLetterGrade = {"A","B","C", "D", "F"}?
Reply With Quote
  #8  
Old 11-20-2010, 02:03 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

Also, I really need help with the Display Click procedure. I'm not exactly sure how to do it from these instructions

Create a subprocedure for the Click event for the btnDisplay button. It should call the CalculateGrade function with three arguments (the three test scores from the text boxes), and display the returned value in the lblGrade box.

What's the best way to start?
Reply With Quote
  #9  
Old 11-20-2010, 02:10 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Quote:
Originally Posted by Naitryn View Post
Would that way also help with the strLetterGrade declared as a string or do I need to declare it another way besides strLetterGrade = {"A","B","C", "D", "F"}?
As I mentioned before the syntax
Code:
strLetterGrade = {"A","B","C", "D", "F"}
is how you would instantiate an array and not how you assign a string. Then again you will be assigning a new value to the string later on and never actually referring to the array you are trying to assign to it anyway.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 11-20-2010, 02:48 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

So would it be better to say strLetterGrade = AverageScore or would that just make it display the score not grade?
Reply With Quote
  #11  
Old 11-20-2010, 03:13 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

That would make it display the average score, your If statement is assigning the grade to the string anyway - there is no need to assign anything to this variable before the If statement executes.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #12  
Old 11-20-2010, 03:19 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

The reason I ask is if I take the strLetterGrade = {"A","B","C", "D", "F"} out my If statements say that strLetterGrade is not declared. Should I leave it in and it'll give me the affect I want? Also, in my Display Click, I called the Calculate Grade but I'm not sure how to do so with the three arguments it wants.
Reply With Quote
  #13  
Old 11-20-2010, 03:44 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You should either be assigning the grade to the variable strLetterGrade to return it before the function completes or just returning the grade itself. There is no need to attempt to assign this array to a string though.

Either return the grade as a string or assign the grade to the string variable.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #14  
Old 11-20-2010, 03:50 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

How do I do that?
Reply With Quote
  #15  
Old 11-20-2010, 04:12 PM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Do what exactly?
  • You already know how to assign a value to a variable because you are doing it in other places in the code.
  • You already know how to return a value from a function because you are already doing it at the end of the function.
You already have the pieces in the code you posted - you seem to be getting hung up on the strLetterGrade = {"A","B","C", "D", "F"} part - just remove the = {"A","B","C", "D", "F"} bit and change the bits that look like Return strLetterGrade("A") to just return the grade itself.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #16  
Old 11-20-2010, 04:26 PM
Naitryn Naitryn is offline
Newcomer
 
Join Date: Nov 2010
Posts: 9
Default

I'm the type of person that I can somewhat understand something by seeing it...doesn't mean I quite know what it is I'm doing, that's why it helps me when someone explains it/shows me as I go. which is why I post bits of my code so I know what exactly I'm doing wrong because the terms have me stumped. That's why the subprocedure for the Click Display has me confused on what to do. I've probably done it, I'm just not sure what it is I've done because of the technical terms.

Last edited by Naitryn; 11-20-2010 at 04:31 PM.
Reply With Quote
  #17  
Old 11-21-2010, 09:26 AM
PlausiblyDamp's Avatar
PlausiblyDampLetter grade based on test scores PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

At the end of the function you have
Code:
 Return strLetterGrade
that is how you return a variable, I really don't know what else you need me to say. Similarly the code
Code:
intNum1 = 90
is how you assign a value to a variable, you already seem to know how to do these things so I have no idea what extra help you need with this part, the only thing I can assume is you have copied the code from somewhere and don't really understand these fundamental concepts.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #18  
Old 11-21-2010, 03:28 PM
ElderKnightLetter grade based on test scores ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,275
Default

Because the conversion is linear, you could get a letter grade this way . . .
Code:
strLetterGrade = Chr$(74 - Int(AverageScore/10))
Almost. The final step would be to change any letter "E" through "Z" to an "F".

You might want to suggest this to get extra credit.
__________________
-- D.J.

I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores Letter grade based on test scores
Letter grade based on test scores
Letter grade based on test scores
 
Letter grade based on test scores
Letter grade based on test scores
 
-->