creating an array of instances of a class

i am getting errors when i try this code:
[code]
//////////////////////////////////////////////////////////////////////////////////
//* blackjackv1.java *//
//* a program which will eventually be a blackjack game, right now i only want it
//* to make an array of 12 cards called hndDealhand[], initialize the first card,
//* and output that card's number and suit *//
//////////////////////////////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;

//a class for cards
//member variables:
// char suit;int number
//constructors:
// Card(char s, int n);
// initializes suit to s and number to n
//methods:
// char getSuit();
// returns suit of the card
// int getNumber();
// returns value of the card
// void setSuit(char s);
// sets the suit to s
// void setNumber(int n);
// sets the number of the card eual to n
class Card {
char suit;
int number;
Card(char s, int n) {
this.suit = s;
this.number = n;
}
Card() {
}
char getSuit() {
return this.suit;
}
int getNumber() {
return this.number;
}
void setNumber(int num) {
this.number = num;
}
void setSuit(char s) {
this.suit = s;
}
}

public class blackjackv1 extends java.applet.Applet implements ActionListener, MouseListener, MouseMotionListener {

// font that outputted text will be
Font f = new Font("TimesRoman",Font.BOLD,36);
// the array that i am trying to make
Card hndDealhand[] = new Card[12];
// set the first card's suit and number to 'h' and 1
hndDealhand[0] = new Card('h',1);
// buttons for later, right now they do nothing
Button btnHit = new Button("Hit");
Button btnStand = new Button("Stand");

// initializes the buttons which don nothing right now
public void init() {
setLayout(null);
btnHit.setSize(40,20);
btnHit.setLocation(150,350);
btnStand.setSize(40,20);
btnStand.setLocation(250,350);
add(btnHit);
add(btnStand);
btnHit.addActionListener( this );
btnHit.addMouseMotionListener( this );
btnStand.addActionListener( this );
btnStand.addMouseMotionListener( this );
addMouseMotionListener( this );
repaint();

}

// outputs the number and suit of the first card in hndDealhand
public void paint(Graphics g) {
g.setFont(f);
g.drawString("Dealer hand: " + hndDealhand[0].getNumber() + " " + hndDealhand[0].getSuit(), 0, 30);

}

// action functions, right now they all do nothing

public void mouseDragged(MouseEvent musEvent) {
}
public void mouseMoved(MouseEvent musEvent) {
}
public void mouseClicked(MouseEvent musEvent) {
}
public void mousePressed(MouseEvent musEvent) {
}
public void mouseReleased(MouseEvent musEvent) {
}
public void mouseEntered(MouseEvent musEvent) {
}
public void mouseExited(MouseEvent musEvent) {
}
public void actionPerformed(ActionEvent actEvent) {
}
}
[/code]
please help me.
thanks,
executive idio

Comments

  • : i am getting errors when i try this code:

    Wanna elaborate on what the error is?

    "Faith alone, won't sustain us anymore."
    -Faith Alone, Bad Religion

  • : : i am getting errors when i try this code:
    :
    : Wanna elaborate on what the error is?
    :
    : "Faith alone, won't sustain us anymore."
    : -Faith Alone, Bad Religion
    :
    :
    The error are:
    blackjackv2.java:60: ']' expected
    hndDealhand[0] = new Card('h',1);
    ^
    blackjackv2.java:60: expected
    hndDealhand[0] = new Card('h',1);
    ^
    blackjackv2.java:53: class blackjackv1 is public, should be declared in a file n
    amed blackjackv1.java
    public class blackjackv1 extends java.applet.Applet implements ActionListener, M
    ouseListener, MouseMotionListener {
    ^
    blackjackv2.java:60: cannot resolve symbol
    symbol : class hndDealhand
    location: class blackjackv1
    hndDealhand[0] = new Card('h',1);
    ^
    4 errors


  • Your errors fall into two groups: the error on line 53, and the errors on line 60.

    The error on line 53 means exactly what it says. You're only allowed to define a single public class in a given source file, and the name of that class must be the same as the name of the source file. The mismatch could be a case of capitalisation - Java tools tend to be case sensitive, including the compiler.

    The errors on line 60 are caused by the fact that you're trying to execute a Java statement without being in the body of a method. You can declare fields within the body of a class (they are instance variables) but you can't execute arbitrary pieces of code. You should put the code in the init() method or the constructor.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories