/** * Models a zombie in a simulation where each zombie is a given distance from * the origin location (0,0) and moves at its given speed towards that location. */ class Zombie { //instance variables private String name private double distance private int speed //constructor public Zombie(String name, int x, int y, int speed) //accessors methods public String getName() public double getDistance() public int getSpeed() /** Reduces this zombie's distance by its speed, to a min of 0 distance. */ public void move() } /** * A zombie simulation in which a human is located at the origin (0,0) of a * coordinate plane and is surrounded by various zombies (as specified in * an input file). Each round, the zombies close in on the human while the * human can pick off one zombie at a time once it enters a killzone range. * Ultimately, the human either survives or fails (is eaten). */ class ZombieSim { //instance variables private ArrayList zombies private int killzone /** * Constructs a zombie simulation by loading the given file of zombies * and using the given killzone radius. * * @throws FileNotFoundException if the file does not exist * @throws InputMismatchException if the file has the wrong format */ public ZombieSim(String inputFilename, int radius) throws FileNotFoundException, InputmismatchException /** * Runs this simulation, returning a String containing the details * and outcome of the simulation run */ public String run() /* More specifically: count starts at 0 each round: increment count (so to 1 first time through) nearest = getNearestZombie() if nearest in killzone range: //human takes a shot if possible remove nearest from zombies list (append to output any details of a zombie kill) if isHumanSafe(): return finished output: SURVIVED for each zombie in list: //all zombies move in towards human zombie.move() if isHumanDead(): return finished output: FAIL */ /** * Returns the zombie closest to the human. * (That is, the zombie with the smallest distance) */ private Zombie getNearestZombie() /** * Returns whether at least one zombie in this sim has reached the * human and so has a distance of 0. */ private boolean isHumanDead() /** * Returns true if there are no zombies left in this sim with a positive * speed. */ private boolean isHumanSafe() } /** * Provides a UI front-end to a Zombie simulation. */ public class UsernameA17 { /** * Runs a zombie simulations with details specified on the command line. * * If 0 or 1 command line arguments are given, prints an error/usage message. * Otherwise, determines which of the first two args is the killzone and * and which is the name of the input file containing zombie definitions. * Then runs the sim with the given details and prints the resulting output * to the screen. * * If a third argument is given, treats this as a filename and appends the * same output as printed to the screen to the end of that file. */ public static void main(String[] args) /* Implementation details: processes the command line arguments (if correct) to get: int killzone String inputFilename String outputFilename (if 3 args given) otherwise, will print appropriate error message and return. Then: sim = new ZombieSim(inputFilename, killzone) output = sim.run() print output if there is an outputFilename: appendToFile(outputFilename, output) */ /** * Opens the given outputFile and appends the given contents * to the end of the file. */ public static appendToFile(String outputfile, String contents) }