Implementing a World with Entities
VerifiedAdded on 2019/09/19
|4
|2431
|99
Report
AI Summary
The assignment involves creating three classes: AnEntity, AWorld, and Interface. The AnEntity class represents entities that can be added to a world. Each entity has attributes such as species, position, and energy. The AWorld class represents the world where these entities exist. It has methods for adding entities, displaying all entities, and showing the size of the world. The Interface class provides the console interface for user interaction, allowing users to add, list, display, or exit the program.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Practical Weeks 3, 4, 5 – Java CS2JA11 Prof Richard Mitchell
Java CS2JA11 – Practical Weeks 3,4,5
Artificial Life Simulator
Over 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 Simulator
The 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); class
AWorld 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 - Robot
To 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 Science 1
Java CS2JA11 – Practical Weeks 3,4,5
Artificial Life Simulator
Over 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 Simulator
The 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); class
AWorld 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 - Robot
To 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 Science 1
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Practical Weeks 3, 4, 5 – Java CS2JA11 Prof Richard Mitchell
Week 3 Tasks – classes AnEntity and AWorld
Class AnEntity
The 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 values
Display 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 it
Add 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 AWorld
The 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 Science 2
Week 3 Tasks – classes AnEntity and AWorld
Class AnEntity
The 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 values
Display 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 it
Add 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 AWorld
The 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 Science 2
Practical Weeks 3, 4, 5 – Java CS2JA11 Prof Richard Mitchell
Weeks 4 and 5 Tasks – class AnInterface and classes AnEntity and AWorld
In weeks 4 and 5, you will extend your AnEntity and AWorld classes, adding a new class AnInterface.
The Interface will provide the console interface which will allow the user to define the size of the world,
and onto which the world will be displayed. This will be developed in stages. The key point is that
interaction to/from the user is in AnInterface, but the actual world and its entities are handled by the
AnEntity and AWorld classes.
Class Interface
First create the class Interface, which has an AWorld object, with a main() function which creates an
object of type Interface which initialises its AWorld and which the user the options of (A)dding an
entity, (L)isting all entities and e(x)it.
Task 4.1 Create the AnInterface class (see the RobotInterface example) and implement the code to Add
and List (which will call code similar to that you wrote in the AWorld class.
Task 4.2 Add an option and the associated functions to (D)isplay the world, something like the
following, where A represents an entity whose symbol is A (perhaps it is an ant), and B is an entity
whose symbols is bee (perhaps a Bee).
/--------------------\
| A |
| |
| B |
| |
| B A |
\--------------------/
For this, you will need to add attributes and methods to the Interface class to display the arena (it is
recommended you look at add the displayChars and currRow attributes and the padChars and
showRobot methods in RobotInterface and amend suitably). Also, add to class AnEntity a method
showEntity, which is passed the Interface, and which calls the Interface method with its Symbol, and
add to AWorld a method to show all entities.
Task 4.3
Modify the Add function, such that the user specifies the species to be added, and the AWorld class
randomly finds a location in the world where there is no existing entity. Hint you will need to add
methods to AWorld and AnEntity – but see the Robot files.
Task 4.4
The world is to be allowed to contain different life forms (eg ants, bees), food and obstacles. These
different types can be handled by the AnEntity class using the ‘species’ attribute to be food, obstacle,
ant, etc. Live forms and Food should be given an energy of 5 units.
Add a configure option such that the user can define for the world, by specifying its size, the percentage
of the world which are food, and which are obstacles, and the number of life forms for each species
The user could enter “40 20 5 3 ant 6 bee 9”, for a 40x20 world, where 5% (of 800) is food, 3% is
obstacles, there are 6 ants and 9 bees. AWorld will need a function which is passed such a string, and
which then sets up the array of entities suitably.
Hint: implement this in a function called fromText which is passed the string. Later you will write a
function toText, which returns the same information (note AnEntity has a toText function).
Department of Computer Science 3
Weeks 4 and 5 Tasks – class AnInterface and classes AnEntity and AWorld
In weeks 4 and 5, you will extend your AnEntity and AWorld classes, adding a new class AnInterface.
The Interface will provide the console interface which will allow the user to define the size of the world,
and onto which the world will be displayed. This will be developed in stages. The key point is that
interaction to/from the user is in AnInterface, but the actual world and its entities are handled by the
AnEntity and AWorld classes.
Class Interface
First create the class Interface, which has an AWorld object, with a main() function which creates an
object of type Interface which initialises its AWorld and which the user the options of (A)dding an
entity, (L)isting all entities and e(x)it.
Task 4.1 Create the AnInterface class (see the RobotInterface example) and implement the code to Add
and List (which will call code similar to that you wrote in the AWorld class.
Task 4.2 Add an option and the associated functions to (D)isplay the world, something like the
following, where A represents an entity whose symbol is A (perhaps it is an ant), and B is an entity
whose symbols is bee (perhaps a Bee).
/--------------------\
| A |
| |
| B |
| |
| B A |
\--------------------/
For this, you will need to add attributes and methods to the Interface class to display the arena (it is
recommended you look at add the displayChars and currRow attributes and the padChars and
showRobot methods in RobotInterface and amend suitably). Also, add to class AnEntity a method
showEntity, which is passed the Interface, and which calls the Interface method with its Symbol, and
add to AWorld a method to show all entities.
Task 4.3
Modify the Add function, such that the user specifies the species to be added, and the AWorld class
randomly finds a location in the world where there is no existing entity. Hint you will need to add
methods to AWorld and AnEntity – but see the Robot files.
Task 4.4
The world is to be allowed to contain different life forms (eg ants, bees), food and obstacles. These
different types can be handled by the AnEntity class using the ‘species’ attribute to be food, obstacle,
ant, etc. Live forms and Food should be given an energy of 5 units.
Add a configure option such that the user can define for the world, by specifying its size, the percentage
of the world which are food, and which are obstacles, and the number of life forms for each species
The user could enter “40 20 5 3 ant 6 bee 9”, for a 40x20 world, where 5% (of 800) is food, 3% is
obstacles, there are 6 ants and 9 bees. AWorld will need a function which is passed such a string, and
which then sets up the array of entities suitably.
Hint: implement this in a function called fromText which is passed the string. Later you will write a
function toText, which returns the same information (note AnEntity has a toText function).
Department of Computer Science 3
Practical Weeks 3, 4, 5 – Java CS2JA11 Prof Richard Mitchell
Exploring the World
The next step is turn your world into a simulation where the live entities (ants, etc) move around in
search of food. Such entities can move to an adjacent cell in the horizontal or vertical directions (North
South East or West), providing there is no obstacle or other live entity there. If a live entity moves to
where food is, it will eat the food. Note, this will require entities to know about the world in which they
exist, so they can ‘see’ if there is food somewhere. Hence, when an entity will need an attribute for the
world in which it exists, and it should be able to call a member function of the world, to see if there is
anything there.
Task 5.1 Define an enumeration type for the directions N, S, W and W, and include a method for this
which generates a random direction.
Hint – look at https://kodejava.org/how-do-i-pick-a-random-value-from-an-enum/ or similar.
Task 5.2 Modify AWorld and AnEntity such the each entity has an attribute being the world in which it
exists. Write a method in AWorld which reports if there is food at a given x,y position and one which
reports if an entity can move to a given x,y position.
Task 5.3 Write a method for an entity smellFood (Direction d, int range) which returns true if there is
food in the given direction up to range locations away.
Task 5.4 Write a method in AWorld (which calls appropriate methods in AnEntity) which, for all live
entities, obeys the following:
Scans its local environment for food
Selects a direction in which to move (towards food if available)
Moves the entity one place in that direction unless there is an obstacle there
If the entity finds food it increases its energy by the amount of energy the food has, and the food
is removed from the world.
In addition, redraw the world after all live entities have been processed – with a slight change, namely
that rather than displaying F for food, display the amount of energy the food has.
Also, write a method in AWorld which returns a string with the statistics of the amount of energy of the
entities and the amount of food left, which is displayed by the Interface class.
In the AnInterface class, add a method to simulate the world n times (calling the AWorld class
appropriately), displaying the world each time.
Get the code in a state such that you are able to demonstrate it in the allotted slot during Week 6.
Optional Task (which you will do in week 7 if not done here)
Write code to allow the user to configure the world by reading data from a file (text or binary – it is up
to you), and to save the configuration to a file. Use the JFileChooser library.
Department of Computer Science 4
Exploring the World
The next step is turn your world into a simulation where the live entities (ants, etc) move around in
search of food. Such entities can move to an adjacent cell in the horizontal or vertical directions (North
South East or West), providing there is no obstacle or other live entity there. If a live entity moves to
where food is, it will eat the food. Note, this will require entities to know about the world in which they
exist, so they can ‘see’ if there is food somewhere. Hence, when an entity will need an attribute for the
world in which it exists, and it should be able to call a member function of the world, to see if there is
anything there.
Task 5.1 Define an enumeration type for the directions N, S, W and W, and include a method for this
which generates a random direction.
Hint – look at https://kodejava.org/how-do-i-pick-a-random-value-from-an-enum/ or similar.
Task 5.2 Modify AWorld and AnEntity such the each entity has an attribute being the world in which it
exists. Write a method in AWorld which reports if there is food at a given x,y position and one which
reports if an entity can move to a given x,y position.
Task 5.3 Write a method for an entity smellFood (Direction d, int range) which returns true if there is
food in the given direction up to range locations away.
Task 5.4 Write a method in AWorld (which calls appropriate methods in AnEntity) which, for all live
entities, obeys the following:
Scans its local environment for food
Selects a direction in which to move (towards food if available)
Moves the entity one place in that direction unless there is an obstacle there
If the entity finds food it increases its energy by the amount of energy the food has, and the food
is removed from the world.
In addition, redraw the world after all live entities have been processed – with a slight change, namely
that rather than displaying F for food, display the amount of energy the food has.
Also, write a method in AWorld which returns a string with the statistics of the amount of energy of the
entities and the amount of food left, which is displayed by the Interface class.
In the AnInterface class, add a method to simulate the world n times (calling the AWorld class
appropriately), displaying the world each time.
Get the code in a state such that you are able to demonstrate it in the allotted slot during Week 6.
Optional Task (which you will do in week 7 if not done here)
Write code to allow the user to configure the world by reading data from a file (text or binary – it is up
to you), and to save the configuration to a file. Use the JFileChooser library.
Department of Computer Science 4
1 out of 4
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024 | Zucol Services PVT LTD | All rights reserved.