Updating a constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th Mar 2022, 7:44 PM
Err3c
Err3c - avatar
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.
29th Mar 2022, 11:45 PM
Mustafa A
Mustafa A - avatar
+ 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) {
29th Mar 2022, 11:02 PM
zemiak
0
What do you mean by updating the constructor? Do you mean the object?
29th Mar 2022, 10:06 PM
Mustafa A
Mustafa A - avatar
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
30th Mar 2022, 6:37 AM
zemiak