Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Enumeration

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Enumeration

    Hello.


    I have this Card class that prints out some random cards that the user can choose and another classed called Deck that does all the heavier work, however the assignment says to do i "enum" or enumeration for all the enumerations in both classes and i feel lost because i am not sure if i am already doing it.

    Both classes together is basically a deck of cards that you take some cards from and it will pick some random cards.

    What do you guys think?

    Main :

    import java.util.*;
     
    public class Card {
     
    	static String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    	static String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
     
    	public static final int[] ourDeck = new int[52];
     
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
     
    		Deck objectDeck = new Deck();
    		objectDeck.stack(ourDeck);
    		objectDeck.flush(ourDeck);
    		System.out.print("Please write in the number of cards that you would like to get : ");
    		int alpha = in.nextInt();
    		objectDeck.create(alpha, ourDeck, suits, rank);
    		in.close();
     
    	}
    }


    public class Deck {
     
    	public int stack(int[] ourDeck) {
    		int i = 0;
    		for (i = 0; i < ourDeck.length; i++) {
    			ourDeck[i] = i;
    		}
    		return i;
     
    	}
     
    	public int flush(int[] ourDeck) {
    		int index = 0;
     
    		for (int i = 0; i < ourDeck.length; i++) {
    			ourDeck[i] = i;
     
    			for (int k = 0; k < ourDeck.length; k++) {
    				index = (int) (Math.random() * ourDeck.length);
    				int temp = ourDeck[k];
    				ourDeck[k] = ourDeck[index];
    				ourDeck[index] = temp;
    			}
    		}
    		return ourDeck[index];
    	}
     
    	public int create(int Mnumber, int[] k, String[] suits, String[] rank) {
     
    		int counter = 0;
    		for (int i = 0; i < Mnumber; i++) {
    			String Suit = suits[k[i] / 13];
    			String Rank = rank[k[i] % 13];
    			counter++;
    			System.out.println("Card number is " + k[i] + " : " + Rank + " of " + Suit);
    			if (counter == Mnumber) {
    				System.out.print("And there are " + (52 - Mnumber) + " cards left!");
    			}
    		}
    		return counter;
     
    	}
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Enumeration

    There are enum's and then there is also the Enumeration interface which is a first generation version of an Iterator. It would be helpful if you would post the exact wording of the assignment(s) so we can see for ourselves what the instructor is asking you to do.

    Regards,
    Jim

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,467
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Enumeration

    What int values are the methods supposed to return? It looks like they would always return the same values.

    Why doesn't the code that calls the methods that return a value, save the returned value? If the returned values are not used, why bother returning anything?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Enumeration

    Quote Originally Posted by jim829 View Post
    There are enum's and then there is also the Enumeration interface which is a first generation version of an Iterator. It would be helpful if you would post the exact wording of the assignment(s) so we can see for ourselves what the instructor is asking you to do.

    Regards,
    Jim

    Create a class Card, representing a playing card in an ordinary card deck with 52 cards. A card has a suite (4 different) and a rank (13 different). Write a class Deck initially containing 52 different objects of the class Card. The class Deck should contain methods for shuffling the deck, deal a card and telling how many cards are still in the deck. Note that it should only be possible to shuffle a deck if it contains 52 cards. (Information at Wikipedia about card decks and card games.)

    Also write a program PlayCardsMain, creating a card deck and dealing some cards, telling the number of remaining cards and which cards that have been dealt.

    Hint: Use enumeration types.


    I already have a class called DeckMain that does everything within itself.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Enumeration

    Enumeration types, or enums are a special class that is used to declare constants. It has default methods to access those constants but can also have other methods too. I recommend you check the tutorials link in my signature below to learn more about them. My take on the assignment was that you could use enums if you want but they aren't required.

    Regards,
    Jim

  6. #6
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Enumeration

    Quote Originally Posted by jim829 View Post
    Enumeration types, or enums are a special class that is used to declare constants. It has default methods to access those constants but can also have other methods too. I recommend you check the tutorials link in my signature below to learn more about them. My take on the assignment was that you could use enums if you want but they aren't required.

    Regards,
    Jim
    I will check it out!
    Thanks jim.

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Enumeration

    Your Deck class needs some help.

    First, the deck class should declare it's own storage for the cards. You could also use a constructor to initialize the deck. You don't need a create method as that is what you do when you create an instance. And your flush routine is really a shuffle routine (at least it appears to be). And you don't need to pass the array as the storage, as I said before, should be in the Deck class. But you don't need to return the internal storage of the Deck. Just use it via methods.

    You should probably have some way of retrieving a card until the deck is exhausted. An interator would work or just a simple pick or deal method with a counter to keep track of dealt cards would work.

    Remember that when you declare a class, the methods describe what you would normally do using an instance of the class in real life. So with a Deck of cards you would:

    shuffle - randomize the cards
    deal - take the cards off the top, one at a time
    reinitialize - gather up the cards from the players and reshuffle them. (note - you could also just create a new instance but in real life you don't open up a new pack of cards for every deal you just gather up and shuffle). But creating a new instance wouldn't be incorrect.

    You could add other operations as you might see fit.

    Regards,
    Jim
    Last edited by jim829; December 20th, 2018 at 08:54 AM.

Similar Threads

  1. Trouble with using Enumeration and Inheritance in Abstract Classes
    By ws22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:49 PM
  2. I need help with Enumeration in hashtable
    By jaouharia1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2012, 08:17 PM
  3. problem with enumeration
    By exens in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 18th, 2012, 01:51 PM