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 3 of 3

Thread: More classes

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

    Default More classes

    Hello!

    So i have this working code for a deck of cards. I can generate cards and print them out. However this is all in one class.
    I want to be able to make it so that i have one class with a name "Card" and anothe with the name "Deck" and use the Card class to get one or
    more cards from the Deck class and at the same be able to tell me the same things such as what cards, or card, that i have drawn and of how many
    cards that are left.

    But this, however, is getting to me... I don't know how to get this working.
    I mean i should just be able to make a couple of methods in the Deck class and then call them all from the Card class.
    And within each method the lines of code, that are below, i should just be able to use for each method.
    Did that but didn't work at all haha.


    Any ideas?


    This is the working code for the one class:
    	public static void main(String[] args) {
    		int Mnumber = 3; // Value for the amout of cards that are given out!
    		int counter = 0;
    		// Deck objectDeck = new Deck();
     
    		String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    		String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
     
    		for (int i = 0; i < ourDeck.length; i++) {
    			ourDeck[i] = i;
     
    		}
    		// objectDeck.flush(ourDeck);
     
    		for (int i = 0; i < ourDeck.length; i++) {
    			int index = (int) (Math.random() * ourDeck.length);
    			int temp = ourDeck[i]; 
    			ourDeck[i] = ourDeck[index];
    			ourDeck[index] = temp;
     
    		}
     
    		for (int i = 0; i < Mnumber; i++) {
    			String Suit = suits[ourDeck[i] / 13];
    			String Rank = rank[ourDeck[i] % 13];
    			counter++;
    			System.out.println("Card number is " + ourDeck[i] + " : " + Rank + " of " + Suit);
    			if (counter == Mnumber) {
    				System.out.print("And there are " + (52 - Mnumber) + " cards left!");
    			}
    		}
    	}

    Here is the Deck and Card class that i never got working:
    public class Deck {
    	public static int[] ourDeck = new int[52];
    	static String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
    	static String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
     
    	public int flush(int[] ourDeck) {
     
    		int temp = 0;
     
    		int i = 0;
    		for (i = 0; i < ourDeck.length; i++) {
    			ourDeck[i] = i;
     
    			for (int k = 0; k < ourDeck.length; k++) {
    				int index = (int) (Math.random() * ourDeck.length);
    				temp = ourDeck[k];
    				ourDeck[k] = ourDeck[index];
    				ourDeck[index] = temp;
     
    			}
     
    		}
    		return temp;
    	}
     
    	public int create(int i) {
    		flush(ourDeck);
    		int Mnumber = 0;
    		int counter = 0;
    		for (i = 0; i < Mnumber; i++) {
    			String Suit = suits[ourDeck[i] / 13];
    			String Rank = rank[ourDeck[i] % 13];
    			counter++;
    			System.out.println("Card number is " + ourDeck[i] + " : " + Rank + " of " + Suit);
    			if (counter == Mnumber) {
    				System.out.print("And there are " + (52 - Mnumber) + " cards left!");
    			}
    		}
    		return i;
     
    	}
    }

    public class Card {
    	public static int[] ourDeck = new int[52];
     
    	public static void main(String[] args) {
    		Deck objectDeck = new Deck();
    		objectDeck.flush(ourDeck);
     
    	}
     
    }

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

    Default Re: More classes

    You don't want to have your main entry point in the Card class. And your Card class should only have attributes associated with the card (e.g. suit, and rank) plus getters an setters if you desire.

    The Deck class should, as you say, create instances of the Card class and store them in a Deck instance.

    Regards,
    Jim

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

    Default Re: More classes

    Got it working now, thanks Jim!

Similar Threads

  1. No classes available
    By jacobgallipeau in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 20th, 2018, 05:57 AM
  2. Replies: 2
    Last Post: July 11th, 2014, 03:02 AM
  3. Replies: 1
    Last Post: July 11th, 2014, 02:01 AM
  4. Classes of classes? [Very beginner asking a question] [w/ Codes]
    By UhKindaAwkward in forum Object Oriented Programming
    Replies: 6
    Last Post: December 24th, 2013, 02:05 AM
  5. Help with creating classes and sub classes
    By Anotherspicybean in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2013, 10:42 PM