import structure.*; import java.io.*; /** * The individual creatures in the world are all representatives of some * species class and share certain common characteristics, such as the species * name and the program they execute. Rather than copy this information into * each creature, this data can be recorded once as part of the description for * a species and then each creature can simply include the appropriate species * pointer as part of its internal data structure. *
* * To encapsulate all of the operations operating on a species within this * abstraction, this provides a constructor that will read a file containing * the name of the creature and its program, as described in the earlier part * of this assignment. To make the folder structure more manageable, the * species files for each creature are stored in a subfolder named Creatures. * This, creating the Species for the file Hop.txt will causes the program to * read in "Creatures/Hop.txt". * *
* * Note: The instruction addresses start at one, not zero. */ public class Species { /** * Create a species for the given file. @pre fileName exists in the Creature * subdirectory. */ public Species(String fileName) { // the try { ... } catch(...) { ... } statement handles // errors in opening. Do not worry about the details- // just insert code where indicated. try { ReadStream input = new ReadStream( new FileInputStream( "Creatures" + java.io.File.separator + fileName)); // insert code to read from Creatures file here (use readLine() ) } catch (IOException e) { Assert.fail(e.toString()); } } /** * Return the name of the species. */ public String getName() { Assert.fail("Implement me."); return null; } /** * Return the color of the species. */ public String getColor() { Assert.fail("Implement me."); return null; } /** * Return the number of instructions in the program. */ public int programSize() { Assert.fail("Implement me."); return -1; } /** * Return an instruction from the program. @pre 1 <= i <= programSize(). * @post returns instruction i of the program. */ public Instruction programStep(int i) { Assert.fail("Implement me."); return null; } /** * Return a String representation of the program. */ public String programToString() { String s = ""; for (int i = 1; i <= programSize(); i++) { s = s + (i) + ": " + programStep(i) + "\n"; } return s; } public static void main(String s[]) { Species sp = new Species("Hop.txt"); System.out.println(sp.getName()); System.out.println("first step should be hop: " + sp.programStep(1)); /* test all other operations here */ } }