[java] Objects in Java - a beginner

Started by
11 comments, last by cordel 15 years, 4 months ago
Hello everyone . I have a class called "ShapeContainer" and I need to build its Constructor. Its declaration : public ShapeContainer() // Constructs a container which holds no shapes each cell is type Point ,when Point is an object : public Point () { // Constructs a point in (0,0) xval=0; yval=0; } so I wrote : public class ShapeContainer { public static final int INIT_SIZE=10; Point[] shape_arr; public ShapeContainer() { // Constructs a container // with no cells shape_arr=new Point[INIT_SIZE]; } but I need to initialize shape_arr with null , how do I do it ? thanx in advance. }
Advertisement
Quote:Original post by cordel
but I need to initialize shape_arr with null , how do I do it ?
thanx in advance.
You don't have to. The language does it for you.

As a matter of design, try to determine whether a List<Point> is not better than an array for what you're trying to do (usually, creating a non-constant array from a global constant is a symptom of a problematic design).
Can I write it maybe like that :


public class ShapeContainer {


public static final int INIT_SIZE=10;
Point[] shape_arr=new Point[INIT_SIZE];

public ShapeContainer() {

shape_arr=null;

}
}
Quote:Original post by cordel
shape_arr=null;
This would literally 'initialize shape_arr with null', meaning that your object wouldn't contain an array. However, since you put that array in there in the first place, I doubt this is what you're trying to do.
Okay ,so if I'll put it this way: shape_arr[INIT_SIZE]=null;
would it initialize all the cells to null ?
Quote:Original post by cordel
Okay ,so if I'll put it this way: shape_arr[INIT_SIZE]=null;
would it initialize all the cells to null ?
I'm assuming you're referring to code such as this:

shape_arr = new Point[INIT_SIZE];shape_arr[INIT_SIZE] = null;


This code will run the first line, which creates an array of points of size INIT_SIZE and initializes all the cells with null. It then runs the second line, which attempts to set a cell to null, but since the cell is out of bounds it will just throw an exception.

Had you written only this line:

shape_arr[INIT_SIZE] = null;


Then the shape_arr variable would not reference a valid array and the statement would throw a null reference error.

As I've already said, the language does it for you, so don't worry about initializing your array cells to null yourself.
Thanks ,first of all...

What you're saying is wierd since my driller said that I need to make sure that the array of Point is initialized to NULL.

If I'm reading you correctly all I need to do is :

Point[] shape_arr;

public ShapeContainer() {
shape_arr=new Point[INIT_SIZE];
}

I still don't understand why he insists to use the constructor ,when I simply can say :

Point[] shape_arr;
shape_arr=new Point[INIT_SIZE];

?
When you create an array in java if you do not define what is in it, it will automatically be defined to be fully null. However Java wants to make you have initialized it prior to using it.
I would suggest doing it like this, because as it stands your constructor is not deciding the size of the array for shape_arr.
Point[] shape_arr = new Point[INIT_SIZE];

public ShapeContainer() {
}

However if if you send a parameter into ShapeContainer() that defined how large the array would be like so ->

public ShapeContainer(int size) {
}
THEN you could simply do
Point[] shape_arr;
and inside the constructor you could do
shape_arr = new Point[size];


Some people like all initialization to happen at the construction period and not the compilation point and I generally agree only if it is not a constant. But seeing as you are doing it with a constant I disagree with your driller. However the reason I showed above is why you should initialize in the constructor.
All right thank you.

I have another question and I'll appreciate your advice.

In general ,I need to create an object called ShapeConatiner which is an array of 10 cells (0..9) and each cell is type Point ,and the Xval symbolise Triangles , the Yval - Rectangles.

Now I'm trying to write "add rec" to ShapeConatiner, when getting to a NULL cell - put the rec there.

the code :

public Rectangle(Point p1,Point p2) { // create rec
point1=new Point(p1);
point2=new Point (p2);
}

public void add(Rectangle r) { // add rec to shape-container
for (int i=0;i<shape_arr.length;i++) {
if (shape_arr==null)
shape_arr.yval=rectangle(r.point1,r.point2);

and it doesn't work ,what's the mistake ?
10x
1. Use the [code] or [source] tags. They're detailed in the FAQ.

2. "Doesn't work" isn't a valid problem description. Help us help you by describing your exact problem. Does it fail to compile? If so what are the compiler errors? Does it crash? Do you get a stack trace? Does it run but produce the wrong results? etc. etc.

3. shape_arr.yval doesn't exist yet because it's null - there is no object for you to retrieve a yval from. Instead just assign to the slot in the array.

Also, you need to 'new' your rectangle object to create the object to go into the array. And you probably should 'break' out of the loop so you don't fill up the entire array when you just want to add one new rectangle.

public void add(Rectangle r) { for (int i=0;i<shape_arr.length;i++) {  if (shape_arr==null) {    shape_arr = new Rectangle(r.point1,r.point2);    break;  }}

This topic is closed to new replies.

Advertisement