Warning!! This is an involved assignment. You will need several days to complete it! You will want to complete the assignment step by step (as outlined below)!
This document provides an overview of the entire assignment. It has actually been broken into three smaller parts, and you should be following those, rather than this document. See the Canvas pages.
Provide a solution to Programming Project #4, at the end of chapter 8 of the textbook. The class Doodle should have the primary main() method. I.e., when I want to run your program, I will run that class. (Another way of looking at it: when you specify the "Main class" for your Eclipse project, it should be "Doodle".)
Here are skeletons of the five classes I used to solve this problem, Organism.java, Ant.java, Doodlebug.java, World.java and Doodle.java. There are an infinite number of ways to solve the problem. You are free to use as many or as few classes as you want. The only hard and fast requirement is that your primary "main()" method must be in a class named Doodle.
Note that my solution places each class in a separate file. This is good software engineering, but not strictly necessary. You could--but I don't recommend it--place all the class definitions in the file Doodle.java, and the system would work just fine. (You may recall that a Java file can contain any number of class definitions, but only one that is marked "public".)
This is a fairly involved assignment. Don't be scared by the number of classes. Just look at my comments in each and try to implement each method to do what my comments indicate it should. Don't be afraid to post questions on the course discussion site or to e-mail me.
Oh, and as usual, don't try to do the whole thing all at once. You might try reaching each of these milestones in order:
Get World's constructor(s) working and it's toString method. Just create a world and print it. (Use the main() I've provided in the World class as a starting point.) I chose to have my toString method precede each line with a line number to make it easier to see if the ants and doodlebugs were moving properly. Click here to see an example.
To make sure the World is being created properly, count the ants and doodlebugs and make sure the right number are there. You may want to get World.isEmptyCell(x,y) working properly first, as you'll want to use it in the constructor (to make sure you are only putting organisms where the cells are empty). Note the code in the constructor that controls whether the random number generator is seeded or not.
Make sure that you complete the implementation of the Organism constructor--you'll need it for the next phase.
Make sure that your Ant and Doodlebug constructors call it!!!
If you are in the lab, show me your output.
It may have been a while since you worked with 2-dimensional arrays. If so, you might want to review pages 387-390 of your textbook.
In the constructor for World you'll use a loop to generate all the ants, and another to generate all the doodlebugs. Each time you go through these loops you'll generate a random x and y coordinate as the position of the organism. Make sure the cell at those coordinates is empty (you might use the World.isEmpty() method!) If it is, go ahead and generate an Ant or Doodlebug (as appropriate) and put it there. Your code might look something like:
public World(int numAnts, int numDoodles) { ...loop to generate all ants...{ cells[y][x] = new Ant(x,y,this); }Notice the use of this in the invocation of the constructor. You need this because the Ant constructor wants a reference to the World that the Ant resides in. The this keyword refers to the World object that is currently being constructed. This, by the way, is an example of a data structure using circular referencing. The World refers to each of its organisms, and each organism refers to the World it belongs in.
To generate random numbers in this program, use the myRand method in World. It uses a "random number generator" stored as an instance variable and initialized in the World constructor. The rest of this section describes how to create a random number generator (which you'll need to finish the World constructor) and explains how the generate is used in myRand (you don't have to implement myRand, but you should look at my code to understand what it does).
While the book suggests that you use the Math.random() method to generate random numbers, I suggest you use java.util.Random objects, instead. (Full documentation can be found be found here.) It can be easier to debug programs using these because you can generate repeatable results. I.e., if you see an error, you can just run the program again and the error should appear at the same time and place. Here's how to set one up:
import java.util.Random; .... private myRndNumGenerator = new Random(123456L); ....
You may not recall the syntax of "123456L". The 'L' indicates that the value is a long, not an int. The number 123456 is called the "seed". (You can use any number as the seed.) This determines what sequence of random numbers this generator will create. If you omit the seed from the constructor, you will get a different sequence of random numbers each time. The most common method to invoke on a Random is nextInt(x), with an integer parameter, x. The method returns a random integer between 0 and x-1, inclusive.
Here is an example. The following code is executed twice:
import java.util.Random; public class DemoRandom { public static void main(String args[]){ Random generator1 = new Random(); Random generator2 = new Random(345612L); System.out.print("Sequence generated without a seed: "); for (int i=0; i<10; i++) System.out.print(generator1.nextInt(10)+" "); System.out.print("\nSequence generated with a seed: "); for (int i=0; i<10; i++) System.out.print(generator2.nextInt(10)+" "); System.out.println(""); } }This yielded the following output:
Sequence generated without a seed: 7 8 8 2 3 6 9 3 2 9 Sequence generated with a seed: 8 4 2 6 6 8 1 2 8 3and...
Sequence generated without a seed: 3 5 6 3 3 4 3 9 8 1 Sequence generated with a seed: 8 4 2 6 6 8 1 2 8 3
Note that the "generated with a seed" sequences are the same in both cases, while the sequences generated with a non-seeded generator differ.