Welcome to Dream.In.Code
Become a Java Expert!

Join 149,973 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,406 people online right now. Registration is fast and FREE... Join Now!




Arrays of Class Instances

 
Reply to this topicStart new topic

Arrays of Class Instances, not sure why it won't compile...

mse12
29 Oct, 2008 - 10:17 AM
Post #1

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

i can not seem to get my code to compile.

any help would be greatly appreciated.

it will not find my displayValue method although it is clearly defined in my Card class...

Card Class
CODE


public class Card {
  private int value;
  
  public int Card(int setValue) {
    value = setValue;
  return value;
  }

  void displayValue() {
    for (int i = 1; i <= 5; i++) {
      int card = (int) (Math.random() * 13) + 2;
      if (card == 11)
        System.out.print("Jack ");
      else if (card == 12)
        System.out.print("Queen ");
      else if (card == 13)
        System.out.print("King ");
      else if (card == 14)
        System.out.print("Ace ");
      else
        System.out.print(value);
    }
  }
}




Poker Class
CODE


/*
1. (20 pts) Write a template class called Card in a separate file. It should have one instance variable for the
value of the card. The constructor receives no parameters and sets the value to a random number from 2 to
14 using Math.random(). Include a method called displayValue() that prints the value of the card using the ,
ÒJackÓ for 11, ÒQueenÓ for 12, ÒKingÓ for 13, and ÒAceÓ for 14. The method receives no parameters and
does not return anything. Hint: see the solution to homework #3.  

2. (50 pts) In a separate file, write an application class called Poker that creates an array of five Cards using the
template class from problem #1. Display all of the card values as well as the highest card value. The
application class should always use the displayValue() method to print card values, but just print the number
for the highest card. The following is an example of how your output might look:

3. (30 pts) In a separate file, write an application class called Poker2 that is just like the Poker class from
problem #2 except that it displays the highest card by using the actual element in the array that has the
highest value. The following is an example of how your output might look:
*/

public class Poker {
  public static void main (String args[]) {
    int highest = 0;
    System.out.print("Your cards are: ");
    Card[] theCards;
    theCards = new Card[5]; // Creates an array of 5 cards
    for (int i = 1; i < 5; i++){      
      theCards[i] = new Card();
      theCards[i] = theCards.displayValue();
    if (theCards[i] > highest)
      highest = theCards[i];  
    }    
  }
}
  
  
  



User is offlineProfile CardPM
+Quote Post

g00se
RE: Arrays Of Class Instances
29 Oct, 2008 - 10:23 AM
Post #2

D.I.C Addict
Group Icon

Joined: 19 Sep, 2008
Posts: 521



Thanked: 44 times
My Contributions
public int Card(int setValue) {

should be

CODE

public Card() {


(per sec)
User is offlineProfile CardPM
+Quote Post

mse12
RE: Arrays Of Class Instances
29 Oct, 2008 - 10:27 AM
Post #3

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

i still have the same exact errors. sad.gif
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Arrays Of Class Instances
29 Oct, 2008 - 10:38 AM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
What goose meant was it should be:

public Card(int setValue) {
value = setValue;
}

It's a constructor with one parameter and a constructor doesn't return anything. (I guess it does but not using the return statement)

User is online!Profile CardPM
+Quote Post

g00se
RE: Arrays Of Class Instances
29 Oct, 2008 - 10:45 AM
Post #5

D.I.C Addict
Group Icon

Joined: 19 Sep, 2008
Posts: 521



Thanked: 44 times
My Contributions
QUOTE(Gloin @ 29 Oct, 2008 - 11:38 AM) *

What goose meant was it should be:

...
It's a constructor with one parameter ..


Nope - it should be an empty ctor (see spec)
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Arrays Of Class Instances
29 Oct, 2008 - 11:08 AM
Post #6

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
Sorry, didn't read the spec, I was just looking at the sample code and figured you can't just change to:

public Card() {

without changing the code within the constructor.

User is online!Profile CardPM
+Quote Post

mse12
RE: Arrays Of Class Instances
29 Oct, 2008 - 07:24 PM
Post #7

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

more compiling issues crazy.gif

CODE
public class Card {
  int value;

  Card() {
    value = (int)(Math.random() * 13) + 2;
  }

  void displayValue() {
      if (value == 11)
        System.out.print("Jack ");
      else if (value == 12)
        System.out.print("Queen ");
      else if (value == 13)
        System.out.print("King ");
      else if (value == 14)
        System.out.print("Ace ");
      else
        System.out.print(value);
    }
  }


CODE
/*
1. (20 pts) Write a template class called Card in a separate file. It should have one instance variable for the
value of the card. The constructor receives no parameters and sets the value to a random number from 2 to
14 using Math.random(). Include a method called displayValue() that prints the value of the card using the ,
ÒJackÓ for 11, ÒQueenÓ for 12, ÒKingÓ for 13, and ÒAceÓ for 14. The method receives no parameters and
does not return anything. Hint: see the solution to homework #3.  

2. (50 pts) In a separate file, write an application class called Poker that creates an array of five Cards using the
template class from problem #1. Display all of the card values as well as the highest card value. The
application class should always use the displayValue() method to print card values, but just print the number
for the highest card. The following is an example of how your output might look:

3. (30 pts) In a separate file, write an application class called Poker2 that is just like the Poker class from
problem #2 except that it displays the highest card by using the actual element in the array that has the
highest value. The following is an example of how your output might look:
*/

public class Poker {
  public static void main (String args[]) {
    int highest = 0;
    System.out.print("Your cards are: ");
    Card[] theCards;
    for (int i = 1; i <= 5; i++) {
      theCards = new Card[5];} // Creates an array of 5 cards
    for (int i = 0; i < theCards.length; i++){      
      if (theCards[i].value > highest)
      highest = theCards[i].value;
      theCards[i].displayValue();}
    System.out.println("Your highest card: " + highest);}}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Arrays Of Class Instances
29 Oct, 2008 - 07:50 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,579



Thanked: 233 times
Dream Kudos: 75
My Contributions
No problem with your Card class

Howver in the Poker class... you will have to learn how to init an array
... you have to init first the full array
... then each of its elements

CODE

public class Poker {
    public static void main (String args[]) {
        int highest = 0;
        System.out.print("Your cards are: ");
        Card[] theCards = new Card[5];         // init first the full array
        
    
        for (int i = 0; i < theCards.length; i++){  
            theCards[i] = new Card();           // each element
            if (theCards[i].value > highest)
                highest = theCards[i].value;
            theCards[i].displayValue();}
        System.out.println("Your highest card: " + highest);
    }
}

User is online!Profile CardPM
+Quote Post

-fedexer-
RE: Arrays Of Class Instances
30 Oct, 2008 - 09:18 AM
Post #9

New D.I.C Head
*

Joined: 3 Jun, 2008
Posts: 43



Thanked: 2 times
My Contributions
Also another point to note is that i reckon you would have been getting an array index out of bounds exception, due to you starting at 1 instead of 0, so you would have been trying to call the 6th element, which doesn't exist.

However pbl's code fixes all of that anyway, i was just making you aware of that also.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month