Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Create an Array wich represent Classes

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create an Array wich represent Classes

    Not sure wheter to post it here or not, but I've got a question.
    I'm Trying to make the following:

    A Class called Point with the methods:
    SetPoint() //Sets pointx to 0 and pointy to 0
    SetPointNumber(int x, int y) //Set pointx to x and pointy to y
    Print() //Print pointx & pointy

    A Class called Shape with the methods:
    ShapeCreate() - This must create 3 points (an array) and set the point x & y to 0.

    I've got the following:
    class Point {
      int x, y;
      void Point() {
        x = 0;
        y = 0;
      }
      void Set(int x, int y) {
        this.x  = x;
        this.y  = y;
      }
      void Print() {
        System.out.println("x = " + x + "\nY = " + y);
      }
    }
     
    class Shape {
      void Shape() {
        Point Points[] = new Point[3];
     
        Point[1].Print();
        Point[2].Print();
        Point[3].Print();
     
      }
    }
     
    public class Practicum4VeelHoek {
      public static void main(String[] args) {
        Shape shape = new Shape();
     
      }
    }

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at practicum.pkg4.veelhoek.Veelhoek.Init(Practicum4Ve elHoek.java:29)
    at practicum.pkg4.veelhoek.Practicum4VeelHoek.main(Pr acticum4VeelHoek.java:50)
    Java Result: 1

    Does anybody know what I'm doing wrong
    Last edited by Sjaakie91; September 21st, 2011 at 05:41 AM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    Italy
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create an Array wich represent Classes

    why
    class Shape {
      void Shape() {
        Point Points[] = new Point[3];
     
        Punten[1].Print();
        Punten[2].Print();
        Punten[3].Print();
     
      }
    }

    and not :
    class Shape {
      void Shape() {
        Point Points[] = new Point[3];
     
        Points[1].Print();
        Points[2].Print();
        Points[3].Print();
     
      }
    }

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create an Array wich represent Classes

    Sorry in My java code it is OK, but on the Forum I translated it to English. I corrected my begin post.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create an Array wich represent Classes

    Array indexes start at zero. So an array of size 3 will have indexes 0, 1, and 2. Using 3 as an index is accessing the fourth element, which a length 3 array obviously doesn't have.

    Recommended reading: http://download.oracle.com/javase/tu...ts/arrays.html

    In the future, you might want to abide by the standard naming conventions. Methods and variables start with a lower-case letter, classes and constructors start with an upper-case letter. It might seem trivial, but it makes your code much harder to read if you don't follow the guidelines.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Where do I create my array?
    By SkynetSystems in forum Object Oriented Programming
    Replies: 7
    Last Post: September 2nd, 2011, 05:11 PM
  2. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM
  3. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 09:14 AM
  4. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM
  5. How to represent a chain of moves?
    By maikeru in forum Collections and Generics
    Replies: 0
    Last Post: December 31st, 2009, 01:24 AM