java /** * Writes the simulationParameters to the file named filename. * The for
ID: 3715664 • Letter: J
Question
java
/**
* Writes the simulationParameters to the file named filename.
* The format of the file is the name of the parameter and value
* on one line separated by =. The order of the lines does not matter.
* Algorithm:
* Open the file named filename for writing. Any IOExceptions should be handled with a throws
* clause and not a try-catch block.
* For each of the simulation parameters whose names are found in Config.SIM_PARAMS
* Write out the name of the parameter, =, the parameter value and then newline.
* Close the file.
*
* Example contents of file:
* seed=233
* ocean_width=20
* ocean_height=10
* starting_fish=100
* starting_sharks=10
* fish_breed=3
* sharks_breed=10
* sharks_starve=4
*
* @param simulationParameters The values of the parameters to write out.
* @param filename The name of the file to write the parameters to.
*/
public static void saveSimulationParameters(int[] simulationParameters, String filename) throws IOException {
// TODO
}
/**
* This loads the simulation parameters from the file named filename.
* The names of the parameters are in the Config.SIM_PARAMS array and the array returned from
* this method is a parallel array containing the parameter values. The name corresponds to
* the value with the same index.
* Algorithm:
* Try to open filename for reading. If the FileNotFoundException is thrown print the
* message printing out the filename without < > and return null;
*
* File not found: <filename>
*
* Read lines from the file as long as each line contains "=". As soon as a line does not
* contain "=" then stop reading from the file. The order of the lines in the
* file is not significant.
* In a line the part before "=" is the name and the part after is the value.
* The separate method you wrote in P7 is helpful here.
* Find the index of the name within Config.SIM_PARAMS (call indexForParam).
* If the index is found then convert the value into an int and store in the corresponding
* index in the array of int that will be returned from this method.
* If the index is not found then print out the message followed by the entire line
* without the < >.
*
* Unrecognized: <line>
*
* @param filename The name of the from which to read simulation parameters.
* @return The array of parameters.
*/
public static int[] loadSimulationParameters(String filename) {
int[] params = null;
// TODO
}
/**
* This writes the simulation parameters and the chart of the simulation to a file.
* If simulationParameters is null or history is null then print an error message
* and leave the method before any output.
* If filename cannot be written to then this method should throw an IOException. *
*
* Parameters are written first, 1 per line in the file. Use an = to separate the
* name from the value. Then write a blank line and then the Population Chart.
* Example file contents are:
* seed=111
* ocean_width=5
* ocean_height=2
* starting_fish=6
* starting_sharks=2
* fish_breed=3
* sharks_breed=3
* sharks_starve=3
*
* Population Chart
* Numbers of fish(.) and sharks(O) in units of 1.
* F 6,S 2 1)OO....
* F 4,S 2 2)OO..
* F 2,S 4 3)..OO
* F 1,S 4 4).OOO
* F 0,S 4 5)OOOO
*
* Looking at one line in detail
* F 6,S 2 1)OO....
* ^^^^^^ 6 fish (the larger of sharks or fish is in the background)
* ^^ 2 sharks
* ^^^^^ chronon 1
* ^^^^ the number of sharks
* ^^^^ the number of fish
*
* The unit size is determined by dividing the maximum possible number of a creature
* (oceanWidth * oceanHeight) by Config.POPULATION_CHART_WIDTH.
* Then iterate through the history printing out the number of fish and sharks.
* PrintWriter has a printf method that is helpful for formatting.
* printf("F%3d", 5)
* prints "F 5", a 5 right justified in a field of size 3.
*
* @param simulationParameters The array of simulation parameter values.
* @param history Each element in the ArrayList is an array with information about a specific
* chronon. The array has 3 elements: chronon, number of fish, and number of sharks,
* in that order.
* @param oceanWidth The width of the ocean.
* @param oceanHeight The height of the ocean.
* @param filename The name of the file to write the parameters and chart to.
*/
public static void savePopulationChart(int[]simulationParameters, ArrayList<int[]> history,
int oceanWidth, int oceanHeight, String filename) throws IOException {
//TODO Milestone 3
}
Explanation / Answer
here is your three functions : -------------->>>>>>>>>>>
import java.io.*;
import java.util.Scanner;
public static void saveSimulationParameters(int[] simulationParameters, String filename) throws IOException {
// TODO
PrintWriter pw = new PrintWriter(new File(filename));
for(int i = 0;i<simulationParameters.length;i++){
pw.println(Config.SIM_PARAMS[i]+"="+simulationParameters[i]);
}
pw.close();
}
public static int[] loadSimulationParameters(String filename) {
int[] params = null;
params = new int[config.SIM_PARAMS.length];
try{
int st = 0;
Scanner sc = new Scanner(new File(filename));
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] arr = line.trim().split("=");
if(arr.length != 2){
break;
}
st = 0;
for(int i = 0;i<config.SIM_PARAMS.length;i++){
if(config.SIM_PARAMS[i].equals(arr[0])){
st = 1;
params[i] = Integer.parseInt(arr[1]);
break;
}
}
if(st == 0){
System.out.println("Unrecognized : "+line);
}
}
sc.close();
}catch(FileNotFoundException e){
System.out.println("File Not Found : "+filename);
return params;
}
return params;
// TODO
}
public static void savePopulationChart(int[]simulationParameters, ArrayList<int[]> history,int oceanWidth, int oceanHeight, String filename) throws IOException {
//TODO Milestone 3
if(simulationParameters == null || history == null){
System.out.println("Null Pointer Exception ");
return;
}
saveSimulationParameters(simulationParameters,filename);
PrintWriter pw = new PrintWriter(new File(filename));
File file =new File(filename);
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println();
for(int i = 0;i<history.size();i++){
int[] arr = history.get(i);
if(arr.length != 3){
continue;
}
pw.printf("F%3d",arr[1]);
pw.printf(",S%3d",arr[2]);
pw.printf("%3d)",arr[0]);
for(int j = 0;j<arr[2];j++){
pw.print("O");
}
for(int j = 0;j<arr[1];j++){
pw.print(". ");
}
}
bw.close();
pw.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.