Results 1 to 2 of 2
  1. #1
    Verna_Venisa is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Exclamation How to print an object-filled array?

    Alright, this is actually my first post ^^', so I hope I'm placing everything into the right areas etc. Let me know if I'm doing anything wrong lol.

    Okay, so I have a project due about creating a deck of cards and doing a handful of different things to them (sorting them, comparing two hands, you catch my drift). Actually, here's just the project spec to make it easy on all of us:cise.ufl.edu/class/cis3023fa08/projects/P1/Spec.html

    Now, I'm not asking any of you to do my homework (as awesome as that would be, I doubt my conscience could take it), but I'm having issues in printing my array cards[]. Now, I have code to print the array AS it's being fed values:

    Java Code:
    for(i=0; i<cardNumber; i++){
    Card card = new Card();
    if(i<12 && i>=0){
    card.setSuit('s');
    card.setNumber(i+1);
    }
    else if(i<25 && i>=13){
    card.setSuit('h');
    card.setNumber(i+1);
    }
    else if(i<38 && i >=26){
    card.setSuit('c');
    card.setNumber(i+1);
    }
    else if(i<52 && i>=39){
    card.setSuit('d');
    card.setNumber(i+1);
    }
    else{}
    cards1[i] = card;
    System.out.println("Card Number " + (i+1));
    System.out.println(card.toString());
    System.out.println();
    }
    However, I want to be able to print this array outside of this particular situation (like after a card is changed, etc). I suspect it has something to do with my toString methods in classes Card or Hand, but I really don't know enough to fix my code.

    Any help would be welcome, and if you see any other errors in my code, please let me know and I'll fix them asap :]

    Ah, and here's the code that I have right now, in case it's needed:

    Java Code:
    import java.util.Comparator;
    
    class Card implements Cloneable, Comparable{
            private static Integer number;
            private static Character suit;
    
            public Card(){
                    number = null;
                    suit = null;		
    		}
    		
            public Card(Integer number, Character suit){
                    this.number = number;
                    this.suit = suit;
    		}
    
            public Integer getNumber(){
                    return number;
    		}
    
            public void setNumber(Integer number){
                    this.number = number;
    		}
    
            public Character getSuit(){
                    return suit;
    		}
    
            public void setSuit(Character suit){
                    this.suit = suit;
    		}
    		
    		public static Card newCard(Character suit, Integer number){
    			Card newCard = new Card(number, suit);
    			return newCard;
    		}
    		
    		public String toString(){
    			return "Number is " + number + "\n" +
    				   "Suit is " + suit;
    		}
    		
    		public int compareTo(Object object) {
    			return -1;
    		}
    }
    
    class Hand implements Cloneable, Comparable{
    	private static Card cards[] = new Card[26];
    	
    	public Hand(){
    		cards = null;
    	}
    	
    	public Hand(Card cards[]){
    		this.cards = cards;
    	}
    	
    	public Card[] getCards(){
    		return cards;
    	}
    	
    	public static void setCards(Card cards[]){
    		cards = cards;
    	}
    	
    	public static Card[] createHand(int inputLength){
    		Card hand1[] = new Card[inputLength];
    		for(int i = 0; i<hand1.length; i++){
    			hand1[i] = cards[i];
    			System.out.println(hand1[i]);
    		}
    		return hand1;
    	}
    	
    	public String toString(Card cards1[], int i){
    		return "Card:  " + (i+1) + cards1[i].toString(cards1, i);
    	}
    	
    	public int compareTo(Object object) {
    			return -1;
    	}	
    }
    
    class Driver{
    	public static void main(String args[]){
    		int selection = 0;
    		int cardNumber = 0;	
    		int i = 0, editCard = 0;
    		Integer editNumber = new Integer();
    			editNumber = null; 
    		Character editSuit = new Character();
    			editSuit = null;
    		Card cards1[] = new Card[26];
    		Card cards2[] = new Card[26];
    		
    		do{
    			System.out.println("Please enter the number of one of the menu options:");
    			System.out.print("(1) Select a pre-created hand (with whatever default of five or more cards of your choosing)\n"
    				+"(2) Enter the total number of cards in the hand and then select each card number and suit.\n"
    				+"(3) Print either hand\n"
    				+"(4) Sort either hand number, suit, or suit and number of the cards held within the hand\n"
    				+"(5) Compare two hands by point total, face cards, or same suit length\n" 
    				+"(6) Quit");
    			selection = UserInput.readInt();
    	
    			if (selection == 1){
    				Card hand1[] = Hand.createHand(5);
    			}
    			else if (selection == 2){
    		
    			do{
    			System.out.println("Please enter the number of cards you would like in your hand (greater than five):");
    			cardNumber = UserInput.readInt();
    				
    			if(cardNumber<5){
    				System.out.println("You must enter a greater number of cards.");
    			}
    			else {
    				for(i=0; i<cardNumber; i++){
    					Card card = new Card();
    					if(i<12 && i>=0){
    						card.setSuit('s');
    						card.setNumber(i+1);
    					}
    					else if(i<25 && i>=13){
    						card.setSuit('h');
    						card.setNumber(i+1);
    					}
    					else if(i<38 && i >=26){
    						card.setSuit('c');
    						card.setNumber(i+1);
    					}
    					else if(i<52 && i>=39){
    						card.setSuit('d');
    						card.setNumber(i+1);
    					}
    					else{}
    					cards1[i] = card;
    					System.out.println("Card Number " + (i+1));
    					System.out.println(card.toString());
    					System.out.println();
    				}
    				Card hand1[] = Hand.createHand(cardNumber);
    			}
    			}while(cardNumber<5);		
    					System.out.println("Second hand");
    						for(i=26; i<cardNumber; i++){
    							Card card = new Card();
    							if(i<12 && i>=0){
    								card.setSuit('s');
    								card.setNumber(i+1);
    							}
    							else if(i<25 && i>=13){
    								card.setSuit('h');
    								card.setNumber(i+1);
    							}
    							else if(i<38 && i >=26){
    								card.setSuit('c');
    								card.setNumber(i+1);
    							}
    							else if(i<52 && i>=39){
    								card.setSuit('d');
    								card.setNumber(i+1);
    							}
    							else{}
    							cards2[i] = card;
    							System.out.println("Card Number " + (i+1));
    							System.out.println(card.toString());
    							System.out.println();
    						}
    				Card card = new Card();
    				System.out.println("Please enter the number of card that you would like to edit");
    					editCard = UserInput.readInt();
    				System.out.println("What suit would you like to make this card? (s, h, c, d)");
    					editSuit = UserInput.readChar();
    				System.out.println("What value would you like to make this card? (between 1 and 13)");
    					editNumber = UserInput.readInt();
    				cards1[editCard] = card;
    				card.setSuit(editSuit);
    				card.setNumber(editNumber);
    			}		
    		} while(selection != 6);
    	}	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What is the program outputing now? What is wrong with its output?
    What do you want the output to be?
    YOu need to localize the problem. If we don't know what the output is supposed to look like, how can we make recommendations?

    You need to add some comments in the code describing what you are doing and why. You have a lot of numbers in the code that are NOT explained: 26, 52, 39 etc Add comments to the code explaining them. It would be better to use final ints to define those constants:
    final int FirstClub = 26;
    final int FirstDiamond = 39;
    Or whatever they are used for. Then when you use that variable it defines what it is being used for.

    What are cards1 and cards2 used for?
    And on and on and on.

Similar Threads

  1. 3d array filled with garbage values
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 01-01-2009, 11:12 PM
  2. initialising Array of object??
    By Somitesh Chakraborty in forum New To Java
    Replies: 4
    Last Post: 08-27-2008, 10:00 PM
  3. Array of Object
    By haiforhussain in forum New To Java
    Replies: 1
    Last Post: 06-25-2008, 12:24 PM
  4. How to save name in Array and Print saved Name
    By ppkkyaw in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:44 AM
  5. Object array query
    By bugger in forum New To Java
    Replies: 5
    Last Post: 01-07-2008, 06:58 PM

Posting Permissions

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