0
Updating a constructor
(letâs do this again since I had the wrong code :,D) Iâm doing this project in class and here I have to update the constructor to where itâs placed randomly on a map and it doesnât take up space from another file I have within the code So how do I change a constructor to do certain actions? https://code.sololearn.com/cLbOEZuyhpHX/?ref=app
4 Answers
+ 3
Err3c If zemiak is right, then you could and should do that outside the object.
Object oriented programming is build upon conceptualisation.
Could a gift randomly place itself? Can it be self aware (aside from maybe an AI) and know its surroundings?
You should just pass the location information to the constructor. Keep a HashMap of coordinates as keys in a class handling the gifts. HashMap lookups are very fast. So you can look if a random spot is occupied before passing it to the constructor of the gift class.
+ 2
it probably means
The constructor should generate a random position for the gift image to be placed on the map image. But it must be ensured that it does not take place of another gift on the map.
The list of gifts and their positions is given in the ArrayList to the constructor via a parameter.
value is just symbol displayed on a screen,
so it should check x,y position and width, height
as problem of collision detection of multiple rectangles
like
if (rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y) {
0
What do you mean by updating the constructor? Do you mean the object?
0
HashMap is good if you want compare exactly same x,y of one other object, .but not for all objects partially overlaying new object