The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help with homework

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Need help with homework Posted: Nov 16, 2003 11:11 PM
Reply to this message Reply
Advertisement
Since vb.net syntax is so similar to java and this is only a console app a figuared this would be ok.
So this is just a simple deck of cards class every function except for shuffle works. In my driver I display the deck then shuffle and display again, but nothing changes. Am I referencing wrong???

Public Class deckOfCards
 
    Dim topIndex As Integer = 0
    Dim cards(51) As card
 
    Public Sub New()
        Dim index As Integer = 0
 
        Dim value As Integer
        Dim suit As Integer
 
        For value = 1 To 13
            For suit = 1 To 4
                cards(index) = New card(value, suit)
                index += 1
            Next
        Next
    End Sub
 
    Function displayDeck()
        Dim index As Integer
        For index = topIndex To cards.Length - 1
            Console.WriteLine(cards(index).ToString())
        Next
    End Function
 
    Function shuffle(ByVal numtimes As Integer)
        Dim t As Integer
        Dim n As Integer
        Dim x As Integer
        Dim j As Integer
        Dim tempcard As card
 
        For t = 0 To t <= numtimes
            j = cards.Length - 1
            For n = 0 To n < j
                x = Int((j * Rnd())) 'Generates a random number between j and 0
                tempcard = cards(n)
                cards(n) = cards(x)
                cards(x) = tempcard
            Next
        Next
    End Function
 
    Function getTopCard() As card 'Just returns the top card in the deck.
        Return cards(topIndex = topIndex + 1)
 
    End Function
    Function isFull() As Boolean
        If topIndex.Equals(0) Then
            Return True
        Else
            Return False
        End If
    End Function
 
    Function isEmpty() As Boolean
        If (topIndex.Equals(51)) Then
            Return True
        Else
            Return False
        End If
 
    End Function
 
    Overrides Function toString() As String
        Dim s As String
        If (isFull()) Then
            s = "Deck of cards is full!"
        ElseIf (isEmpty()) Then
            s = "The deck is empty!"
        End If
 
        Return s
    End Function
 
End Class
 


here is the driver.
Module deckDriver
    Sub main()
 
        Dim ndeck = New deckOfCards
        ndeck.displayDeck()
        ndeck.shuffle(2)
        Console.WriteLine("*************Here is the orginal deck shuffled*************")
        ndeck.displayDeck()
        Console.WriteLine("*************TopCard*************")
        Console.WriteLine(ndeck.getTopCard())
        Console.WriteLine("*************Deck shuffled without topCard*************")
        ndeck.shuffle(4)
        ndeck.displayDeck()
        Console.WriteLine("-----------------------------------")
        Console.WriteLine(ndeck.ToString())
 
        Console.ReadLine()
 
    End Sub
End Module

Topic: Trouble with protected class access Previous Topic   Next Topic Topic: Need help with these questions....

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use