Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVAAAAAAA - Help me write/edit bolded lines!!!!!! public class WaTor { /** * This is the main method for WaTor simulation. * Based on: http://home.cc.gatech.edu/biocs1/uploads/2/wator_dewdney.pdf

JAVAAAAAAA

- Help me write/edit bolded lines!!!!!!

public class WaTor { /** * This is the main method for WaTor simulation. * Based on: http://home.cc.gatech.edu/biocs1/uploads/2/wator_dewdney.pdf * This method contains the main simulation loop. In main the Scanner * for System.in is allocated and used to interact with the user. * * @param args (unused) */ public static void main(String[] args) {

//scanner and random number generator for use throughout Scanner input = new Scanner(System.in); Random randGen = new Random();

//values at the same index in these parallel arrays correspond to the //same creature //a value equal or greater than 0 at a location indicates a fish of //that age at that location. int[][] fish = null; //true at a location indicates that the fish moved during the current //chronon boolean[][] fishMoved = null;

//a value equal or greater than 0 at a location indicates a shark of //that age at that location int[][] sharks = null; //true at a location indicates that the shark moved during the current //chronon boolean[][] sharksMoved = null; //a value equal or greater than 0 indicates the number of chronon //since the shark last ate. int[][] starve = null;

//an array for simulation parameters //to be used when saving or loading parameters in Milestone 3 int[] simulationParameters = null; //welcome message System.out.println("Welcome to Wa-Tor");

//Ask user if they would like to load simulation parameters from a file. //If the user enters a y or Y as the only non-whitespace characters //then prompt for filename and call loadSimulationParameters //TODO in Milestone 3 //prompts the user to enter the simulation parameters if ( simulationParameters == null) { simulationParameters = new int[Config.SIM_PARAMS.length]; for ( int i = 0; i < Config.SIM_PARAMS.length; i++) { System.out.print("Enter " + Config.SIM_PARAMS[i] + ": "); simulationParameters[i] = input.nextInt(); } input.nextLine(); //read and ignore remaining newline } //if seed is > 0 then set the random number generator to seed if (simulationParameters[indexForParam("seed")] > 0) { randGen.setSeed(simulationParameters[indexForParam("seed")]); } //save simulation parameters in local variables to help make code //more readable. int oceanWidth = simulationParameters[indexForParam("ocean_width")]; int oceanHeight = simulationParameters[indexForParam("ocean_height")]; int startingFish = simulationParameters[indexForParam("starting_fish")]; int startingSharks = simulationParameters[indexForParam("starting_sharks")]; int fishBreed = simulationParameters[indexForParam("fish_breed")]; int sharksBreed = simulationParameters[indexForParam("sharks_breed")]; int sharksStarve = simulationParameters[indexForParam("sharks_starve")];

//create parallel arrays to track fish and sharks fish = new int[oceanHeight][oceanWidth]; fishMoved = new boolean[oceanHeight][oceanWidth]; sharks = new int[oceanHeight][oceanWidth]; sharksMoved = new boolean[oceanHeight][oceanWidth]; starve = new int[oceanHeight][oceanWidth];

//make sure fish, sharks and starve arrays are empty (call emptyArray) //TODO Milestone 1 emptyArray(fish); emptyArray(sharks); emptyArray(starve);

//place the initial fish and sharks and print out the number //placed int numFish = 0; int numSharks = 0; //TODO Milestone 1 numFish = placeFish(fish, startingFish, fishBreed, randGen); numSharks = placeSharks(fish, sharks, startingSharks, sharksBreed, randGen); System.out.println("Placed " + numFish + " fish." ); System.out.println("Placed " + numSharks + " sharks.");

int currentChronon = 1; //simulation ends when no more sharks or fish remain boolean simulationEnd = numFish <= 0 || numSharks <= 0; while ( !simulationEnd){

//print out the locations of the fish and sharks //TODO Milestone 1 showFishAndSharks(currentChronon, fish, sharks); //CHECK - MOVED

//prompt user for Enter, # of chronon, or 'end' //Enter advances to next chronon, a number //entered means run that many chronon, (TODO Milestone 2), //'end' will end the simulation System.out.print("Press Enter, # of chronon, or 'end': "); String response = input.nextLine().trim(); if (response.equalsIgnoreCase("end")) { break; //leave simulation loop } //clear fishMoved and sharksMoved from previous chronon //TODO Milestone 1 fishMoved = null; sharksMoved = null; //call fishSwimAndBreed //TODO Milestone 2 //call sharksHuntAndBreed //TODO Milestone 2 //increment current chronon and count the current number of fish and sharks //TODO Milestone 1 currentChronon++; numFish = countCreatures(fish); numSharks = countCreatures(sharks);

//if all the fish or sharks are gone then end simulation simulationEnd = numFish <= 0 || numSharks <= 0; }; //print the final ocean contents //TODO Milestone 1 //Print out why the simulation ended. if ( numSharks <= 0 ) { System.out.println("Wa-Tor simulation ended since no sharks remain."); } else if ( numFish <= 0 ){ System.out.println("Wa-Tor simulation ended since no fish remain."); } else { System.out.println("Wa-Tor simulation ended at user request."); }

//If the user was prompted to enter simulation parameters //then prompt the user to see if they would like to save them. //If the user enters a y or Y as the only non-whitespace characters //then prompt for filename and save, otherwise don't save parameters. //call saveSimulationParameters to actually save the parameters to the file. //If saveSimulationParameters throws an IOException then catch it and //repeat the code to prompt asking the user if they want to save //the simulation parameters. //TODO Milestone 3 //Always prompt the user to see if they would like to save a //population chart of the simulation. //If the user enters a y or Y as the only non-whitespace characters //then prompt for filename and save, otherwise don't save chart. //call savePopulationChart to save the parameters to the file. //If savePopulationChart throws an IOException then catch it and //repeat the code to prompt asking the user if they want to save //the population chart. //TODO Milestone 3

input.close(); } /** * This is called when a fish cannot move. This increments the fish's age and notes in * the fishMove array that it has been updated this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param row The row of the fish that is staying. * @param col The col of the fish that is staying. */ public static void aFishStays(int[][] fish, boolean[][] fishMove, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG fish %d,%d stays ", row, col); } fish[row][col]++; // increment age of fish fishMove[row][col] = true; } /** * The fish moves from fromRow,fromCol to toRow,toCol. The age of the fish is incremented. The * fishMove array records that this fish has moved this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the fish is moving from. * @param fromCol The column the fish is moving from. * @param toRow The row the fish is moving to. * @param toCol The column the fish is moving to. */ public static void aFishMoves(int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG fish moved from %d,%d to %d,%d ", fromRow, fromCol, toRow, toCol); } // just move fish fish[toRow][toCol] = fish[fromRow][fromCol] + 1; // increment age fishMove[toRow][toCol] = true;

// clear previous location fish[fromRow][fromCol] = Config.EMPTY; fishMove[fromRow][fromCol] = false; } /** * The fish moves from fromRow,fromCol to toRow,toCol. This fish breeds so its * age is reset to 0. The new fish is put in the fromRow,fromCol with an age of 0. The * fishMove array records that both fish moved this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the fish is moving from and where the new fish is located. * @param fromCol The column the fish is moving from and where the new fish is located. * @param toRow The row the fish is moving to. * @param toCol The column the fish is moving to. */ public static void aFishMovesAndBreeds(int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG fish moved from %d,%d to %d,%d and breed ", fromRow, fromCol, toRow, toCol); } // move fish, resetting age in new location fish[toRow][toCol] = 0; fishMove[toRow][toCol] = true;

// breed fish[fromRow][fromCol] = 0; // new fish in previous location fishMove[fromRow][fromCol] = true; }

/** * This removes the shark from the sharks, sharksMove and starve arrays. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param row The row the shark is in. * @param col The column the shark is in. */ public static void sharkStarves(int[][] sharks, boolean[][] sharksMove, int[][] starve, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG shark %d,%d starves ", row, col); } sharks[row][col] = Config.EMPTY; starve[row][col] = Config.EMPTY; sharksMove[row][col] = false; } /** * This is called when a shark cannot move. This increments the shark's age and time since * the shark last ate and notes in the sharkMove array that it has been updated this chronon. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param row The row the shark is in. * @param col The column the shark is in. */ public static void sharkStays(int[][] sharks, boolean[][] sharksMove, int[][] starve, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG shark %d,%d can't move ", row, col); } sharks[row][col]++; // increment age of shark starve[row][col]++; // increment time since last ate sharksMove[row][col] = true; } /** * This moves a shark from fromRow,fromCol to toRow,toCol. This increments the age and time * since the shark last ate and notes that this shark has moved this chronon. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param fromRow The row the shark is moving from. * @param fromCol The column the shark is moving from. * @param toRow The row the shark is moving to. * @param toCol The column the shark is moving to. */ public static void sharkMoves(int[][] sharks, boolean[][] sharksMove, int[][] starve, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG shark moved from %d,%d to %d,%d ", fromRow, fromCol, toRow, toCol); } // just move shark sharks[toRow][toCol] = sharks[fromRow][fromCol] + 1; // move age sharksMove[toRow][toCol] = true; starve[toRow][toCol] = starve[fromRow][fromCol] + 1;

sharks[fromRow][fromCol] = Config.EMPTY; sharksMove[fromRow][fromCol] = false; starve[fromRow][fromCol] = 0; }

/** * The shark moves from fromRow,fromCol to toRow,toCol. This shark breeds so its * age is reset to 0 but its time since last ate is incremented. * The new shark is put in the fromRow,fromCol with an age of 0 and 0 time since last ate. The * fishMove array records that both fish moved this chronon. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param fromRow The row the shark is moving from. * @param fromCol The column the shark is moving from. * @param toRow The row the shark is moving to. * @param toCol The column the shark is moving to. */ public static void sharkMovesAndBreeds(int[][] sharks, boolean[][] sharksMove, int[][] starve, int fromRow, int fromCol, int toRow, int toCol) {

if (Config.DEBUG) { System.out.printf("DEBUG shark moved from %d,%d to %d,%d and breeds ", fromRow, fromCol, toRow, toCol); } sharks[toRow][toCol] = 0; // reset age in new location sharks[fromRow][fromCol] = 0; // new fish in previous location

sharksMove[toRow][toCol] = true; sharksMove[fromRow][fromCol] = true;

starve[toRow][toCol] = starve[fromRow][fromCol] + 1; starve[fromRow][fromCol] = 0; }

/** * The shark in fromRow,fromCol moves to toRow,toCol and eats the fish. The sharks age is * incremented, time since it last ate and that this shark moved this chronon are noted. * The fish is now gone. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the shark is moving from. * @param fromCol The column the shark is moving from. * @param toRow The row the shark is moving to. * @param toCol The column the shark is moving to. */ public static void sharkEatsFish(int[][] sharks, boolean[][] sharksMove, int[][] starve, int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG shark moved from %d,%d and ate fish %d,%d ", fromRow, fromCol, toRow, toCol); } // eat fish fish[toRow][toCol] = Config.EMPTY; fishMove[toRow][toCol] = false;

// move shark sharks[toRow][toCol] = sharks[fromRow][fromCol] + 1; // move age sharksMove[toRow][toCol] = true; starve[toRow][toCol] = starve[fromRow][fromCol] = 0;

// clear old location sharks[fromRow][fromCol] = Config.EMPTY; sharksMove[fromRow][fromCol] = false; starve[fromRow][fromCol] = 0; }

/** * The shark in fromRow,fromCol moves to toRow,toCol and eats the fish. The fish is now gone. * This shark breeds so its age is reset to 0 and its time since last ate is incremented. * The new shark is put in the fromRow,fromCol with an age of 0 and 0 time since last ate. * That these sharks moved this chronon is noted. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the shark is moving from. * @param fromCol The column the shark is moving from. * @param toRow The row the shark is moving to. * @param toCol The column the shark is moving to. */ public static void sharkEatsFishAndBreeds(int[][] sharks, boolean[][] sharksMove, int[][] starve, int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG shark moved from %d,%d and ate fish %d,%d and breed ", fromRow, fromCol, toRow, toCol); } // shark eats fish and may breed // eat fish fish[toRow][toCol] = Config.EMPTY; fishMove[toRow][toCol] = false;

// move to new location sharks[toRow][toCol] = 0; // reset age in new location sharksMove[toRow][toCol] = true; starve[toRow][toCol] = 0;

// breed sharks[fromRow][fromCol] = 0; // new shark in previous location sharksMove[fromRow][fromCol] = true; starve[fromRow][fromCol] = 0; }

/** * This sets all elements within the array to Config.EMPTY. * This does not assume any array size but uses the .length attribute of the array. * If arr is null the method prints an error message and returns. * * @param arr The array that only has EMPTY elements when method has executed. */ public static void emptyArray(int[][] arr) { if ( arr == null) { System.out.println("emptyArray arr is null"); return; } for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = Config.EMPTY; } } }

/** * This sets all elements within the array to false, indicating not moved this chronon. * This does not assume any array size but uses the .length attribute of the array. * If arr is null the method prints a message and returns. * * @param arr The array will have only false elements when method completes. */ public static void clearMoves(boolean[][] arr) { if ( arr == null) { System.out.println("clearMoves arr is null"); return; } for (int row = 0; row < arr.length; row++) { for (int col = 0; col < arr[row].length; col++) { arr[row][col] = false; } } } /** * Shows the locations of all the fish and sharks noting a fish with Config.FISH_MARK, * a shark with Config.SHARK_MARK and empty water with Config.WATER_MARK. * At the top is a title "Chronon: " with the current chronon and at the bottom is a count * of the number of fish and sharks. * Example of a 3 row, 5 column ocean. Note every mark is also followed by a space. * Chronon: 1 * O . . * O . . . * . . O * fish:7 sharks:3 * * @param chronon The current chronon. * @param fish The array containing all the ages of all the fish. * @param sharks The array containing all the ages of all the sharks. */ public static void showFishAndSharks(int chronon, int[][] fish, int[][] sharks) { // TODO Milestone 1

-----------------------------------------------------------------------------

public class Config { /** * Character constants used to show what is in each ocean location. * Use the constants in your code, do not use the literals (e.g., '.', 'O') * in your code. */ final static char FISH_MARK = '.'; final static char SHARK_MARK = 'O'; final static char WATER_MARK = ' '; /** * Constant to initialize all the empty locations in the fish, * shark and starve arrays. */ final static int EMPTY = -1;

/** * The maximum attempts to try to randomly place a fish or shark. */ final static int MAX_PLACE_ATTEMPTS = 5; /** * The width of the population chart. */ final static int POPULATION_CHART_WIDTH = 50; /** * The number of history indexes and then the constants for the * indexes into the history array for chronon, fish and shark numbers. */ final static int NUM_HISTORY_ITEMS = 3; final static int HISTORY_CHRONON_INDEX = 0; final static int HISTORY_NUM_FISH_INDEX = 1; final static int HISTORY_NUM_SHARKS_INDEX = 2; /** * Names of the simulation parameters. Within the program a * parallel int array will have the corresponding values. The same * index to both arrays will refer to the name and value of * a single parameter. */ final static String [] SIM_PARAMS = { "seed", "ocean_width", "ocean_height", "starting_fish", "starting_sharks", "fish_breed", "sharks_breed", "sharks_starve" }; /** * Checked within if statements to see whether to print out * debug information or not. Set to true and run the program * to see debug output. */ final static boolean DEBUG = false; }

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

Students also viewed these Databases questions

Question

i need 5 5 7 . .

Answered: 1 week ago

Question

Develop successful mentoring programs. page 400

Answered: 1 week ago