Java code tasks: Please help to create a main class ( class Panic ) of the progr
ID: 3740462 • Letter: J
Question
Java code tasks:
Please help to create a main class (class Panic) of the program called "Panic". Here is an introduction.
Here is the link to an introduction and some samples of this program. Please open the link: https://paste.ee/p/y2BqB
TASKS:
class Panic
This is the main class. The name of the game is Panic!
Methods
public static void main(String[] args).We will actually use the args now. We're expecting one or two arguments, which can be supplied when you invoke the program in one of these two ways:
or,
The first argument must be present, and must be a filename of a map to read.
The second argument might be present, describing the "logger" - this is where we will send messages describing what's happening on the map. If this argument is present, it is the name of a file that should be opened and all log messages written to it, and then closed. (Any file previously present with that name is overwritten, not appended). If there is no second argument, then System.out must be used by default, and all logging messages will show up on the console (this is convenient to use during development).
Note: We should use the PrintStream constructor, PrintStream(File), when preparing log to write to a file. Why did we use PrintStream as the type of log, and not the more fashionable PrintWriter (which handles different encodings better)? Because we wanted to allow either writing to the console (via PrintStream System.out, stuck in the old Java 1.0 mindset) or to an arbitrary file (perhaps via a PrintWriter), we needed a common type to be used in either situation. We might find that the newer PrintWriter has a constructor that accepts an OutputStream (ancestor to PrintStream), which seems promising - but creating a PrintWriter based on System.out causes competition for the console, between System.out and your PrintWriter log. Competition for these sorts of resources is problematic. This is also why you should never create multiple scanners that read from the same source (such as System.in or a particular File object), and instead you should create one and pass it around as an argument. So for this assignment, we chose instead to use PrintStream, which contains the constructor PrintStream(File).
Once we understand what map file to read (1st argument) and where to write log messages (2nd argument), this method will create a Map object. To start the simulation, we send messages to the logger including both the 'begin' message, a listing of any things at their locations (two included in this example), and the current map:
Repeatedly, we print a message indicating the iteration number (starting at zero), and ask the map to iterate() once through all its things. Each successive line would show during a simulation, with many other lines between them of course:
…
when no escaping people remain (all are safe or dead), the simulation is over, and we send the message:
If it refers to a file, close() the log before returning. (You shouldn't close the log if it is actually System.out!)
Please help if you can. I would greatly appreciate your helps alot.
Explanation / Answer
You need to give atleast one argument, if you give one argument the log will be treated as sysout statements and they will get printed. If you give the log file as input the program will redirect the out put to the file.
Also if you are using a log file, then the file is closed.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
public class Panic {
public static void main(String[] args) throws IOException {
PrintStream console = System.out;
boolean logfile = false;
if(args.length<1){
System.out.println("Usage : java Panic "samples/mapfile.txt" "samples/outputs.txt"");
console = System.out;
System.setOut(console);
}else if(args.length==1){
System.out.println("No Logger File is not mentioned");
System.out.println("Usage : java Panic "samples/mapfile.txt" "samples/outputs.txt"");
console = System.out;
System.setOut(console);
}else if(args.length==2){
File oup = new File(args[1]);
FileOutputStream fos = new FileOutputStream(oup);
console = new PrintStream(fos);
System.setOut(console);
logfile = true;
}
File inp = new File(args[0]);
BufferedReader br = new BufferedReader(new FileReader(inp));
System.out.println("begin simulation");
int k=0;
while((br.readLine())!=null){
System.out.println("Iteration:"+k);
k++;
}
System.out.println("end simulation");
br.close();
if(logfile){
console.close();
} }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.