hi everyone, i need help with a project that i am doing.
i need to use inheritance and arrays to make a poker game, making a card class and a poker class that extends the card class
in the card class i would need to use arrays to make a deck, make suits and ranks, etc.
in the poker class i would need to check if the output cards are one pair, three of a kind, two pair, etc. and determine the winner
can anyone help me start off on how to use arrays in this project
before i used just regular statements to initialize a deck and do the other things
also can anyone give me suggestions on how to either not use if-else statements, greatly reduce the number of if-else statements used to determine the type of hand, or how to use arrays and inheritance to do this

i greatly appreciate any help and thank you so much for helping me
as of now im still working on this but i really need you help

i have my code so far
i forgot to add that the game must be two to four players so if anyone can help me with that i've tried to do that with the deck array
would it be better to put a multidimensional array for the two to four players and the cards in the base class(card) or derived class(poker)?
would i need a toString method and equals method, accessor, mutator methods, constuctors?

note: this is the base class
the code doesn't exactly work, if you have any suggestions on how to make it work i would greatly appreciate it
also if you have any suggestions for the derived class(poker class) i would really appreciate it
thank you for any help

import java.util.Random;

public class Card_Class 
{
	private int rank;
	private int suit;
	
	public void makeCards()
	{
		//Class invariant: Deck of cards, shuffles deck, names ranks and suits

		int [][] deck = new int[52][a];// deck of cards

		for (int i = 0; i < 52; i++)//make a new deck
			for(int j = 0; j < i.length; j++)
			{
				deck[i] = i;
			}
	
		Random generating = new Random();//create object of Random
		
		for (int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times 
			for(int j = 0; j < i.length; j++)
			{
				int randPos1 = generating.nextInt(52);//generate two numbers
				int randPos2 = generating.nextInt(52);

				int temp = deck[randPos1];//swap the two numbers to shuffle the deck
				deck[randPos1] = deck[randPos2];
				deck[randPos2] = temp;
			}
	
		int rank = 0;
		int suit = 0;
		String output = null;

		for (int i = 0; i < 5; i++) 
			for(int j = 0; j < i.length; j++)
			{
				rank = (deck[i] % 13) + 1; 
				suit = deck[i] / 13; 

				if (rank == 1)
					output = "Ace of ";
				else if (rank == 11)
					output = "Jack of ";
				else if (rank == 12)
					output = "Queen of ";
				else if (rank == 13)
					output = "King of ";
				else
					output = rank + " of ";
		
				if (suit == 0)
					output += "Spades";
				else if (suit == 1)
					output += "Clubs";
				else if (suit == 2)
					output += "Hearts";
				else
					output += "Diamonds";
				System.out.println(output);
			}
	}	
}

the game must be two to four players so if anyone can help me with that i've tried to do that with the deck array
would it be better to put a multidimensional array for the two to four players and the cards in the base class(card) or derived class(poker)?

I wouldn't recommend the multi-dimensional array route - it will lead to a big central data stucture shared by all the code for the whole application. Exactly the opposite of good Object design.
Most obvious O.O. solution would be to create a Player class, and a Game class. The Player class can hold whatever Cards the player holds, and also handle the player's calls, bets, current stake money etc. The Game class can represent the game as a whole, it can contain 2-4 instances of Player, and the logic for new game (call the Deck's shuffle method etc), who bets next (call the Player's bet() method etc), etc etc.

(This is a good example of one of the most simple Object Oriented Design methods. Read the problem description and underline any significant nouns (eg: card, deck, player, game) - these are possible candidates for defining your top-level classes.

im sorry i forgot to put that there is no betting, checking, etc.
also i can only have the two classes described so is there another way to make the two to four players thing work in my code
also if anyone can help me to fix my code and/or give suggestions on ways to do the rest of my program i would really appreciate that
thank you very much on any feedback

i think i made some progress

public class Poker_Class extends Card_Class
{
	//Class invariant: Defines types of hands(straight, two pair, etc.), determines number of players, swap method to interchange up to 3 cards
	//Prints out the winner and what each person has 
	
	int x = (cardRank%13)-(cardRank2%13);//determine straight
    			int ex = (cardRank2%13)-(cardRank3%13);
    			int exx = (cardRank3%13)-(cardRank%13);
    			
    			boolean straight = false;
    			boolean flush = false;
    			boolean royal = false;
    			
    			if((cardRank==10)&&(cardRank2==11)&&(cardRank3==12))
    				royal = true;
    			else if((cardRank==10)&&(cardRank2==12)&&(cardRank3==11))
    				royal = true;	
    			else if((cardRank==11)&&(cardRank2==10)&&(cardRank3==12))
    				royal = true;	
    			else if((cardRank==11)&&(cardRank2==12)&&(cardRank3==10))
    				royal = true;	
    			else if((cardRank==12)&&(cardRank2==11)&&(cardRank3==10))
    				royal = true;		
    			else if((cardRank==12)&&(cardRank2==10)&&(cardRank3==11))
    				royal = true;		
    			else if((straight==true) && (flush==true))//test for straight flush
    				System.out.println("Straight Flush ");
    			else if((royal==true) && (straight==true) && (flush==true))//test for royal straight flush
    				System.out.println("Royal Straight Flush");	
    			else if((Math.abs(x)==1)&&(Math.abs(ex)==1)&&(Math.abs(exx)==2))//test for straight
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((Math.abs(x)==1)&&(Math.abs(ex)==2)&&(Math.abs(exx)==1))
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((Math.abs(x)==2)&&(Math.abs(ex)==1)&&(Math.abs(exx)==1))
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((cardRank==0&&cardRank2==11&&cardRank3==12)||(cardRank==0&&cardRank2==12&&cardRank3==11))
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((cardRank==11&&cardRank2==0&&cardRank3==12)||(cardRank==11&&cardRank2==12&&cardRank3==0))
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((cardRank==12&&cardRank2==11&&cardRank3==0)||(cardRank==12&&cardRank2==0&&cardRank3==11))
    			{
    				straight = true;
    				System.out.println("Straight");
    			}
    			else if((cardSuit==0)&&(cardSuit2==0)&&(cardSuit3==0))//test for flush
    			{
  					flush = true;
    				System.out.println(" Flush ");
    			}
    			else if((cardSuit==1)&&(cardSuit2==1)&&(cardSuit3==1))
    			{
  					flush = true;
    				System.out.println(" Flush ");
    			}
    			else if((cardSuit==2)&&(cardSuit2==2)&&(cardSuit3==2))
    			{
  					flush = true;
    				System.out.println(" Flush ");
    			}
    			else if((cardSuit==3)&&(cardSuit2==3)&&(cardSuit3==3))
    			{
  					flush = true;
    				System.out.println(" Flush ");
    			}
    			else if((rank==rank2)&&(rank2==rank3))// test for three of a kind
    				System.out.println(" Three of a kind ");
    			else if((rank==rank2)||(rank2==rank3)||(rank==rank3))// test for one pair
    				System.out.println(" One pair ");
    			else// high card
    			{
    				System.out.println("High Card");
    			}
	
}

first is there any way of making this code better and in methods since i need to inherit from the card class
and how can i inherit the deck of cards, etc. from the card class to run my poker game
please i really need help urgently
thank you

im guessing because my teacher doesnt want to go through all the trouble of looking at multiple classes

but if it helps me finish this project i really dont care
i just need help it doesnt matter to me
thank you

oh yeah my new code for the poker class is really off
can anyone help with that

here is my updated code with the number of players in it
the problem is that the cards repeat if you dont know what i mean please compile my code and see what i mean
also if anyone has any suggestions for other things
please feel free to tell me
thank you

import java.util.Random;
import java.util.Scanner;

public class Card_Class
{
	private static int rank;
	private static int suit;
	
	public static void main(String[] args)
	{
		//Class invariant: Deck of cards, shuffles deck, names ranks and suits
		
		int [] deck = new int[52];// deck of cards

		for(int i = 0; i < 52; i++)//make a new deck
		{
			deck[i] = i;
		}
	
		Random generating = new Random();//create object of Random
		
		for(int i = 0; i < 100; i++)// shuffle the deck by swapping random two cards 100 times 
		{
			int randPos1 = generating.nextInt(52);//generate two numbers
			int randPos2 = generating.nextInt(52);

			int temp = deck[randPos1];//swap the two numbers to shuffle the deck
			deck[randPos1] = deck[randPos2];
			deck[randPos2] = temp;
		}
		
		int rank = 0;
		int suit = 0;
		String output = null;
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("Enter the number of players.");
		int p = keyboard.nextInt();
		
		System.out.println();
		
		if(p == 4)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 4; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 3)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 3; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else if(p == 2)
		{
			for (int i = 0; i < 5; i++) 
			{
				for(int j = 0; j < 2; j++)
				{
					rank = (deck[i] % 13) + 1; 
					suit = deck[i] / 13; 
					
					if (rank == 1)
						output = "Ace of ";
					else if (rank == 11)
						output = "Jack of ";
					else if (rank == 12)
						output = "Queen of ";
					else if (rank == 13)
						output = "King of ";
					else
						output = rank + " of ";
		
					if (suit == 0)
						output += "Spades";
					else if (suit == 1)
						output += "Clubs";
					else if (suit == 2)
						output += "Hearts";
					else
						output += "Diamonds";
					System.out.println(output);
				}
			}
		}
		else
			System.out.println("Error: only two to four players allowed.");
	}		
}

To get the code for the objects, think of how things are in real life. I can't say anything about what your teacher wants, but use as many classes as you need. You will have a card class, and deck and hand classes which are containers for cards. Then maybe a game and player class. I'm working on a similar poker hand evaluation thing and its very difficult to do right, so don't worry if you don't get it right away. :D

Regarding your code: what's up with the p variable? Why does the card output affected by it? It should be simpler, just a switch for the rank and suit, and combine the two strings. You could even put the suit and rank even name in the Card class.

well the cards repeat like so:

if 2 players (sample output):

2 of diamonds
2 of diamonds
Queen of Spades
Queen of Spades
6 of Hearts
6 of Hearts
8 of diamonds
8 of diamonds
7 of Hearts
7 of Hearts

i don't know how to fix this
any ideas

also, i made a card and deck class but i need help with the hand class i will try to find those classes and post them
thank you for the help

well the cards repeat like so:
...
i don't know how to fix this

You run exactly the same code for each player in your inner loop - the output depends only on i and not on j - so you get the same output twice.

ps
rather than have 3 blocks of almost-indentical code like this:, if(p == 3) ... for(int j = 0; j < 3; j++) you can have just one copy like this: for(int j = 0; j < p; j++) pps
JugglerDrummer speaks sense. Classes for Card, Deck, Hand etc (as I said before!), and yes, the code to format a card for display should be in the Card class.
If your assignment requires sub-classing you could create a generic CardGame class that implements shuffling, dealing, creating the right number of players, displaying hands etc, and a subclass Poker that extends it by adding poker-specific stuff like looking for straights and flushes.

This article has been dead for over six months. Start a new discussion instead.