• Post Reply Bookmark Topic Watch Topic
  • New Topic

Eliminate object from array by setting boolean to false  RSS feed

 
Wilbert Bundalian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas/ suggestions thanks so much!


This is the exercise im working on:

Write a program that defines a Firefly class that has three instance variables, X, Y, and Z. The constructor for the class initializes each variable to a random value in the range -10.0 to nearly +10.0 representing the position of the firefly. Also, include an instance variable alive that is initialized to true. The class includes a move() method that adds random amounts in the range (-1.0, +1.0) to X, Y, and Z.

Create an array of 10 firefly objects, each initialized to a random location. Now go through the array in sequence moving each firefly object randomly. Each time a firefly moves, eliminate any other firefly that is a distance of 1.0 or less from the one that moved. Do this by setting alive to false. Continue until only one firefly is left. That firefly is the winner.

I have created the Firefly and Tester class and its running. My problem is how to:

eliminate any other firefly that is a distance of 1.0 or less from the one that moved. Do this by setting alive to false. Continue until only one firefly is left. That firefly is the winner.

Thanks!

here's my code:





 
Tehmina Yaseen
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare alive as

and use while loop condition like this
 
Wilbert Bundalian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tehmina Yaseen.

The boolean alive should be initialized to true as per the exercise.
-->Also, include an instance variable alive that is initialized to true.

confused on how and where to put this in the code:

if(alive==true)
continue printing firefly
else
eliminate the firefly

and how to eliminate the firefly in the array so it will not be displayed.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use an arrayList then instead of using alive you can just delete the dead ones.  I assume zombie fireflies aren't in the picture?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just fixed your problems, then realized this is a homework assignment.  You quite possibly haven't run across ArrayList yet and aren't expected to use it.  So, you've got a few bugs   Look hard at line 39.  Figure out why distance needs to be an array.  You need another for loop.  Rewrite your distance calculation, it's correct but confusing the way you do it.  If it wuz me I'd write a method calculateDistance that took the 6 inputs and returned the distance.

Finally, I admit I'm a newbie to Java.  But I'm quickly learning that putting all your code in main() will bite you eventually.  I just spent a week and a half taking working code out of main for a few files and making methods so I could use the lessons in one big, final, hairy lesson.  I broke something, and spent a good week and a half only to find a stupid mistake (I'd changed the scale of something from just an int to a double ranging 0 to 100, and didn't realize I had a .500 Wyoming Express when I did the gun, foot, bang excersize).

Finally, from an engineering perspective, come up with an outline like:


Have main() call this routine.  Write methods for each of these steps (for the while figure out what done means).  Write more methods as needed (e.g. calculateDistance).   Now you can write test code for each method as needed, and it's easier to follow what's happening.

Finally, if using Eclipse read this.

PS.  Line 39?  It ain't right.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, just looked at the FireFly class.  x, y, and z are independent.  If move() takes any argument at all it will be the minimum and maximum amount the bug can move.  You'll be wanting to move your math from Excersize4 to move(), and come up with a new value for x, y, and z.
 
Wilbert Bundalian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim. It's not a homework, just practicing and learning java.
About ArrayList, yes haven't run across it so I really need to figure about the boolean alive.

I know Line 39 is wrong, that's where I am confused right now. I'll try to figure out about your suggestions.

Thanks
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Email is evil, I'm trying to wind down for bed and it keeps coming in.

I've been looking for a set of exercises to learn Java from.  What are you using?  I've got 40 years experience in software and am learning Java.  My haphazard method isn't working too well.
 
Wilbert Bundalian
Greenhorn
Posts: 5
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 words.  Suh weet!

I've been looking for something like this for 2 years now with no luck.

Thanks!
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going through these I see you are a beginner programmer.  From my biggest post take the stuff after "from an engineering perspective" to heart.  That mindset will work wonders, you have to learn how to break problems into smaller problems, smaller problems into smaller problems, etc.  It's how we got to the moon

I'm going to bed, I have to be up in 6 1/2 hours   (5 AM, taking a friend to the airport)
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tehmina Yaseen wrote:Declare alive as
. . .
No, that is bad advice.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wilbert Bundalian wrote:. . . if(alive==true) . . .
Never use == true or == false; both are poor style and very error‑prone. We see people who mistakenly write = and now have multiple errors for the price of one.
Never
if (b == true) ...
always
if (b) ...
Never
if (b == false) ...
always
if (!b) ...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you come across loops? It is very inelegant to initialise an array member by member; it is much easier to use a loop. You can often use a Stream as well, but that is probably too advanced for your current experience.
Don't put all your code in the main method. Don't make things static. You need different methods to create fireflies, make them move, etc. Don't call such methods from the main method. The main method shou‍ld contain one statement:-Call other code from the runGame method; since that isn't marked static, there is no temptation to mark anything else static.

Don't use Math#sqrt if you can use this method instead. I can't remember what the corresponding formula for separations in 3D is. Is it
d = √(x² + y² + z²)
where d is distance and x y z are the separations in the three directions? If so, I think you have that formula correct. Try
Math.hypot(Math.hypot(x₁ - x₂, y₁ - y₂), z₁ - z₂)
as an alternative.

The suggestion of giving the alive variable public access was bad; give all your fields private access and if you need to kill the fireflies off, give them a die() method.

It was suggested you use a List instead of an array, but the instructions seem to require an array. It is not possible to change the size of an array, so you have several possibilities:-
  • 1: Kill off the firefly and then copy the remainder of the array into a smaller array. Have a look at the System class and the Arrays class.
  • 2: Kill off the firefly and make it unresponsive to calls, which you can do with if-else statements or the ?: operator.
  • Find out about the standard Java® way of printing objects with the toString method. You can have a different value returned from a live firefly and a dead firefly.

    Don't replace the element in the array with null; that can cause all sorts of other problems.
     
    Wilbert Bundalian
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Campbell
    Will try to edit main to make it short and simple
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!