Results 1 to 8 of 8
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default java inheritance problem

    Hello all again..

    i am working an a CrazyEights project...

    i just want to ask how can i pass informations from super class to Two subClasses but i need to have the same informations and in my project i am talking about 2 Objects one Deck Object which will keep the Cards of the game and the Table Object which will keep the cards in the table and this informations must be the same for each subclasses.. to give a better image of the problem i will give the program below part by part

    first we have a Card Class whice create cards objects and has some methods for each object info :

    Java Code:
    class Card{
    	private String number ;
    	private String color ;
    	
    	public Card(String number , String color ){
    		this.number = number ;
    		this.color = color ;
    	}
    	
    	public String NumberValue(){
    		return number ;
    	}
    	
    	public String ColorValue(){
    		return color;
    	}
    }
    i have a Deck Class in which i create all cards type Card Object and i have two methods, one which allow player or computer to take onother card from deck and two, a CheckCards method which check if cards in deck are less than 10 and the take the TableCards and take them back to Deck .. :

    Java Code:
    class Deck{
    	private int cardsNumber = 52 ;
    	private Random randomGenerator = new Random();
    	private ArrayList<Card> CardArray = new ArrayList<Card>();
    	private String [] ColorArray = {"Spade","Club","Heart","Diamond"};
    	
    	public Deck(){
    		for(int j = 0 ; j < 4 ; j ++){
    			Card element = new Card("A",ColorArray[j]);
    			CardArray.add(element);
    			for(int i = 1 ; i < 10 ; i ++){
    				element = new Card((i+1)+"",ColorArray[j]);
    				CardArray.add(element);
    			}
    			element = new Card("J",ColorArray[j]);
    			CardArray.add(element);
    			element = new Card("Q",ColorArray[j]);
    			CardArray.add(element);
    			element = new Card("K",ColorArray[j]);
    			CardArray.add(element);
    		}
    	}
    	
    	public Card getCard(){
    		int randomNumber = randomGenerator.nextInt(CardArray.size());
    		Card CardObj = CardArray.get(randomNumber);
    		CardArray.remove(CardArray.get(randomNumber));
    		cardsNumber -=1; 
    		System.out.println("                                    " + cardsNumber);  // this line will print the number of card left each time somone take a card 
    		return CardObj;
    	}
    	
    	public void CheckCards(Table element){
    		if (cardsNumber < 10){
    			System.out.println("Too low cards in deck..Table Cards will go back to deck");
    			ArrayList<Card> TableCards = new ArrayList<Card>();
    			TableCards = element.getTableCard();
    			Card Top = TableCards.get(TableCards.size()-1); 
    			for(int i=0 ; i < TableCards.size()-1 ; i++){
    				CardArray.add(TableCards.get(i));
    			}
    			cardsNumber = CardArray.size();
    			element.setTopCard(Top);
    		}
    	}
    }
    Then i have a Table Class which keep the info about the table card , about the top card in the table , the color and the number of this card :



    Java Code:
    class Table{
    	private ArrayList<Card> TableCards = new ArrayList<Card>();
    	private Card Top = null;
    	private String color ;
    	private String number ;
    	
    	public void setTopCard(Card element){
    		TableCards.clear();
    		TableCards.add(element) ;
    		Top = TableCards.get(TableCards.size()-1);
    	}
    	
    	public void NextCard(Card element){
    		
    		TableCards.add(element);
    		Top = TableCards.get(TableCards.size()-1);
    	}
    	
    	public String getColor(){
    		if (Top == null){
    			return null;
    		}else{
    			return Top.ColorValue();
    		}
    	}
    	
    	public String getNumber(){
    		if (Top == null){
    			return null;
    		}else{
    			return Top.NumberValue();
    		}
    	}
    	
    	public void setColor(String color){
    		Card element = new Card(Top.NumberValue() , color);
    		Top = element ;
    	}
    	
    	public ArrayList<Card> getTableCard(){
    		return TableCards;
    	}
    }


    now the project without the inheritance has a big Class Player which make the game of player and the game of computer
    this class i want to inherite in two subclasses for example i want to create a class GeneralPlayer and two subclasses ComputerPlayer and HumanPlayer

    this is the class without inheritance :


    Java Code:
    class Player{
    	private Deck DeckObj;
    	private ArrayList<Card> playerHand = new ArrayList<Card>();
    	private ArrayList<Card> computerHand = new ArrayList<Card>();
    	private Table TableObj = new Table();
    	private String [] ColorArray = {"Spade","Club","Heart","Diamond"};
    	
    	public Player(Deck element){
    		
    		DeckObj = element;
    		for(int i = 0 ; i < 5 ; i++){
    			playerHand.add(DeckObj.getCard());
    			computerHand.add(DeckObj.getCard());
    		}
    		TableObj.NextCard(DeckObj.getCard());
    	}
    	
    	public void takeCard(String turn){
    		DeckObj.CheckCards(TableObj);
    		if(turn == null){
    			computerHand.add(DeckObj.getCard());
    		}else{
    			boolean allow = true ;
    			for(int i = 0 ; i < playerHand.size() ; i ++){
    				if(playerHand.get(i).ColorValue().equals(TableObj.getColor()) || playerHand.get(i).NumberValue().equals(TableObj.getNumber()) || playerHand.get(i).NumberValue().equals("8")){
    					allow = false ;
    				}
    			}
    			if(allow){
    				playerHand.add(DeckObj.getCard());
    			}else{
    				System.out.println("You Can't take a Card when you already have to play.");
    			}
    		}
    	}
    	
    	public boolean PersonPlay(String userCardInput){
    		boolean flag = false ;
    		int pos = 0;
    		String color = "" ;
    		for(int i = 0 ; i < playerHand.size() ; i ++){
    			if (userCardInput.equalsIgnoreCase(playerHand.get(i).NumberValue() + " " + playerHand.get(i).ColorValue()) || userCardInput.equalsIgnoreCase(playerHand.get(i).ColorValue() + " " + playerHand.get(i).NumberValue())){
    				flag = true ;
    				pos = i ;
    				break ;
    			}	
    		}
    		if(!flag){
    			System.out.print("Check Spelling or make sure about your card choice and try again :");
    			return false ;
    		}else{
    			if(playerHand.get(pos).NumberValue().equals(TableObj.getNumber()) || playerHand.get(pos).ColorValue().equals(TableObj.getColor()) || playerHand.get(pos).NumberValue().equals("8")){
    				if(playerHand.get(pos).NumberValue().equals("8")){
    					Scanner input = new Scanner(System.in);
    					System.out.println("Available Colors = Spade,Club,Heart,Diamond ");
    					System.out.print("You play a 8 choose a color please : " ) ;
    					while(flag){
    						color = input.next();
    						for(int i = 0 ; i < 4 ; i ++ ){
    							if(color.equalsIgnoreCase(ColorArray[i])){
    								color = ColorArray[i];
    								flag = false ;
    								System.out.println("You Choose " + ColorArray[i] + " so this will be the new Color " );
    							}
    						}
    						if(flag){
    							System.out.print("Wrong color Try Again : ");
    						}
    					}
    					TableObj.NextCard(playerHand.get(pos));
    					TableObj.setColor(color);
    					playerHand.remove(playerHand.get(pos));
    					return true ;
    				}else{
    					TableObj.NextCard(playerHand.get(pos));
    					playerHand.remove(playerHand.get(pos));
    					return true ;
    				}
    			}else{
    				System.out.println("Card does not match with the table color or value.Try again :");
    				return false ;
    			}
    		}
    	}
    
    	public boolean ComputerPlay(){
    		boolean flag = false ;
    		int pos = 0;
    		int [] myArray = new int [4];
    		int eights = 0 ;
    		while(!flag){
    			for(int i = 0  ; i < computerHand.size();i++){
    				if(computerHand.get(i).ColorValue().equals(TableObj.getColor()) && !computerHand.get(i).NumberValue().equals("8")){
    					TableObj.NextCard(computerHand.get(i));
    					System.out.println("Computer play a " + computerHand.get(i).NumberValue() + " " + computerHand.get(i).ColorValue());
    					computerHand.remove(computerHand.get(i));
    					flag = true;
    					break;
    				}
    				for(int j = 0 ; j < 4; j++){
    					if(computerHand.get(i).ColorValue().equals(ColorArray[j])){
    						myArray[j] = myArray[j] + 1 ;
    						break;
    					}
    				}
    			}
    			if(flag){
    				break ;
    			}
    			
    			for(int i = 0  ; i < computerHand.size();i++){
    				if(computerHand.get(i).NumberValue().equals(TableObj.getNumber()) && !computerHand.get(i).NumberValue().equals("8")){
    					TableObj.NextCard(computerHand.get(i));
    					System.out.println("Computer play a " + computerHand.get(i).NumberValue() + " " + computerHand.get(i).ColorValue());
    					computerHand.remove(computerHand.get(i));
    					flag = true ;
    					break;
    				}
    				if(computerHand.get(i).NumberValue().equals("8")){
    					eights ++ ;
    					pos = i ;
    				}
    			}
    			
    			if(flag){
    				break ;
    			}
    			if(eights > 0){
    				System.out.println("Computer play a " + computerHand.get(pos).NumberValue() + " " + computerHand.get(pos).ColorValue());
    				TableObj.NextCard(computerHand.get(pos));
    				computerHand.remove(computerHand.get(pos));
    				int positionOfMax = 0;
    				int max = myArray[0] ;
    				for(int i = 0 ; i < myArray.length ; i ++){
    					if(max < myArray[i]){
    						max = myArray[i];
    						positionOfMax = i ;
    					}
    				}
    				System.out.println("And he choose the "+ColorArray[positionOfMax] + " color " );
    				TableObj.setColor(ColorArray[positionOfMax]);
    				flag = true ;
    			}
    			
    			if(!flag){
    				System.out.println("Computer take a card from deck !");
    				takeCard(null);
    			}
    			
    		}
    		return true ;
    	}
    	
    	public void CardsPrint(){
    		
    		System.out.print("You have : ");
    		for(int i = 0 ; i < playerHand.size()-1 ; i++){
    			System.out.print(playerHand.get(i).NumberValue() + " " + playerHand.get(i).ColorValue()+ ", " );
    		}
    		System.out.print(playerHand.get(playerHand.size()-1).NumberValue() + " " + playerHand.get(playerHand.size()-1).ColorValue());
    		System.out.println();
    		System.out.print("Top of cards in the table :");
    		System.out.println(TableObj.getNumber() + " " + TableObj.getColor());
    	}
    	
    	public String WinnerCheck(){
    		String winner;
    		if(playerHand.size() == 0){
    			return winner = "Player";
    		}else if(computerHand.size()==0){
    			return winner = "Computer";
    		}
    		else{
    			return winner = null ;
    		}
    	}
    }

    to rename this (Big) class in GeneralPlayer and cut some information and paste some in HumanPlayer and some in ComputerPlayer and only leave in GeneralPlayer the necessary information about the both of games (Human - Computer) and i make this :


    Java Code:
    class GeneralPlayer{
    	protected Table TableObj = new Table(); 
    	protected Deck DeckObj = new Deck();
    	protected ArrayList<Card> playerHand = new ArrayList<Card>();
    	protected ArrayList<Card> computerHand = new ArrayList<Card>();
    	protected String [] ColorArray = {"Spade","Club","Heart","Diamond"};
    	
    	public GeneralPlayer(){
    		for(int i = 0 ; i < 5 ; i++){
    			playerHand.add(DeckObj.getCard());
    			computerHand.add(DeckObj.getCard());
    		}
    		TableObj.NextCard(DeckObj.getCard());
    	}
    	
    	public String WinnerCheck(){
    		String winner;
    		if(playerHand.size() == 0){
    			return winner = "Player";
    		}else if(computerHand.size()==0){
    			return winner = "Computer";
    		}
    		else{
    			return winner = null ;
    		}
    	}
    }
    
    class ComputerPlayer extends GeneralPlayer {
    
    	public boolean ComputerPlay(){
    		
    		boolean flag = false ;
    		int pos = 0;
    		int [] myArray = new int [4];
    		int eights = 0 ;
    		while(!flag){
    			for(int i = 0  ; i < computerHand.size();i++){
    				if(computerHand.get(i).ColorValue().equals(TableObj.getColor()) && !computerHand.get(i).NumberValue().equals("8")){
    					TableObj.NextCard(computerHand.get(i));
    					System.out.println("Computer play a " + computerHand.get(i).NumberValue() + " " + computerHand.get(i).ColorValue());
    					computerHand.remove(computerHand.get(i));
    					flag = true;
    					break;
    				}
    				for(int j = 0 ; j < 4; j++){
    					if(computerHand.get(i).ColorValue().equals(ColorArray[j])){
    						myArray[j] = myArray[j] + 1 ;
    						break;
    					}
    				}
    			}
    			if(flag){
    				break ;
    			}
    			
    			for(int i = 0  ; i < computerHand.size();i++){
    				if(computerHand.get(i).NumberValue().equals(TableObj.getNumber()) && !computerHand.get(i).NumberValue().equals("8")){
    					TableObj.NextCard(computerHand.get(i));
    					System.out.println("Computer play a " + computerHand.get(i).NumberValue() + " " + computerHand.get(i).ColorValue());
    					computerHand.remove(computerHand.get(i));
    					flag = true ;
    					break;
    				}
    				if(computerHand.get(i).NumberValue().equals("8")){
    					eights ++ ;
    					pos = i ;
    				}
    			}
    			
    			if(flag){
    				break ;
    			}
    			if(eights > 0){
    				System.out.println("Computer play a " + computerHand.get(pos).NumberValue() + " " + computerHand.get(pos).ColorValue());
    				TableObj.NextCard(computerHand.get(pos));
    				computerHand.remove(computerHand.get(pos));
    				int positionOfMax = 0;
    				int max = myArray[0] ;
    				for(int i = 0 ; i < myArray.length ; i ++){
    					if(max < myArray[i]){
    						max = myArray[i];
    						positionOfMax = i ;
    					}
    				}
    				System.out.println("And he choose the "+ColorArray[positionOfMax] + " color " );
    				TableObj.setColor(ColorArray[positionOfMax]);
    				flag = true ;
    			}
    			
    			if(!flag){
    				System.out.println("Computer take a card from deck !");
    				takeCard();
    			}
    			
    		}
    		return true ;
    	}
    	
    	public void takeCard(){
    		DeckObj.CheckCards(TableObj);
    		computerHand.add(DeckObj.getCard());
    	}
    }
    
    class HumanPlayer extends GeneralPlayer {
    	
    	public boolean PersonPlay(String userCardInput){
    		boolean flag = false ;
    		int pos = 0;
    		String color = "" ;
    		for(int i = 0 ; i < playerHand.size() ; i ++){
    			if (userCardInput.equalsIgnoreCase(playerHand.get(i).NumberValue() + " " + playerHand.get(i).ColorValue()) || userCardInput.equalsIgnoreCase(playerHand.get(i).ColorValue() + " " + playerHand.get(i).NumberValue())){
    				flag = true ;
    				pos = i ;
    				break ;
    			}	
    		}
    		if(!flag){
    			System.out.print("Check Spelling or make sure about your card choice and try again :");
    			return false ;
    		}else{
    			if(playerHand.get(pos).NumberValue().equals(TableObj.getNumber()) || playerHand.get(pos).ColorValue().equals(TableObj.getColor()) || playerHand.get(pos).NumberValue().equals("8")){
    				if(playerHand.get(pos).NumberValue().equals("8")){
    					Scanner input = new Scanner(System.in);
    					System.out.println("Available Colors = Spade,Club,Heart,Diamond ");
    					System.out.print("You play a 8 choose a color please : " ) ;
    					while(flag){
    						color = input.next();
    						for(int i = 0 ; i < 4 ; i ++ ){
    							if(color.equalsIgnoreCase(ColorArray[i])){
    								color = ColorArray[i];
    								flag = false ;
    								System.out.println("You Choose " + ColorArray[i] + " so this will be the new Color " );
    							}
    						}
    						if(flag){
    							System.out.print("Wrong color Try Again : ");
    						}
    					}
    					TableObj.NextCard(playerHand.get(pos));
    					TableObj.setColor(color);
    					playerHand.remove(playerHand.get(pos));
    					return true ;
    				}else{
    					TableObj.NextCard(playerHand.get(pos));
    					playerHand.remove(playerHand.get(pos));
    					return true ;
    				}
    			}else{
    				System.out.println("Card does not match with the table color or value.Try again :");
    				return false ;
    			}
    		}
    	}
    	
    	public void takeCard(){
    		DeckObj.CheckCards(TableObj);
    		boolean allow = true ;
    		for(int i = 0 ; i < playerHand.size() ; i ++){
    			if(playerHand.get(i).ColorValue().equals(TableObj.getColor()) || playerHand.get(i).NumberValue().equals(TableObj.getNumber()) || playerHand.get(i).NumberValue().equals("8")){
    				allow = false ;
    			}
    		}
    		if(allow){
    			playerHand.add(DeckObj.getCard());
    		}else{
    			System.out.println("You Can't take a Card when you already have to play.");
    		}
    	}
    	
    	public void CardsPrint(){
    		
    		System.out.print("You have : ");
    		for(int i = 0 ; i < playerHand.size()-1 ; i++){
    			System.out.print(playerHand.get(i).NumberValue() + " " + playerHand.get(i).ColorValue()+ ", " );
    		}
    		System.out.print(playerHand.get(playerHand.size()-1).NumberValue() + " " + playerHand.get(playerHand.size()-1).ColorValue());
    		System.out.println();
    		
    		System.out.print("computer have : ");
    		for(int i = 0 ; i < playerHand.size(); i++){
    			System.out.print(computerHand.get(i).NumberValue() + " " + computerHand.get(i).ColorValue()+ ", " );
    		}
    		System.out.println();
    		System.out.print("Top of cards in the table :");
    		System.out.println(TableObj.getNumber() + " " + TableObj.getColor());
    	}
    
    }
    why i do this ? because i see that some methods are only useful from only one of players (Human - computer)
    ( for example CardsPrint() method is only in use of Human player )

    now the problem is when i try to run this program with this main :


    Java Code:
    class CrazyEights{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		ComputerPlayer Computer = new ComputerPlayer();
    		HumanPlayer Human = new HumanPlayer();
    		boolean flag;
    		String answer  ;
    		
    		System.out.println("~~~~~~~~~~~~~~~~~~~CRAZY EIGHTS~~~~~~~~~~~~~~~~~");
    		System.out.println(" Some Rules Befor Play : You play until your cards finished");
    		System.out.println(" You will Draw a card until you will have to play");
    		System.out.println(" And you can change the color with 8 Card!");
    		System.out.println();
    		String Winner = null;
    		while(Winner == null){
    			System.out.println("It's Your Turn.");
    			Human.CardsPrint();
    			System.out.println();
    			System.out.println("Take or Play A Card?");
    			System.out.println("To take a card give \"Take\" ");
    			System.out.print("And for the Card Give the full name (e.x 5 Heart) : ");
    			answer = input.nextLine();
    			flag = false ;
    			while(!flag){
    				if(answer.equalsIgnoreCase("Take")){
    					Human.takeCard();
    					Human.CardsPrint();
    					System.out.println();
    					System.out.print("Take or Play A Card? : ");
    					answer = input.nextLine();
    				}else{
    					flag = Human.PersonPlay(answer);
    					if(!flag){
    						answer = input.nextLine();
    					}
    				}
    			}
    			Winner = Human.WinnerCheck();
    			if (Winner != null){
    				break ;
    			}
    			System.out.println();
    			System.out.println();
    			System.out.println("It's computer's turn now");
    			Computer.ComputerPlay();
    			
    		}
    		System.out.println("The Winner is " + Winner );
    	}
    }
    (if you try to run the program you will see some numbers at beginning this is the cards left in game while the computer and player take 5 cards )
    the program is running but the HumanObj has a separate informations from ComputerPlayer ( e.x Deck,Table,computerHand,playerHand )
    so how can i make the TableObj and the DeckObj to be the same in both of subclasses ?

    thx you in advance.. if my post need more information tell me
    and apologize for my english which is rather poor , please be lenient ;)
    Last edited by ShadowWalker; 04-24-2013 at 05:15 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: java inheritance problem

    You need t have your controller (in your case the main() method) create these and pass them to the constructors for any players.
    At the moment you are simply creating new ones for each Player.
    And since you have a Table object you may as well make that an attribute of the Deck, so pass it into the Deck constructor.
    That would save you having to pass it into checkCards(), which will help with the recommendation I make below.

    A couple of style comments.
    Methods and variables should start with a lowercase.
    Accessor methods should be called get<name of attribute starting with an uppercase>...eg an accessor for an attribute called 'number' would be getNumber.
    The checkCards() method should be private to the Deck, and should be called whenever getCards() is called. If you look at your code, you are calling it prior to every getCard, so you may as well hide that logic inside Deck, and not rely on other classes remembering to do that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: java inheritance problem

    Very good advice! thx you for the fast reply i will try it ;) !!

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: java inheritance problem

    All good but if i have right the Constructor of GeneralPlayer will run twice right? so this will make problem in the game .. :\

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: java inheritance problem

    If you have a constructor like this in GeneralPlayer:
    Java Code:
    public GeneralPlayer(Deck deck, Table table) {
       this.deck = deck;
       this.table = table;
    }
    and the same sort of constructor for the two subclasses (which need a call to super(deck, table) as the first line), then I don't see what the problem is going to be?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: java inheritance problem

    yeah this is my new Constructor but as you can see i want to give 5 cards in computerHand and in playerHand so this must be seperate in ComputerPlayer class and in HumanPlayer class right? hmm now i see the point of inheritance :P

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: java inheritance problem

    Well, the GeneralPlayer should have just the one hand...their own.
    I don't see why that's a problem?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: java inheritance problem

    no problem i fix it ;) i am ok now thx you for your help!!

Similar Threads

  1. Inheritance problem
    By paquizzle in forum New To Java
    Replies: 1
    Last Post: 08-18-2012, 09:12 PM
  2. Java Class Inheritance Problem
    By Batman in forum New To Java
    Replies: 2
    Last Post: 09-12-2011, 12:01 AM
  3. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  4. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •