Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * This writes the simulation parameters and the chart of the simulation to a

ID: 3717343 • Letter: #

Question

/**
* 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.
*
* The unit size is determined by dividing the maximum possible number of a creature (oceanWidth
* X 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 {

How do I go about this...

Explanation / Answer

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();
}