logo

Artificial Life Simulator - Java CS2JA11 Practical Weeks 3,4,5

4 Pages2431 Words99 Views
   

Added on  2019-09-19

About This Document

Develop a code to simulate a two dimensional world in which different types of artificial life exist and move around, and these are displayed in a text console interface. Learn about AnEntity, AWorld, and AnInterface classes. Configure the world by reading data from a file and save the configuration to a file.

Artificial Life Simulator - Java CS2JA11 Practical Weeks 3,4,5

   Added on 2019-09-19

ShareRelated Documents
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 which different types of artificial life exist and move around, and these are displayed in a text console interface. During Week 6 you will demonstrate your program. After Week 6, you will first extend the code, adding more inheritance, file commands etc., after which you develop a similar world, but display it using a Graphical User Interface (GUI), and that will form the basis of the module’s first major piece of 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 of the 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 has the 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 class AnInterface which provides the textual interface which allows the user to enter commands and onto which 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 and AWorld will still be valid.Similar Example - RobotTo illustrate the structure, please see the classes Robot, RobotArena and RobotInterface which show a console 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 the arena in which the robot exists. This should be readily adaptable for class AnEntity which will need to know about the world in which it exists.The robot has a constructor, which is passed its position and the arena, and methods which return its position, checks whether the robot is at a given position, and it has a method to display itself, which is passed an argument specifying the interface in which it is shown, and the method just calls the associated method in the interface which displays the robot. The robot therefore does not need to know details about the interface.The Robot class has a main function, which allows the class to be tested on its own (showing for instance 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 the arena and sets up the array to have up to maxRobots robots. It has a function to add a robot, which repeatedly generates random x,y numbers until a pair is found where there is no robot, and adds the robot there. It calls the getRobotAt method which tries to find a robot at a given position. The final method showRobots, is passed the robot interface and calls functions to display each robot there.Department of Computer Science1
Artificial Life Simulator - Java CS2JA11 Practical Weeks 3,4,5_1
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), horizontal position (int), vertical position (int), energy (int), a unique ID (int), etc. The symbol is used to represent the bug and can be the first character of the species name if not otherwise specified – and it will be used to display the entity in the world.It should have at least two constructors (one with no arguments), methods to get and set the attributes and the methods toString and toText. The null argument constructor should initialise all attributes with suitable default values (for Strings provide a dummy string); the other should be passed values of all the attributes The method toString should return the species and its position as a String, whereas toText should 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.1 Create a new class AnEntity within an existing package (or create a new package for this project if you want) and write the constructor and the other methods.Task 3.2 To 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.3 Comment 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 defined by its size, in the horizontal and vertical directions, and an array of AnEntity objects. It should have two constructors, one with no arguments which sets default size and maximum number of entities, and one which 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 have functions to get and set its size. It should also have a function to add another entity to the world (which is passed the species, position, etc – which will use the AnEntity class), and one that lists each entity in the world. It should have a main() function to test it, the code for which will have similarities to that written in the main() of AnEntity.Task 3.4 Create a new class AWorld within the same package as AnEntity, again with a main function so you can test it, write the code for the above functions.Task 3.5 To 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 each entity in turn. Once they are all entered, display to the user the details of each entity.Department of Computer Science2
Artificial Life Simulator - Java CS2JA11 Practical Weeks 3,4,5_2

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
The Journey Beyond Hells Gate
|12
|4436
|376

4100COMP: Introduction to Computer Programming
|15
|4518
|489

Database Application to a GUI Application Assignment
|3
|1080
|251

Program Design for Network Routing using Dijkstra's Algorithm
|6
|828
|262

Java Restaurant Billing Management System Coursework
|4
|794
|179

Java program for university admission application handling
|3
|883
|225