Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Total an ArrayList


    Hi there, I currently have this class. And I was wondering if anyone could help me total the ArrayList created.

    Code:
    /*
     * Models a pack of cards with methods to discover 'interesting' things about the pack.
     */
    import java.util.ArrayList;
    
    public class PackCards {
    
    	private ArrayList<Card> pack;
    
    	/*
    	 * Create a random pack of size n
    	 */
    	public PackCards(int n) {
    		Card c;
    		pack = new ArrayList<Card>();
    		for (int i = 1; i <= n; i++) {
    			c = new Card();
    			pack.add(c);
    		}
    	}
    
    	public void printPack() {
    		for (Card c : pack) {
    			System.out.println(c.cardString());
    		}
    	}
    
    	/*
    	 * Total the numerical value of all the cards in the pack
    	 */
    	public int totalPack() {
    		int totalCard = 0
    		for (int i=0; i < )
    
    	}
    
    	/*
    	 * Find all the cards of a suit specified by String e.g. "Spades"
    	 */
    //	public ArrayList<Card> findSuit(String suit) {
    //
    //	}
    
    	/*
    	 * Find largest card in pack taking into account numerical value and suit
    	 * Diamonds is lowest, then Clubs, then Hearts, then Spades is largest.
    	 */
    //	public Card findLargest() {
    //
    //	}
    
    	/*
    	 * Return true if there is a duplicate card in the pack
    	 */
    //	public boolean hasDuplicate() {
    //
    //	}
    
    	/*
    	 * Return any duplicate card, 'null otherwise
    	 */
    //	public Card getDuplicate() {
    //
    //	}
    
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    I say this because I know how to total an Array but not an array list. Do I still need to declare something at the start of the class e.g "private int cardTotal;" and then in the method set it to 0?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    Please help? I'm tearing my hair out at this
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    4
    Totaling an array isn't much different than an ArrayList.
    You might do this for an array;

    Code:
    int total(int[] array) {
        int total = 0;
        for (int i = 0; i < array.length; i++)
            total += array[i];
        return total;
    }
    The only changes that need to be made is element accessing (since java doesn't support operator overloading) and finding the length.

    Code:
    int total(ArrayList<Integer> array) {
        int total = 0;
        for (int i = 0; i < array.size(); i++)
            total += array.get(i);
        return total;
    }
    Of course you still have a bit of work to do. You're using an ArrayList of cards, so you'll probably be adding a value from the card to the total.

    total += array.get(i);
    might become
    total += array.get(i).value;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    Currently I have this:

    Code:
    	public int totalPack() {
    		int cardTotal = 0;
    		for (Card c : pack){
                 cardTotal += array.get(I).getValue();}
    		return cardTotal;
    	}
    but it's not returning a value. Any ideas?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    What is the purpose of the for loop that gets c from the list and then never uses the value of c in the loop?
    How is the list: array related to pack? Which one has the data that is to be summed?

    The posted code looks like you have mixed code for two different things.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    Would it be more help if I was to post the rest of the classes?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    I figured out my mistake, sorry for the bother guys.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    You need to post definitions for all the variables used in the posted code, but not any of the code that is not related to the problem.

    You didn't answer the questions I asked about the code in post#5 and about how it has some unrelated parts in it.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    Originally Posted by NormR
    You need to post definitions for all the variables used in the posted code, but not any of the code that is not related to the problem.

    You didn't answer the questions I asked about the code in post#5 and about how it has some unrelated parts in it.
    I was being an idiot basically. I used "array" as a placeholder pack and also used ".getValue" as a placeholder for ".getNumber". I really should check my code more thoroughly next time.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    I have another question.

    So basically what I need to do is to find the largest card in a randomly generated pack of 10 cards. So say for example the program generated the following 10 cards that are then stored in an ArrayList of Card obejcts:

    Four of Spades
    Eight of Spades
    Six of Hearts
    King of Clubs
    Six of Clubs
    Seven of Diamonds
    Ace of Clubs
    King of Clubs
    Seven of Diamonds
    Ace of Spades

    So currently I'm trying to write a method that will find the largest card in the pack. Each card has a numerical value of 1-13 with an Ace being 1 and a King being 13. So I want the method to loop through the ArrayList and find the cards with the highest numerical value. If 2 or more cards have the same numerical value the highest is determined by the suit of the card. With it being in the order of Spades > Hearts > Clubs > Diamonds. I have the first part of the method where it compares the numerical value but I'm stuck on comparing the suits of the cards. So far I have this:

    Code:
       public Card findLargest() { 
          Card largest; 
          Card c; 
          for (int i = 0; i < pack.size(); i++){ 
             if (c.getNumber() > largest.getNumber()){ 
             largest = c 
             else if (c.getNumber() == largest.getNumber()){ 
                if (largest.getSuit().equals("Diamonds")) 
                     largest = !c 
                 
             } 
       } 
    
       } 
       }
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    stuck on comparing the suits of the cards
    Can you explain the problem you are having?

    Is this it: Given two suits (as Strings) determine which has the higher value.
    Several solutions:
    Use a Map<Suit, value>
    Put all the suits in one String and use indexOf() on that String for the two suit Strings. Then compare the values returned. They won't be 0 1 2 3 but their values will be in the right order.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    Originally Posted by NormR
    Can you explain the problem you are having?

    Is this it: Given two suits (as Strings) determine which has the higher value.
    Several solutions:
    Use a Map<Suit, value>
    Put all the suits in one String and use indexOf() on that String for the two suit Strings. Then compare the values returned. They won't be 0 1 2 3 but their values will be in the right order.
    Basically say in the 10 randomly generated cards there is King of Diamonds and the King of Spades. Because they both have a numerical value of 13 I then need to compare the their suits to see which one is of a higher value which means that the King of Spades would be of a higher value because Spades > Diamonds.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0
    I know that my loop should follow this structure:

    Code:
    	public Card findLargest() {
    		Card largest;
    		Card c;
    		for (int i = 0; i < pack.size(); i++){
    			if (c.getNumber() > largest.getNumber()){
    			largest = c
    			else if (c.getNumber() == largest.getNumber()){
    				if (largest.getSuit().equals("Diamonds"))
    					  largest = c
    					  else if ()
    						  
    					  else if ()
    				
    			}
    	}
    
    	}
    	}
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    compare the their suits to see which one is of a higher value
    See post #12 for 2 possible solutions.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo