Hi,
I'm working on a simulation that uses a fish in a 1x5 "fish tube" matrix. The fish (this symbol: ">=)" ) moves randomly around the tube and the number of "bumps" against the top, bottom, right, and left sides are counted. What I want to do is this: Turn the ">=)" (fish) into a Fish object. Let me paste my code here and I'll explain further afterwards:

public class FishMain
{
    public static void main (String[] args)
    {
        final int moves = 10;
        Enviroment env = new Enviroment();
        
        // Uncomment the lines below if using objects
        //Fish fish1 = new Fish();
        //env.addFish(fish1); //Adds a Fish object to the tank.
        
        env.header();
        env.display();
        
        for (int h = 1; h <= moves; h++)
        {
            env.move();
            env.display();
        }
        
        System.out.println("Number of bumps: " + env.getBumps());
        System.out.println("-----------------------------\n\n");
    }
}

public class Enviroment
{
    static final int env_rows = 1;
    static final int env_cols = 5;
    String[][] boundedEnv = new String[env_rows][env_cols];
    String[] direction = {"left","right","up","down"};
    static int bumps = 0;
    static int loc = 0;
    static int curloc = 0;
    static int firstRun = 0;
    
    public Enviroment()
    {
        for (int fill = 0; fill < 5; fill++)
            boundedEnv[0][fill] = "   ";
        
        // Sets fish intitially in tank
        curloc = (int)(Math.random() * 5);
        boundedEnv[0][curloc] = ">=)";
        firstRun = 1;
    }
    
    public void clear()
    {
        for (int clr = 0; clr < 5; clr++)
            boundedEnv[0][clr] = "   ";
    }
    
    public void display()
    {
        System.out.println("===============");
        for (int i = 0; i < 5; i++)
            System.out.print(boundedEnv[0][i]);
        System.out.println("\n===============");
        
        // Check to see if this run is the initial placement
        if (firstRun != 1)
            System.out.println("Position: " + direction[getLocation()] + "\n");
        else
        {
            System.out.println("***Initial Position***");
            firstRun = 0;
        }            
    }
    
    public void header()
    {
        System.out.println("Fish Tank:");
    }
    
    public void move()
    {
        loc = (int)(Math.random() * 4);
        
        if (loc == 0) //left
        {
            clear();
            
            if (curloc != 0)
            {
                curloc -= 1;
                boundedEnv[0][curloc] = ">=)";  
            }
            else
            {
                boundedEnv[0][curloc] = ">=)";
                bumps++;
            }
        }
        else if (loc == 1) //right
        {
            clear();
            
            if (curloc != 4)
            {
                curloc += 1;
                boundedEnv[0][curloc] = ">=)";  
            }
            else
            {
                boundedEnv[0][curloc] = ">=)";
                bumps++;
            }
        }
        else if (loc == 2) //up
        {
            clear();
            boundedEnv[0][curloc] = ">=)";  
            bumps++;
        }
        else if (loc == 3) //down
        {
            clear();
            boundedEnv[0][curloc] = ">=)";  
            bumps++;
        }
        
    }
    
    public int getLocation()
    {
        return loc;
    }
    
    public int getBumps()
    {
        return bumps;
    }
}

That code will work, but as you can see the "fish" isn't an object. When I looked at a similar simulation in a Java book, it gave an outline of the code. From it, I have come up with the following class and method for Enviroment:

//Add to Enviroment:
    public void addFish(Object thisFish)
    {
        // Not sure what to do here
    }

// Fish class:
public class Fish
{
    int location;
    String fishPicture = ">=)";
    
    public Fish()
    {
        
    }
    
    public void changeLocation(int pos)
    {
        
    }
    
    public String fishpic()
    {
        return fishPicture;
    }
}

I'm pretty stumped now. Objects are confusing to me and I'm not sure how to swap out my ">=)" fish with a Fish object. Please help me get this working so I can make this program more dynamic.
-Thanks so much!

What's the problem?
You can implement this in several ways. Easiest is to have the environment know where all the fish are. Create a coordinate class (or just use the existing Point class) and give the fish a method to move them which takes the current coordinate and returns the new coordinate.
The environment can then check those new coordinates to determine whether the fishy bumped into something.

You could let the fish do that but that's a bit more complicated and may not be appropriate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.