Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15522

What's wrong with this oop program.

$
0
0
Hi guys,

I wanted to write a class about integers but I can't seem to get to think in oop. I think I'm just writing procedural code
but the only difference is that I drop the functions in a class.
The second is I need to implement the array control code in the class and in the test program.
So it's not slik an neat solution primeraly because I can't use the print function inside a class
Also isnt there something neather to get an array back from a method to the test program?

After some searching on the internet. The way to good understand oop programming is to read a book about refactoring.
If so can one of you guys recommend me a good book. Im now using vb6 how to program from deitel.

class CIntegerSet

Code:

Option Explicit

Dim twist As New CIntegerSet
Private myarray(101) As Boolean
Private newarray(101) As Boolean

Public Function insertElement(ByVal i As Integer)
    If i >= 0 And i < UBound(myarray) Then
        myarray(i) = 1
    End If
End Function

Public Property Get givenewArray() As Boolean()
Dim i As Integer
    For i = 0 To UBound(newarray)
        If myarray(i) Then
            newarray(i) = True
        End If
        givenewArray = newarray()
    Next i
End Property
Public Function deleteelement(ByVal i As Integer)
    If i >= 0 And i < UBound(myarray) Then
        newarray(i) = False
    End If
End Function


Public Function giveArray() As Boolean()
        giveArray = myarray()
End Function



Public Property Get isSet(ByVal i As Integer) As Boolean

    If i >= 0 And i < UBound(myarray) Then
        If myarray(i) = True Then
            isSet = True
        End If
    End If
End Property

Public Function isEmpty(ByVal i As Integer) As Boolean()
    For i = 0 To UBound(myarray)
        If isSet(i) Then
            isEmpty = False
        End If
    Next i
End Function

Public Function union(ByVal i As Integer) As Boolean
    If i >= 0 And i < UBound(myarray) Then
        If myarray(i) = True Then
            newarray(i) = True
            union = newarray(i)
        End If
    End If
End Function

Public Function intersection(ByVal i As Integer) As Boolean
    If i >= 0 And i < UBound(myarray) Then
            intersection = myarray(i)
    End If
End Function

Public Function tostring(ByVal i As Integer) As String
    For i = 0 To UBound(myarray)
        myarray(i) = True
        tostring = i
    Next i
End Function

Public Function isequalto(ByVal i As Integer) As Boolean
    For i = 0 To UBound(myarray)
        myarray(i) = True
        isequalto = myarray(i)
    Next i
End Function

TestIntegerSet

Code:

Dim even As New CIntegerSet
Dim odd As New CIntegerSet
Dim calc As New CIntegerSet
Dim i As Integer
Dim myarray() As Boolean
Dim myarray2() As Boolean
Dim newarray() As Boolean

Private Sub Form_Load()
Me.Show

For i = 0 To 10
    calc.insertElement (i)
    If i Mod 2 = 0 Then
        even.insertElement (i)
    Else
        odd.insertElement (i)
    End If
Next i

myarray2() = odd.giveArray
Print "smallodds: "

    Print "[Set:";

    For i = 0 To UBound(myarray2)
        If odd.isSet(i) = True Then
            Print " "; i;
        End If
    Next i
    Print "]"
myarray() = even.giveArray
Print "smallevens: "
       
    Print "[Set:";
    For i = 0 To UBound(myarray)
        If even.isSet(i) = True Then
            Print " "; i;
        End If
    Next i
    Print "]"


   
newarray() = calc.givenewArray

Print "union"
    For i = 0 To UBound(newarray)
        If even.union(i) Or odd.union(i) Then
            Print i;
        End If
    Next i
    Print "]"
   
Print "intersection"
    For i = 0 To 10
        If even.intersection(i) = True Or odd.intersection(i) = False And even.intersection(i) = False Or odd.intersection(i) = True Or even.intersection(i) = False And odd.intersection(i) = False Then
'            myarray(i) = False
            isEmpty (myarray(i))
        ElseIf even.intersection(i) = True And odd.intersection(i) Then
            Print myarray(i);
        End If
    Next i
        If myarray(i) = False Then
            Print "---"
        End If

Print "deleteelement"
    For i = 0 To UBound(myarray)
        Print calc.deleteelement(i);
    Next i
Print "]"

Print "tostring"
    For i = 0 To 10
        If odd.tostring(i) Then
            Print i;
        End If
    Next i
Print "]"
Print "isequalto"
    For i = 0 To 10
        If even.isequalto(i) And odd.isequalto(i) Then
            Print i;
        End If
    Next i
Print "]"
End Sub

And here's the exercise question :

Create class CIntegerSet. Each object of the class can hold Integers in the range 0
through 100. A set is represented internally as an array of ones and zeros. Array element a(i) is 1
if integer i is in the set. Array element a(j) is 0 if integer j is not in the set.

Provide the following methodes: Method UnionOfIntegerSets creates a third set which
is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to 1 if that
element is 1 in either or both of the existing sets; otherwise, the element of the third set is set to 0).
Method IntersectionOfIntegerSets creates a third set which is the set-theoretic intersection
of two existing sets (i.e., an elementof the third set's array is set to 0 if that element is 0 in either
or both of the existing sets; otherwise, the element of the third set is set to 1). Method insertElement
inserts a new Integer k into a set (by setting a(k) to 1). Method DeleteElement deletes
Integer m (by setting a(m) to 0). Method ToString returns a String containing a set as a list
of numbers separted by spaces and prints only those elements that are present in the set or --- for
an empty set. Method IsEqualTo determines if two sets are equal. Write a program to test your
CIntegerSet class. Instantiate several CIntegerSet objects. Test that all your methods work properly

That was a long post hope you'll guys can bear with me :)

Viewing all articles
Browse latest Browse all 15522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>