• Post Reply Bookmark Topic Watch Topic
  • New Topic

coordinates in 2d array in java  RSS feed

 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am new to java and to this forum. The question I have deals with coordinate pairs in 2d arrays in java. I am supposed to Design a class named MyPoint to represent a point. The class contains:

Two double private data fields named x and y that specify the coordinates of the point. A pubic constructor that creates a point with a specified width and height. A pubic method named getX() that returns the x coordinate of a point. A pubic method named getY() that returns the y coordinate of a point. Write a test class named Lab12 with a main method to create a 2 by 3 array filled with points as follows:

(0.5, 1.2) (0.0, 3.14) (15.0, 27.5)

(6.6, 7.7) (1.2, 2.1) (12.0, 127.0)

The main method is supposed to use a pair of nested for-loops to print the contents of the array.

Your output should be

pt[0][0] = (0.5, 1.2)

pt[0][1] = (0.0, 3.14)

pt[0][2] = (15.0, 27.5)

pt[1][0] = (6.6, 7.7)

pt[1][1] = (1.2, 2.1)

pt[1][2] = (12.0, 127.0)

I have the class completed. I am working on the main method now. I think I am on the right track to print the array, but I need to know how to represent the coordinates in the 2d array. This is my code so far for the main method;

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you have a Point class?

The way I read this assignment, you should not have an array of doubles (and what you have looks wrong to me), but an array of points, something like this:



that gives you and empty array capable of holding your points, and then you'd have to insert them each in the correct spot.
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I do have the Point class, it is

thanks for your reply. once I correct the 2d array i will post it
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi namomi, welcome to the Ranch.

Don't be so fast with writing the code. I see you got some bad things in Lab12 class. Particularly lines 7 and 8. There you put your elements into arrays of arrays, but it doesn't look right, as somehow you trying to put 2 double numbers to one element - that is not allowed by Java. You should know that if you'd compile your code more often. When you write every 2 or 3 lines, try to compile it to see if it compiles, if not, fix errors and move forward. The earlier you know you got some errors, the easier is to fix them.

Lets start with the class MyPoint.

Well done having private instance variables. However, you don't need explicitly intialize x and y to 0, these are getting default values 0.0 upon object creation anyway.

Why you left default constructor there? In case it stays where it is now that means somebody can instantiate MyPoint without specified width and height, and then x and y would be 0.0 each. Is that right based on your instructions?
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutauras Vilda! this is the new point class;

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick fix

I didn't particularly mean to delete that constructor, I just wanted you to double check with your instructions, what these say. In case you understand them well, then we trust you removed them with full understanding. Being a bit pendantic, please delete lines 2, 5 or 6 and line 19 (3 in total) and you'll have ideal code formatting which fit all coding conventions.

Is that all with your MyPoint class? Or you have anything else to add there?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets assume that is it with MyPoint class (check if you need to document it with JavaDoc's).

Next, you have been asked to create Lab12 class. Problems I see started with populating array of arrays with my point instances.

Do you remember how to create an instance of MyPoint class you just created?
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought i deleted the default constructor. the default constructor wouldnt have any arguments right?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:i thought i deleted the default constructor. the default constructor wouldnt have any arguments right?
Yes, you deleted one of constructors - you deleted no argument constructor (personally I think it is right), unless your instructions says differently. Default constructor compiler adds one in case you don't define yours. In your case you defined constructor with 2 parameters and one constructor with no argument (once again, it is called no argument, and not default).
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not sure i am following you about the instance of the MyPoint class
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:Write a test class named Lab12 with a main method to create a 2 by 3 array filled with points
What does that mean? That means, that your array elements suppose to be points, these points can/should be constructed using MyPoint class you just created.

Do you know how to create an instance of the class?
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an array, but the way better than you had in your initial post. This array will be able to hold instances of MyPoint class.

Now look at the size of array. Declaring that way new MyPoint[3][2] you're saying, that your MyPoint array will be able to hold 3 elements which will be arrays too, and each of those arrays will be able to hold by 2 elements. Is it what you need? Think further. Pair of doubles represent 1 point. 1 element of array of arrays which is array itself suppose to be able to hold 3 elements in it.
naomi newman wrote:(0.5, 1.2) (0.0, 3.14) (15.0, 27.5)

(6.6, 7.7) (1.2, 2.1) (12.0, 127.0)

meansIs that right? I don't think, do you?

You need an array in which you could fit 2 arrays, where each of those arrays could hold 3 elements.
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it isnt. i see now that the 2 and 3 needs to be switched
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it needs to be

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct

Now you need to add elements to MyPoint array, where each element is a MyPoint class instance. How would you go with that?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is your hint, which tells you, which point suppose to have which arguments
naomi newman wrote:pt[0][0] = (0.5, 1.2)
pt[0][1] = (0.0, 3.14)
pt[0][2] = (15.0, 27.5)
pt[1][0] = (6.6, 7.7)
pt[1][1] = (1.2, 2.1)
pt[1][2] = (12.0, 127.0)
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would it be


 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or would it have to be more explicit like


etc
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're doing great.

Just one thing, always indent arrays of arrays that way, as otherwise you can get lost very quickly


[edit] No. Correct way is a first way you showed, why? Because instances being created with keyword new. And you need instances of MyPoint in the array of the same name.

Now think how would you move forward with that. I see you got loop to iterate over the array. Think of what methods you were creating in MyPoint class to get x and y.
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its giving me an error for 27.5
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:its giving me an error for 27.5
Check carefully, aren't you missing "," before 27.5?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I'm not giving away too much to you in speedy way. I could be penalized for that by seniors

Try to cary on yourself, once you finish, post everything here, so we could see if you got it right.

Note: you created methods getX() and getY() because of a purpose
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now its telling me

Array constants can only be used in initializers

as an error message when i add the elements in the pointMatrix
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:now its telling me

Array constants can only be used in initializers

as an error message when i add the elements in the pointMatrix
It is not saying that. Errors messages issued by compilers usually are more explicit and telling more details. Usually you can see the line number where the error occured and some other useful accurate information.

You got something differently from what I showed earlier. Check carefully.
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for



it says
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Array constants can only be used in initializers

at Point.Lab12.main(Lab12.java:9)
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I broke the long lines to show you how much easier it is to read, only Liutauras has shown you already.
You can write
int[] numbers = new int[]{1, 2, 3, 4, 5};

or similar. In your case you need to double the [] and double the {}.
You can also do this:-
int[] numbers = new int[]{1, 2, 3, 4, 5};

int[] numbers = {1, 2, 3, 4, 5};

You are allowed to miss out the new XVZ[] bit, but only when you have the declaration and initialisation on the same line.

And welcome again
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you initializing array twice.

By writing
A[][] a = new A[2][3];
Then
a = {{}, {}};

You need to choose which way you want to populate elements. If you declare array first, then you'll need loop or manually specifying array indeces initialize each element.
Or if you choose second way, then initialization A[][] a = new A[2][3] is not needed.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1st way:
A[] a = new A[1];
a[0] = new A(...);

2nd way:
A[] a = {new A(...)};
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so now i have


 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was mentioned about keyword new? How instances a being created? new MyPoint(4.3, 4.4);
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay! I see my problem. I now have

 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see now that my for loop for printing the array is incorrect. can you tell me what i have done wrong?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:I see now that my for loop for printing the array is incorrect. can you tell me what i have done wrong?

MyPoint array element at the possition pointMatrix[i][j] is an instance of class MyPoint. In that class you have methods to get x and y. I told earlier, that these are on purpose created, in fact you did that. So you need to use them now in order to get x and y.
Check your lecture slides for a reference how to do that. All techniques you need to solve that exercise suppose to be taught already. Also check your notes, you might have something useful written there.

As an advise, always try to look up information in books when you don't know something, once you find relevant part - read all chapter to get deeper and wider understanding. Usually it turns out there is more important information you were not aware of. Books could be found at school library. This is an old way of looking information (I still prefer that way if it is an option). In nowadays many people prefer to use a web as a primary information source, but there is a big mess, you need to know exactly what you're looking for. Books are more trustable source.
 
naomi newman
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay i will try, thank you for all your help today
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naomi newman wrote:okay! I see my problem. I now have

You can do it by setting the individual elements of the arrays, but you will soon see that an array initialiser like that is much quicker to write. Also no risk of getting the indices out of bounds.
Naomi Newman wrote:I see now that my for loop for printing the array is incorrect. can you tell me what i have done wrong?
If you look in Java Puzzlers by Bloch and Gafter, it has a question about how to print such an array. I actually got that one right when I first saw it. Use System.out.println(this method);. You should also override the toString() method to return something you can understand, e.g.
[Point: x=123, y=456]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!