Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Around Line 138, insert the code (the method content) you write to determine the 'Top 3' winners. Write method (insert code) which prints top 3

Around Line 138, insert the code (the method content) you write to determine the 'Top 3' winners. Write method (insert code) which prints top 3 bug killers If # 'kills' are tied, tiebreak winner is longest survivor amongst those tied. Print only the three winners. Only modify the method's code. Do not rewrite other code outside the method.image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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, 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 = V (X1 - X2)2 + (Y1 - Y2)2 + (21 - 22)2 Print out the number and final location of the winning firefly and the number of iterations it took. ADDITIONAL MODIFICATIONS: This program will also: - be able to handle variable numbers of bugs ('competitors') - Assign a name (String) to each competitor - variable world size (cube shape) variable kill radius track the specific killer of each individual bug (firefly) - track each bug's death round number print world state at start & end, including killers & death rounds use & demonstrate some static (class) variables use & demonstrate getter and setter methods use & demonstrate Math.pow() and Math.sqrt() use & demonstrate decimal rounding algorithm use & demonstrate convient static printing methods, PC) & PLN) - use & demonstrate a debug printing option (turn off/on after bugName[] line) use all 3 APCSA primitives + String class use toString() use extensive comments use & demonstrate Object class (the mother of all classes) use & demonstrate actual and formal parameters using Array of Firefly objects - use & demonstrate limits of object class (parent) vs child class as formal parameters - example of short main method, which invokes modular methods that do the 'work' */ import java.util.*; // Imports all java util libraries import java.util. Random; // redundant... explicit reminder we need Random!! ====== =========================== ======== =========== // == // ======================= RUNNER CLASS (use public here, only ) // === public class Ch65_3D_Deathmatch_Runner5c_plus TOP3 { public static void main(String[] args) { // Name yur bugz... as many bugs as you want // total is set by the quantity of names in bugNames[] String bugNames[] {"A", "B","C", "D", "E", "F","G" ,"H" , "I","J", "K","L","M", "N","0", "P", "Q" }; // Firefly static methods (cless, not objects)... Firefly.setDebug(false); // setDebug() to set debug printing ON (true) or OFF (false) Firefly.setWorldSize(20.0); // setWorldSize() static method to set size of cube (length) Firefly.setkillRadius(1.0); // Firefly setkillRadius() static method to set kill radius int numBugs Firefly bugs [ ] = bugNames.length; // Determine how many bugs we have, numBugs, for convenience = new Firefly[numBugs]; // Create array object of length numBugs containing Fireflys for(int i=0;i worldCubeUL) tempX=worldCubeUL; // Check boundaries of cube if ( tempx worldCubeUL) tempY=worldCubeUL; // simply set the movement to the 'wall' if ( tempY worldCubeUL) tempZ=worldCubeUL; // namely, X, Y, Z if ( tempZ 1) return true; // Check the # of alive bugs; return true else return false; // if there are 2 or more remaining, else false } private double rndCoordLoc() // Compute a single (random) dimension's coord { // based on cube dimensions (LL & UL) double coord = worldCubelL + new Random() .nextDouble() * (worldCubeUL - worldCubeLL); return coord; 1/ Use standard random algorithm & return result } private double rndMoveDist() // Compute a single (random) dimension's coord { // based on movement limit double dist = -bugMoveDistLimit + new Random() .nextDouble() * (bugMoveDistLimit*2); return dist; // Use standard random algorithm & return result } public String getBugName() // This is a 'Getter' method!! // Note the use of 'this.' // Simply returns the bug's name (String) return this.bugName; } public boolean getBugAlive() { return this.bugAlive; } // This is a 'Getter' method!! // Note the use of 'this.' // Returns the alive status (boolean) public static double getWorldSize() { return worldCubeSize; } // This is a static 'Getter' method!! // Note that there is no use of 'this.' // Returns static (class) double variable public static double getkillRadius() // This is a static 'Getter' method!! // Note that there is no use of 'this.' // Returns static (class) double variable return killRadius; } public static void setWorldSize(double x) // This is a 'Setter' (Mutator) method!! { worldCubeSize = x; 1/ Copy formal parameter to the PIV worldCubeLL = -worldCubeSize/2.0; // Cut side in half to center coordinate worldCubeUL = worldCubeSize/2.0; // Max Pos cube limit return; // Return must be void !!!!!! } public static void setkillRadius (double x) // This is a 'Setter' (Mutator) method!! { killRadius = x; // This is attack radius return; // Return must be void !!!!!! } // This is a 'Setter' (Mutator) method!! public static void setDebug(boolean x) { debugon = x; return; } 1/ Turning Debug 'on' will spew more updates // Return must be void !!!!!! // Additional Getters (added in ver. 5b) public String getBugKillerName() {return bugKillerName; } public int getBugDeathRound() {return bugDeathRound; } public double getBug Locx {return bug Locx;} public double getBugLocY() {return bugLocY;} public double getBuglocz() {return bugLocZ;} public static double getWorldCubell() {return worldCubeLL;} public static double getWorldCubeUL() {return worldCubeUL;} public static double getBugMoveDistLimit() {return bugMoveDistLimit;} public static int getNumBugsTotal() {return numBugsTotal;} public static int getNumBugsAlive() {return numBugsAlive;} public static boolean getDebugon() {return debugon;} // toString public String toString() { return "name="+bugName+"\tx="+r3(bugLocx)+"\ty="+r3(bugLocY)+"\tz="+r3(bugLocZ)+"\tAlive=" +bugalive+"\tDeathRound="+bugDeathRound+"\tkiller=("+bugKillerName+")"; } Static Rounding method, to 3 decimal places // Standard n-decimal rounding algorithm; know it! private static double r3(double x) {return Math.round(x*1000.0)/1000.0;} // Static Print methods This simply shortens code, eases readability public static void P(Object arg) {System.out.print(arg); } public static void PLN(Object arg) {System.out.println(arg);} // ******** END FIREFLY CLASS ******** 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, 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 = V (X1 - X2)2 + (Y1 - Y2)2 + (21 - 22)2 Print out the number and final location of the winning firefly and the number of iterations it took. ADDITIONAL MODIFICATIONS: This program will also: - be able to handle variable numbers of bugs ('competitors') - Assign a name (String) to each competitor - variable world size (cube shape) variable kill radius track the specific killer of each individual bug (firefly) - track each bug's death round number print world state at start & end, including killers & death rounds use & demonstrate some static (class) variables use & demonstrate getter and setter methods use & demonstrate Math.pow() and Math.sqrt() use & demonstrate decimal rounding algorithm use & demonstrate convient static printing methods, PC) & PLN) - use & demonstrate a debug printing option (turn off/on after bugName[] line) use all 3 APCSA primitives + String class use toString() use extensive comments use & demonstrate Object class (the mother of all classes) use & demonstrate actual and formal parameters using Array of Firefly objects - use & demonstrate limits of object class (parent) vs child class as formal parameters - example of short main method, which invokes modular methods that do the 'work' */ import java.util.*; // Imports all java util libraries import java.util. Random; // redundant... explicit reminder we need Random!! ====== =========================== ======== =========== // == // ======================= RUNNER CLASS (use public here, only ) // === public class Ch65_3D_Deathmatch_Runner5c_plus TOP3 { public static void main(String[] args) { // Name yur bugz... as many bugs as you want // total is set by the quantity of names in bugNames[] String bugNames[] {"A", "B","C", "D", "E", "F","G" ,"H" , "I","J", "K","L","M", "N","0", "P", "Q" }; // Firefly static methods (cless, not objects)... Firefly.setDebug(false); // setDebug() to set debug printing ON (true) or OFF (false) Firefly.setWorldSize(20.0); // setWorldSize() static method to set size of cube (length) Firefly.setkillRadius(1.0); // Firefly setkillRadius() static method to set kill radius int numBugs Firefly bugs [ ] = bugNames.length; // Determine how many bugs we have, numBugs, for convenience = new Firefly[numBugs]; // Create array object of length numBugs containing Fireflys for(int i=0;i worldCubeUL) tempX=worldCubeUL; // Check boundaries of cube if ( tempx worldCubeUL) tempY=worldCubeUL; // simply set the movement to the 'wall' if ( tempY worldCubeUL) tempZ=worldCubeUL; // namely, X, Y, Z if ( tempZ 1) return true; // Check the # of alive bugs; return true else return false; // if there are 2 or more remaining, else false } private double rndCoordLoc() // Compute a single (random) dimension's coord { // based on cube dimensions (LL & UL) double coord = worldCubelL + new Random() .nextDouble() * (worldCubeUL - worldCubeLL); return coord; 1/ Use standard random algorithm & return result } private double rndMoveDist() // Compute a single (random) dimension's coord { // based on movement limit double dist = -bugMoveDistLimit + new Random() .nextDouble() * (bugMoveDistLimit*2); return dist; // Use standard random algorithm & return result } public String getBugName() // This is a 'Getter' method!! // Note the use of 'this.' // Simply returns the bug's name (String) return this.bugName; } public boolean getBugAlive() { return this.bugAlive; } // This is a 'Getter' method!! // Note the use of 'this.' // Returns the alive status (boolean) public static double getWorldSize() { return worldCubeSize; } // This is a static 'Getter' method!! // Note that there is no use of 'this.' // Returns static (class) double variable public static double getkillRadius() // This is a static 'Getter' method!! // Note that there is no use of 'this.' // Returns static (class) double variable return killRadius; } public static void setWorldSize(double x) // This is a 'Setter' (Mutator) method!! { worldCubeSize = x; 1/ Copy formal parameter to the PIV worldCubeLL = -worldCubeSize/2.0; // Cut side in half to center coordinate worldCubeUL = worldCubeSize/2.0; // Max Pos cube limit return; // Return must be void !!!!!! } public static void setkillRadius (double x) // This is a 'Setter' (Mutator) method!! { killRadius = x; // This is attack radius return; // Return must be void !!!!!! } // This is a 'Setter' (Mutator) method!! public static void setDebug(boolean x) { debugon = x; return; } 1/ Turning Debug 'on' will spew more updates // Return must be void !!!!!! // Additional Getters (added in ver. 5b) public String getBugKillerName() {return bugKillerName; } public int getBugDeathRound() {return bugDeathRound; } public double getBug Locx {return bug Locx;} public double getBugLocY() {return bugLocY;} public double getBuglocz() {return bugLocZ;} public static double getWorldCubell() {return worldCubeLL;} public static double getWorldCubeUL() {return worldCubeUL;} public static double getBugMoveDistLimit() {return bugMoveDistLimit;} public static int getNumBugsTotal() {return numBugsTotal;} public static int getNumBugsAlive() {return numBugsAlive;} public static boolean getDebugon() {return debugon;} // toString public String toString() { return "name="+bugName+"\tx="+r3(bugLocx)+"\ty="+r3(bugLocY)+"\tz="+r3(bugLocZ)+"\tAlive=" +bugalive+"\tDeathRound="+bugDeathRound+"\tkiller=("+bugKillerName+")"; } Static Rounding method, to 3 decimal places // Standard n-decimal rounding algorithm; know it! private static double r3(double x) {return Math.round(x*1000.0)/1000.0;} // Static Print methods This simply shortens code, eases readability public static void P(Object arg) {System.out.print(arg); } public static void PLN(Object arg) {System.out.println(arg);} // ******** END FIREFLY CLASS ********

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

Step: 3

blur-text-image

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