1

I created 2 source code files, Shirt.java and ShirtTestArray.java

The problem I am facing is that whenever I try to access the displayShirtInformation() method, I'm not able to do it.. and get an error class,interface,or enum expected

The 2 source files are given below--

Shirt.java--

public class Shirt {

  public int shirtID = 0; // Default ID for the shirt
  public String description = "-description required-"; // default

  // The color codes are R=Red, B=Blue, G=Green, U=Unset
  public char colorCode = 'U';
  public double price = 0.0; // Default price for all shirts
  public int quantityInStock = 0; // Default quantity for all shirts

  public Shirt() {
  }

  public Shirt(int ID, String d, char c, double p, int q) {
    shirtID = ID;
    description = d;
    colorCode = c;
    price = p;
    quantityInStock = q;
  }

  // This method displays the values for an item
  public void displayShirtInformation() {

    System.out.println("Shirt ID: " + shirtID);
    System.out.println("Shirt description:" + description);
    System.out.println("Color Code: " + colorCode);
    System.out.println("Shirt price: " + price);
    System.out.println("Quantity in stock: " + quantityInStock);

  } // end of display method
} // end of class

ShirtTestArray.java --

public void  ShirtTestArray{
  public static void main(String [] args) {
    Shirt [] shirtArray={new Shirt (1,"blue",'b',76.67,4),new Shirt(2,"green",'g',98.79,1),
    new Shirt(3,"green",'g',34.78,90)};

    shirtArray[0].displayShirtInformation();
    shirtArray[1].displayShirtInformation();
    shirtArray[2].displayShirtInformation();

  }
}
1
  • In the interest of following OO best practices, please make your member variables private - that, or consider an immutable object. You may also wish to change your 'color' char to an enum, to prevent data-entry errors (which your own code possibly falls victim to - is the case important?). Commented Aug 24, 2011 at 15:41

3 Answers 3

9

The second class should be declared as class, not void:

public class ShirtTestArray{
       ^
    public static void main(String [] args){
    Shirt [] shirtArray={new Shirt

(1,"blue",'b',76.67,4),new Shirt(2,"green",'g',98.79,1),
    new Shirt(3,"green",'g',34.78,90)};



       shirtArray[0].displayShirtInformation();
       shirtArray[1].displayShirtInformation();
       shirtArray[2].displayShirtInformation();

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

nice catch, didn't see that one.
0

You're giving ShirtTestArray a return definition, saying it should be a method, when it should be a class.

Replace 'void' with 'class' and you should be rolling.

Comments

0

Syntax error is public void ShirtTestArray{ instead of public class ShirtTestArray{. Cheers :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.