Practical Weeks 3, 4, 5 – Java CS2JA11.
Added on - 19 Sep 2019
Practical Weeks 3, 4, 5 – Java CS2JA11Prof Richard MitchellJava CS2JA11 – Practical Weeks 3,4,5Artificial Life SimulatorOver the next few weeks you will develop the code to simulate a two dimensional world in whichdifferent types of artificial life exist and move around, and these are displayed in a text consoleinterface. During Week 6 you will demonstrate your program. After Week 6, you will first extend thecode, adding more inheritance, file commands etc., after which you develop a similar world, but displayit using a Graphical User Interface (GUI), and that will form the basis of the module’s first major pieceof coursework, worth 40% of the marks for the module.The assignment has been structured so that, assuming you program in the correct manner, that much ofthe code you write during these practicals can be used with minimal change in the major coursework.This sheet describes what you will do for the text console, with specific tasks in the different weeks.Structure of SimulatorThe code you will demonstrate in Week 6 should have at least three classes: class AnEntity which hasthe attributes and methods for an entity in the world (this could be a life form, food or an obstacle); classAWorld which has the attributes and methods for the world in which the entities exist; and classAnInterface which provides the textual interface which allows the user to enter commands and ontowhich the world and relevant information are displayed to the user.This structure means that, for the major coursework where a GUI is used, much of AnEntity andAWorld will still be valid.Similar Example - RobotTo illustrate the structure, please see the classes Robot, RobotArena and RobotInterface which show aconsole interface to an arena in which multiple robots exist.The Robot class has the position of the robot (ints x,y) and the RobotArena variable which indicates thearena in which the robot exists. This should be readily adaptable for class AnEntity which will need toknow about the world in which it exists.The robot has a constructor, which is passed its position and the arena, and methods which return itsposition, checks whether the robot is at a given position, and it has a method to display itself, which ispassed an argument specifying the interface in which it is shown, and the method just calls theassociated method in the interface which displays the robot. The robot therefore does not need to knowdetails about the interface.The Robot class has a main function, which allows the class to be tested on its own (showing forinstance how to have an array of robots. It uses scanner as a means whereby numbers can be entered,rather than using JOptionPane. The final program uses the main function in RobotInterface.The RobotArena class has the x,y size of the arena and an array of robots: the constructor defines thearena and sets up the array to have up to maxRobots robots. It has a function to add a robot, whichrepeatedly generates random x,y numbers until a pair is found where there is no robot, and adds therobot there. It calls the getRobotAt method which tries to find a robot at a given position. The finalmethod showRobots, is passed the robot interface and calls functions to display each robot there.Department of Computer Science1
Practical Weeks 3, 4, 5 – Java CS2JA11Prof Richard MitchellWeek 3 Tasks – classes AnEntity and AWorldClass AnEntityThe class AnEntity should be represented by the attributes : species (String), a symbol (char), horizontalposition (int), vertical position (int), energy (int), a unique ID (int), etc. The symbol is used to representthe bug and can be the first character of the species name if not otherwise specified – and it will be usedto display the entity in the world.It should have at least two constructors (one with no arguments), methods to get and set the attributesand the methods toString and toText. The null argument constructor should initialise all attributes withsuitable default values (for Strings provide a dummy string); the other should be passed values of all theattributes The method toString should return the species and its position as a String, whereas toTextshould return all attributes concatenated as a single String.The class should also have a main() method which you will use to test your code.Task 3.1Create a new class AnEntity within an existing package (or create a new package for thisproject if you want) and write the constructor and the other methods.Task 3.2To test it, in the main() function:Ask the user to enter suitable values for an entity (species, position etc)Create an object of type AnEntity with these valuesDisplay to the user the details of the entity (by displaying the String returned by toText.Task 3.3Comment out the code in main() you have used for task 3.2 – so you have a record of itAdd code to allow the user to enter three objects of type AnEntity (use an array and a for loop)The function should then display all the objects.Class AWorldThe class AWorld defines a world in which various objects of class AnEntity exist. A World is definedby its size, in the horizontal and vertical directions, and an array of AnEntity objects. It should have twoconstructors, one with no arguments which sets default size and maximum number of entities, and onewhich is passed the size and the maximum number : both should store the size and initialise the world,provide space for the appropriate number of entities, but with no entitites defined. It should also havefunctions to get and set its size. It should also have a function to add another entity to the world (whichis passed the species, position, etc – which will use the AnEntity class), and one that lists each entity inthe world. It should have a main() function to test it, the code for which will have similarities to thatwritten in the main() of AnEntity.Task 3.4Create a new class AWorld within the same package as AnEntity, again with a main functionso you can test it, write the code for the above functions.Task 3.5To test it, in the main function(), ask the user for the maximum number of entities in the world,create an object of type AWorld, and then have a for loop in which the user enters the details about eachentity in turn. Once they are all entered, display to the user the details of each entity.Department of Computer Science2