Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

help altering code in one of my methods.

Posted on 2006-04-27
5
Medium Priority
?
553 Views
Last Modified: 2012-06-27
I created the class card

class Card
{
  private int rank;
  private char suit;
 
  public int getRank()
  {
    return(rank);
  }
 
  public char getSuit()
  {
    return(suit);
  }
 
  public Card(int r,char s)
  {
    rank = r;
    suit = s;
  }
 
 
  public boolean equals(Card otherCard)
  {
    if((rank == otherCard.rank) &&
       (suit == otherCard.suit))
      return(true);
    else
      return(false);
  }
 
  public void displayCard()
  {
    System.out.println(rank + " of " + suit);
  }
 
}

i then created the class deskOfCards

import java.util.*;
import java.util.Collections.*;

class DeckOfCards
{
  private ArrayList<Card> card = new ArrayList<Card>();
 
 public DeckOfCards()
  {
   
    for(int i=0; i < 52; i++)
    {
      if(i < 13)
        card.add(new Card(i%13+1,'S'));
      else        
        if(i < 26)
            card.add(new Card(i%13+1,'H'));
       
      else
        if(i < 39)
              card.add(new Card(i%13+1,'D'));
      else
              card.add(new Card(i%13+1,'C'));
   
   
     
    }  
     
   
  }
 
  public void shuffle()
  {
    Collections.shuffle(card);
  }

  public Card pickNextCard()
  {
    return card.remove(0);
  }
 
}

and also the class

import java.util.*;

public class HandOfCards
{
  public ArrayList<Card> card1;

public HandOfCards()
{
  card1 = new ArrayList<Card>();
}

public void addCard(Card s)
{
  card1.add(s);
}

public void display()
{
  for(int i = 0; i <= card1.size(); i++)
  {
  card1.get(i).displayCard();
 }
}

public int totalScore()
{
  int cardvalue = 0;
  int sum = 0;
  for(int i = 0; i <= card1.size(); i++)
  {
    cardvalue = card1.get(i).getRank();
    sum = sum + cardvalue;
  }
  return(sum);
}

public boolean isBust()
{
 if (totalScore() > 21)
  return(true);
 else
   return(false);
}
}


I then made another class with a main method to run and test these classes

import java.util.*;

public class Pontoon
{
  public static void main(String[] args)
  {
    int stickortwist = 2;
    Scanner keyboard = new Scanner(System.in);
    DeckOfCards newDeck = new DeckOfCards();
    newDeck.shuffle();
    HandOfCards newHand = new HandOfCards();
    newHand.addCard(newDeck.pickNextCard());
    newHand.addCard(newDeck.pickNextCard());
    System.out.println("Press 1 to stick, or 2 to twist");
    stickortwist = keyboard.nextInt();
    if (stickortwist == 2)
   {
    newHand.addCard(newDeck.pickNextCard());
    System.out.println("Press 1 to stick, or 2 to twist");
   stickortwist = keyboard.nextInt();
    }
    newHand.totalScore();
  }
}

When i try and run the main method i get the error

IndexOutOfBoundsException: Index: 3, Size: 3
  at java.util.ArrayList.RangeCheck(Unknown Source)
  at java.util.ArrayList.get(Unknown Source)
  at HandOfCards.totalScore(HandOfCards.java:31)
  at Pontoon.main(Pontoon.java:22)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)


so i think i need to change the code for the totalScore method but i don't know what is wrong with it.

thanks for your help

0
Comment
Question by:andybest
  • 3
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16551784
>>for(int i = 0; i <= card1.size(); i++)

should be

for(int i = 0; i < card1.size(); i++)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16551875
Yes, array index starts from 0 in Java (like it did in C, C++ too) so the last element is at (size - 1) :)
0
 

Author Comment

by:andybest
ID: 16551877
wow, that was simple. cheers for the help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16551890
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16551898
btw you might like to look at the Java blurb on Enum. Card is a perfect candidate for enum and in fact is used for one of Sun's examples
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question