/******************************* MAIN HEADER ********************************** Title: Assignment #1: Aquarium Simulation Files: Aquarium.java, Fish.java, Piranha.java, FoodCenter.java Author: James D. Skrentny, skrentny@cs.wisc.edu copyright 2000 all rights reserved Course: CS 302: All Lectures Compiler: CodeWarrior IDE 4.0 (JDK 1.2) Platform: Windows NT 4.0 **************************** 80 columns wide *********************************/ import javabook2.*; /** * This program is an aquarium simulation with a user specified number of * fish, piranha, and food centers. Both the fish and piranha move about * (i.e swim) the tank. Fish can eat at food centers, and piranha can eat * fish. A graphical representation of the tank is shown. * * The program user can choose to display the status of the contents of the * aquarium as well as running the simulation for one cycle, n cycles, or * continuously until there are no living things in the aquarium. * * Bugs: Known bugs **/ class Aquarium { private static Tank tank; // contains fish, piranha, food centers // draws the graphical aquarium private static ListBox list; // for simulation menu private static InputBox input; // for user input (# items, N cylces) private static OutputBox output; // for displaying aquarium stats // for the menu options chosen by the user private final static int DISPLAY_STATS = 0; private final static int NEXT_MOVEMENT = 1; private final static int NEXT_N_MOVES = 2; private final static int CONTINUOUS = 3; private final static int QUIT = 4; public static void main (String [] args) { // construct a tank and draw it tank = new Tank(); tank.refreshDisplay(); // construct user I/O objects input = new InputBox (tank); output = new OutputBox(tank, "Aquarium Output"); // construct and configure the menu list = new ListBox (tank, "Menu Choice"); list.addItem("Display Aquarium Status"); list.addItem("Next Movement"); list.addItem("Next N Movements"); list.addItem("Continuous Mode"); list.addItem("Quit"); // Get quantities of tank items int fishQty = readPositiveInt("How many fish do you want?"); int piranhaQty = readPositiveInt("How many piranha do you want?"); int foodQty = readPositiveInt("How many food centers do you want?"); // add items to the tank addFish(fishQty); addPiranha(piranhaQty); addFoodCenters(foodQty); // redraw the tank tank.refreshDisplay(); while (somethingStillAlive()) { // display the options for the user int menuChoice = list.getSelectedIndex(); // process the chosen option switch (menuChoice) { case DISPLAY_STATS: displayStats(); break; case NEXT_MOVEMENT: nextMovement(); break; case NEXT_N_MOVES: nextNMovements(); break; case CONTINUOUS: continuous(); break; case QUIT: System.exit(0); break; default: System.out.println("Choose an option."); } } System.out.println("SIMULATION OVER: no living things"); System.exit(0); } /** * Asks the user for the number of Fish, and constructs and adds them * to the tank. **/ private static void addFish (int quantity) { for ( int i=0; i <= quantity; i++) { tank.addFish(new Fish()); } } /** * Asks the user for the number of Piranha, and constructs and adds * them to the tank. **/ private static void addPiranha (int quantity) { for (int i=0; i