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:
Code :
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
Re: Create an Array wich represent Classes
why
Code :
class Shape {
void Shape() {
Point Points[] = new Point[3];
Punten[1].Print();
Punten[2].Print();
Punten[3].Print();
}
}
and not :
Code :
class Shape {
void Shape() {
Point Points[] = new Point[3];
Points[1].Print();
Points[2].Print();
Points[3].Print();
}
}
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.
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.