first commit

This commit is contained in:
Jose Caban
2025-06-07 01:59:34 -04:00
commit 388ac241f0
3558 changed files with 9116289 additions and 0 deletions

View File

@@ -0,0 +1,535 @@
/**
* CS1322: Programming Assignment #2 - Fall 2002
*
*
* <PRE>
* Class Zoo - A class designed to provide a menu for users to interact
* with to run the Zoo for P2. This class is provided by CS1322,
* for you to modify as needed.
* You should add your collaboration statement, to this ident box below,
* and place your own @author tag below the one given by the author of
* the code given.
* Revisions 1.0: September 15th, 2002
* Received class from CS1322
* 1.1: September 28th, 2002
* Finished Coding
* </PRE>
*
* @author <A HREF="mailto:adhilton@cc.gatech.edu">Andrew Hilton</A>
* @version Version 1.1, September 28th, 2002
*/
import java.io.*;
public class Zoo
{
/**
* This variable contains all of the buildings (Aquariums and
* Aviaries) that are in this Zoo.
*/
private ZooBuilding[] myBuildings;
/**
* This constant specifies the number of buildings in the Zoo.
*/
private static final int NUM_BUILDINGS=6;
/**
* This constant is the main menu choice for putting a new animal
* into the Zoo.
*/
private static final int CHOICE_PUT_ANIMAL=1;
/**
* This constant is the main menu choice for listing the animals
* in the Zoo.
*/
private static final int CHOICE_LIST_ANIMALS=2;
/**
* This constant is the main menu choice for feeding the animals in
* the Zoo.
*/
private static final int CHOICE_FEED_ANIMALS=3;
/**
* This constant is the main menu choice for moving the net in one
* of the Aviaries.
*/
private static final int CHOICE_MOVE_NET=4;
/**
* This constant is the main menu choice for quitting the program.
*/
private static final int CHOICE_QUIT=0;
/**
* This constant specifies the String that is printed to display the
* main menu.
*/
private static final String MAIN_MENU=
"Main Zoo Control\n" +
"----------------\n" +
CHOICE_PUT_ANIMAL+".) Put an animal in a building\n" +
CHOICE_LIST_ANIMALS+".) List the animals in a building\n" +
CHOICE_FEED_ANIMALS+".) Feed the Animals in a building\n" +
CHOICE_MOVE_NET+".) Move the net in an aviary\n"+
CHOICE_QUIT+".) Quit\n"+
"\nEnter your choice: ";
/**
* This constant specifies the animal creation menu choice to make a bat.
*/
private static final int ANIMAL_BAT=1;
/**
* This constant specifies the animal creation menu choice to make a bird.
*/
private static final int ANIMAL_BIRD=2;
/**
* This constant specifies the animal creation menu choice to make a duck.
*/
private static final int ANIMAL_DUCK=3;
/**
* This constant specifies the animal creation menu choice to make a gator.
*/
private static final int ANIMAL_GATOR=4;
/**
* This constant specifies the animal creation menu choice to make a
* goldfish.
*/
private static final int ANIMAL_GOLDFISH=5;
/**
* This constant specifies the animal creation menu choice to make a shark.
*/
private static final int ANIMAL_SHARK=6;
/**
* This constant specifies the String that is displayed for the animal
* creation menu.
*/
private static final String ANIMAL_MENU=
"Choose an Animal Type:\n"+
"----------------------\n"+
ANIMAL_BAT+".) Bat\n"+
ANIMAL_BIRD+".) Bird\n"+
ANIMAL_DUCK+".) Duck\n"+
ANIMAL_GATOR+".) Gator\n"+
ANIMAL_GOLDFISH+".) Goldfish\n"+
ANIMAL_SHARK+".) Shark\n"+
"\nEnter your choice: ";
/**
* The default constructor for Zoo.
* This constructor initializes myBuildings to be an array of
* size NUM_BUILDINGS, then fills it in, alternating between Aviaries
* and Aquariums.
*/
public Zoo()
{
myBuildings=new ZooBuilding[NUM_BUILDINGS];
for(int i=0;i<myBuildings.length;i++)
{
if(i%2==0)
myBuildings[i]=new Aviary("Aviary number "+(i/2+1));
else
myBuildings[i]=new Aquarium("Aquarium number "+ (i/2+1) );
}//for
}//Zoo()
/**
* Reads user input and creates a specified animal.
* This method reads in input from the user to determine what
* type of Animal to make, and then reads the appropriate information
* for that type of Animal.
* @return a newly created Animal that matches the user's specifications,
* or null if the user selects an invalid Animal number
*/
private Animal makeAnimalForUser()
{
System.out.println(ANIMAL_MENU);
int choice= readIntFromUser();
Animal toRet=null;
String name;
String species;
String dangerous;
boolean isDangerous;
int depth;
switch(choice)
{
case ANIMAL_BAT:
name=promptAndRead("Name: ");
toRet=new Bat(name);
break;
case ANIMAL_BIRD:
name=promptAndRead("Name: ");
species=promptAndRead("Species: ");
do
{
dangerous=promptAndRead("Dangerous [Y/N]: ");
}
while(!(dangerous.equalsIgnoreCase("Y")||
dangerous.equalsIgnoreCase("N")));
isDangerous=dangerous.equalsIgnoreCase("Y");
toRet= new Bird(name,species,isDangerous);
break;
case ANIMAL_DUCK:
name=promptAndRead("Name: ");
toRet= new Duck(name);
break;
case ANIMAL_GATOR:
name=promptAndRead("Name: ");
toRet= new Gator(name);
break;
case ANIMAL_GOLDFISH:
name=promptAndRead("Name: ");
System.out.print("Depth: ");
depth=readIntFromUser();
toRet= new GoldFish(name,depth);
break;
case ANIMAL_SHARK:
name=promptAndRead("Name: ");
System.out.print("Depth: ");
depth=readIntFromUser();
toRet= new Shark(name,depth);
break;
default:
System.out.println("That is not a valid animal type");
break;
}
return toRet;
}//makeAnimalForUser
/**
* This method moves the Animals in all of the buildings around
* randomly. In addition to moving the Animals around, this method
* makes Animals become hungry, at random- each with a 75% chance
* of becoming hungry when this method is called.
*/
private void moveAnimalsAround()
{
for(int i=0;i<myBuildings.length;i++)
{
ZooBuilding zb=myBuildings[i];
LinkedList ll=zb.getTheAnimals();
Object[] animalsInList=ll.asArray();
for(int j=0;j<animalsInList.length;j++)
{
if(animalsInList[j] instanceof FlyingType &&
animalsInList[j] instanceof SwimmingType)
{
//this animal can both swim and fly.
//better find out where it is
FlyingType ft=(FlyingType) animalsInList[j];
SwimmingType st = (SwimmingType) animalsInList[j];
int alt=ft.getAltitude();
int depth=st.getDepth();
boolean swimming;
if(alt<=0)
swimming=true;
else
swimming=false;
if(swimming)
{
//if near the water's surface, maybe we take off
//(1/4 chance)
if(depth<=1 &&Math.random()<0.25)
{
st.setDepth(0);
ft.setAltitude((int)(Math.random()*25)+15);
}
else
{
int change=(int)((Math.random()*5)-2);
depth += change;
st.setDepth(depth);
}
}//swimming
else
{
//flying low. maybe we land and swim?
// (1/4 chance)
if(alt <=25 && Math.random()<0.25)
{
ft.setAltitude(0);
st.setDepth(1);
}
else
{
int change=(int)(Math.random()*48)-24;
alt += change;
ft.setAltitude(alt);
}
}//flying
}
else if(animalsInList[j] instanceof FlyingType)
{
FlyingType ft=(FlyingType) animalsInList[j];
int alt=ft.getAltitude();
int change=(int)((Math.random()*20)+(Math.random()*20)-10);
alt += change;
if(alt<0)
{
alt=0;
}
ft.setAltitude(alt);
}//flying
else if(animalsInList[j] instanceof SwimmingType)
{
SwimmingType st=(SwimmingType) animalsInList[j];
//*** YOUR CODE GOES HERE *** (3)
int depth = st.getDepth();
/************/
int change=(int)((Math.random()*30)-12);
depth+=change;
//*** YOUR CODE GOES HERE *** (4)
st.setDepth(depth);
/*****************/
}//swimming
if(animalsInList[j] instanceof Animal)
{
if(Math.random()<0.75) //75% chance of animal getting hungry
{
((Animal)animalsInList[j]).setIsHungry(true);
}
}
else
{
System.err.println("Error: Non-animal in array at index "+
j+"\n found: "+animalsInList[j]);
System.exit(-1);
}
}//for j
}//for i
}//moveAnimalsAround()
/**
* A conveinece method to prompt the user for input and read it.
* Prints the prompt, then reads and returns a line of input.
* @param prompt is the prompt to print
* @return the line of input read from the user.
*/
private static String promptAndRead(String prompt)
{
System.out.print(prompt);
return IOHelper.readLine();
}
/**
* Reads in an int from the user and returns it.
* This method reads lines of input from the user until the user enters
* a valid number. When a valid number is entered, it is converted
* from a String to an int and returned.
* @return the number that the user entered.
*/
private static int readIntFromUser()
{
String line="";
int choice=0;
boolean done=false;
do
{
//***YOUR CODE GOES HERE*** (1)
line = IOHelper.readLine();
//now do conversion of line to an int
try
{
choice=Integer.parseInt(line);
done=true;
}
catch(NumberFormatException nfe)
{
System.out.println("That was not a number! Try again");
}
}
while(!done);
return choice;
}//readIntFromUser()
/**
* Selects a building number.
* Prompts the user to select a building, continues to reprompt
* until a valid building number is entered.
* @return the building number entered by the user
*/
private int chooseBuilding()
{
int choice;
do
{
System.out.print("Enter a building number");
choice=readIntFromUser();
}
while(choice<0|| choice>=myBuildings.length);
return choice;
}//chooseBuilding
/**
* Handles putting a new Animal into the Zoo.
* This method reads input from the user to determine which building the
* Animal should go into. Then it calls makeAnimalForUser() to
* create a new Animal to put in. If the Animal cannot live in that
* type of building, an error is printed and the method returns,
* otherwise the Animal is added to the building.
*/
private void doPutAnimal()
{
int buildingNumber=chooseBuilding();
ZooBuilding zb=myBuildings[buildingNumber];
Animal toAdd=makeAnimalForUser();
if(toAdd==null)
{
return;
}
if(((myBuildings[buildingNumber] instanceof Aviary) &&
!(toAdd instanceof FlyingType))||
((myBuildings[buildingNumber] instanceof Aquarium) &&
!(toAdd instanceof SwimmingType)))
{
System.out.println("That animal cannot live in that building!");
return;
}
//***YOUR CODE GOES HERE *** (5)
zb.getTheAnimals().addToBack(toAdd);
/**********************************/
}//doPutAnimal()
/**
* Lists the animals in a building.
* This method prompts the user for a building number, then
* lists all of the animals in that building.
*/
private void doListAnimals()
{
//*** YOUR CODE GOES HERE *** (6)
int choice = chooseBuilding();
ZooBuilding zb = myBuildings[choice];
Animal temp;
for(int i=0; i<zb.getTheAnimals().size(); i++){
temp = (Animal)zb.getTheAnimals().removeFromFront();
System.out.println(temp);
zb.getTheAnimals().addToBack(temp);
}
}//doListAnimals()
/**
* Feeds the Animals in a building.
* This method prompts the user for a building number and a type of food,
* then feeds that food to the animals in the specified building.
*/
private void doFeedAnimals()
{
int buildingNumber=chooseBuilding();
ZooBuilding zb=myBuildings[buildingNumber];
String food=promptAndRead("What type of food?");
//*** YOUR CODE GOES HERE *** (7)
zb.feedAnimals(food);
}//doFeedAnimals()
/**
*prompt for building, check if its aviary,
*and if it is, add/remove the net
*/
private void doMoveNet()
{
// *** YOUR CODE GOES HERE *** (9)
//Let's do this all in Hungarian Notation :)
//So MS isn't completely useless, eh?
String sEntry;
int iChoice = 0;
boolean bDone = false;
do{
sEntry = promptAndRead("What building?");
try
{
iChoice=Integer.parseInt(sEntry);
bDone=true;
}
catch(NumberFormatException nfe)
{
System.out.println("That was not a number! Try again");
}
}while(!bDone);
if(!(myBuildings[iChoice] instanceof Aviary)){
System.out.println("That building is not an Aviary, it has no net");
return;
}
boolean bNet;
Aviary avTemp = (Aviary)myBuildings[iChoice];
bNet = avTemp.getNetClosed();
if(bNet){
avTemp.setNetClosed(false);
}
else {//!bNet
avTemp.setNetClosed(true);
}
}//doMoveNet
private void showZooStatus()
{
System.out.println("Zoo status");
System.out.println("----------");
for(int i=0;i<myBuildings.length;i++)
{
System.out.println(myBuildings[i]);
if(myBuildings[i].isSafeToEnter())
{
System.out.println("\t Is safe to enter.");
}
else
{
System.out.println("\t Is NOT safe to enter.");
}
System.out.println("\t Has "+myBuildings[i].getHungryAnimals()+
" hungry animals.");
}//for i
}//showZooStatus
/**
* Runs the Zoo simulation.
* This method displays the menu and performs the requested action
* until the user chooses to quit.
*/
private void run()
{
int userChoice;
do
{
moveAnimalsAround();
showZooStatus();
//***YOUR CODE GOES HERE*** (8)
System.out.println(MAIN_MENU);
/****************************/
userChoice=readIntFromUser();
switch(userChoice)
{
case CHOICE_PUT_ANIMAL:
doPutAnimal();
break;
case CHOICE_LIST_ANIMALS:
doListAnimals();
break;
case CHOICE_FEED_ANIMALS:
doFeedAnimals();
break;
//*** YOUR CODE GOES HERE *** (10)
case CHOICE_MOVE_NET:
doMoveNet();
break;
case CHOICE_QUIT:
/* do nothing */
break;
default:
//***YOUR CODE GOES HERE***(2)
System.out.println("That is not a valid choice, try again");
break;
}//switch(userChoice)
}
while(userChoice!= CHOICE_QUIT);
}//run()
/**
* The starting point for the program- makes a new Zoo, and calls
* run() to do the simulation.
* @params args IGNORED.
*/
public static void main(String[] args)
{
Zoo myZoo=new Zoo();
myZoo.run();
}//main()
}//end class Zoo