Re: How to distinguish odd / even numbers and sumthem
- From: "Stephany Young" <noone@localhost>
- Date: Thu, 25 Jan 2007 16:13:25 +1300
Yes, you're right. The final Return False should be Return True.
Yes, it should fail (return False) if any character cannot be parsed as in
integer. If you supply a value such as 123A456 then that is not a number and
therfore is out of context for the method.
Remember that the OP stated that the user was entering a 'number'.
Of course one could argue that such a function should handle any string that
might represent a number including decimal points, thousands separators,
currency symbols, signs, etc., and, if one was so inclined could easily
expand the functionality to be more 'fuzzy'.
My preference, in a class, is to group the private members so that they are
all in one place.
"Tom Leylan" <tleylan@xxxxxxxxxx> wrote in message
news:%23f1negCQHHA.4364@xxxxxxxxxxxxxxxxxxxxxxx
I see I forgot to edit my function call... in my test routines I named it
Test7() but I renamed the thing as I posted. And yes I like it as a class.
Is something up with the TotalOddsAndEvens method? It's returning False
all the time and I'm not sure that a non-digit in the string should make
it fail. And since the class is named OddsAndEvens I might suggest
"Total()" is a good enough name for the method.
And this is why I like these simple examples. Note how you grouped the
private variables at the top of the class? It is a very common style and
used in many books, probably in the MS docs as well. I long ago stopped
doing this and opted for a style which came out of Delphi as I recall.
I declare the private instance variable and then the property. That way I
can see things on a "per property" basis and easily determine whether I
have remembered to generate the property, if it is public/private and
whether it is read-only. It's only a style but I've grown to love it...
so it would look like this:
Private m_odds As Int32
Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property
Private m_evens As Int32
Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property
"Stephany Young" <noone@localhost> wrote in message
news:uRIHpSCQHHA.404@xxxxxxxxxxxxxxxxxxxxxxx
You're really having fun today Tom! ;)
Shouldn't it be:
' upon some click event or something
Test7(txtInput.Text.Trim())
But now we're really cooking with gas:
Dim oae As New OddsAndEvens
If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then
lblEven.Text = "Sum of even numbers is: " &
oae.TotalOfEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString()
Else
MessageBox.Show("Supplied value is not a valid value")
End If
Public Class OddsAndEvens
Private m_odds As Int32
Private m_evens As Int32
Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property
Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property
Public Function TotalOddsAndEvens(value As String) As Boolean
m_odds = 0
m_evens = 0
Dim nValue As Int32 = 0
For i As Integer = 0 To value.Length - 1
If Int32.TryParse(sInput.Substring(i, 1), nValue)
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
Else
Return False
End If
Next
Return False
End Sub
Public Shared Function IsEven(ByVal num As Int32) As Boolean
Return (num Mod 2 = 0)
End Function
Public Shared Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function
End Class
"Tom Leylan" <tleylan@xxxxxxxxxx> wrote in message
news:er3jRBCQHHA.3960@xxxxxxxxxxxxxxxxxxxxxxx
I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.
I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is
the extra code needed to initialize them (outside of the routine) and to
pass them by reference just obfuscates things (since (I doubt) this is
going to be part of a reusable library.)
The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".
I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.
So here is mine:
' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)
Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0
Dim bIsDigit As Boolean
Dim nValue As Int32 = 0
For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next
lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()
End Sub
Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function
Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function
"Stephany Young" <noone@localhost> wrote in message
news:usEBuRBQHHA.992@xxxxxxxxxxxxxxxxxxxxxxx
Dim intTotEvens As Integer
Dim intTotOdds As Integer
Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578
For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next
Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()
Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()
Does that at least get me a C+? :)
This should grade higher:
Dim _evens As Integer
Dim _odds As Integer
For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next
Label1.Text = "Sum of odd numbers is: " & _odds.ToString()
Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tleylan@xxxxxxxxxx> wrote in message
news:%23vG7tDBQHHA.1152@xxxxxxxxxxxxxxxxxxxxxxx
Well that will get him a C- :-)
"Lost" <lostagain@xxxxxxxxxxxxxx> wrote in message
news:45b7ea11$0$97216$892e7fe2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"Ron" <pts4560@xxxxxxxxx> wrote:
I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...
the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10
any ideas?
thanks
Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer
intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) > 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)
--
Modo vincis, modo peris
.
- Follow-Ups:
- Re: How to distinguish odd / even numbers and sumthem
- From: Tom Leylan
- Re: How to distinguish odd / even numbers and sumthem
- From: Ron
- Re: How to distinguish odd / even numbers and sumthem
- References:
- Re: How to distinguish odd / even numbers and sumthem
- From: Tom Leylan
- Re: How to distinguish odd / even numbers and sumthem
- From: Stephany Young
- Re: How to distinguish odd / even numbers and sumthem
- From: Tom Leylan
- Re: How to distinguish odd / even numbers and sumthem
- From: Stephany Young
- Re: How to distinguish odd / even numbers and sumthem
- From: Tom Leylan
- Re: How to distinguish odd / even numbers and sumthem
- Prev by Date: Notify Icon problem
- Next by Date: Re: PDF creation from vb.NET
- Previous by thread: Re: How to distinguish odd / even numbers and sumthem
- Next by thread: Re: How to distinguish odd / even numbers and sumthem
- Index(es):
Relevant Pages
|