Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 5 3D Deathmatch (Java) A three dimensional random walk is where a particle starts out at some location (X, Y, Z) then moves by

Exercise 5 3D Deathmatch (Java)

A three dimensional random walk is where a particle starts out at some location (X, Y, Z) then moves by small random increments in X, Y, and Z. Think of this as a confused firefly, fluttering randomly throughout the night.

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.

Print out interesting messages as all this is being done. (It would be nice to show graphics for this, but that would be a real project.)

Use the Pythagorean formula to calculate the distance between two objects:

 _____________________________________________ distance = \/ (X1 - X2)2 + (Y1 - Y2)2 + (Z1 - Z2)2

Print out the number and final location of the winning firefly and the number of iterations it took.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions