Monday, March 12, 2012

Creating a random zoo

Let's say you wanted to monitor a zoo that you own.

In this zoo you have multiple animals already in there and they randomly are doing things. They are either eating, sleeping, playing, or wandering. Now there is no set order for how they will do these things so we want to have it randomize. We also know that they are never doing the same thing twice in a row.

We also can accept adding new animals. And we can remove animals from our zoo, but only if they are there.

There is no limit to how many animals you can have in your zoo.

You can have multiple of the same animal, but they cannot be listed separately.

Also, you can add more activities.

Now we need to think about how to approach this problem. So lets list what we know and ways to go about programming it.

So we know that we have multiple animals already in our zoo. So we have to make those animals. However, they have activities they are doing. So we can't just use a string. We need to make a class/struct like we did in the previous one.

We also can add new animals to our zoo. So in our class we are going to make a function for making a new animal.

We also need to be able to randomly see what the animal is doing. We can't allow them to do the same thing twice. I'll be demonstrating this in Java this time.

Notice that it doesn't say whether to have it run endlessly or not. We are going to implement the option to run endlessly regardless.

The way we create a list without a limit is by using an ArrayList or a Vector(THIS IS C++).

An Arraylist is essentially a self re sizing array. Same with vector. They are very nice in the fact we do not have a limit to how many things are going to be in our list. Also, they have a lot of nice functions that go with them.

So i made two classes. ZooDriver(Where I run the program.) and Animals(a class for making animals).
ZooDriver.java
import java.util.ArrayList;
import java.util.Scanner;

public class ZooDriver {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);// System.in is how we take input
// in java
ArrayList activities = new ArrayList();// Since we can
// have multiple
// activities
activities.add("Sleeping");
activities.add("Eating");
activities.add("Playing");
activities.add("Wandering");
ArrayList animals = new ArrayList();// More than one
// animal
addAnimals(animals, "Tiger", false);
addAnimals(animals, "Tiger", false);
addAnimals(animals, "Tiger", false);
addAnimals(animals, "Tiger", false);
addAnimals(animals, "Tiger", false);
addAnimals(animals, "T-Rex", false);
addAnimals(animals, "Panda", false);
addAnimals(animals, "Monkey", false);
addAnimals(animals, "Monkey", false);

System.out.println("Welcome to the Internet Zoo!");
String check = "";
while (check != "5") {
System.out.println("What would you like to do?");
System.out
.println("1. See all the animals and what they are doing?");
System.out.println("2. See a specific animal and its activity?");
System.out.println("3. Add a new animal to the Zoo?");
System.out.println("4. Add a new activity?");
System.out.println("5. Quit");
check = scan.next();

// So we have 5 choices which means we need 5 if statements. But
// since our while takes care of our quit. We just need 4.
while (!(check.equals("1") || check.equals("2")
|| check.equals("3") || check.equals("4") || check
.equals("5"))) {
System.out.println("That is not valid input");
System.out.println(check);
check = scan.next();
}
if (check.equals("1")) {
for (int m = 0; m < animals.size(); m++) {
System.out.println(animals.get(m).toString(activities));
}
System.out.println("\n"); // wanted more spacing
} else if (check.equals("2")) {
System.out
.println("What animal do you want to see?(Not case sensitive)");
String specific = scan.next();
specific = specific.toLowerCase();
for (int m = 0; m < animals.size(); m++) {
if (specific.equals(animals.get(m).getName().toLowerCase())) {
System.out.println(animals.get(m).toString(activities));
}
}

} else if (check.equals("3")) {
System.out
.println("what animal do you want to add?(Not case sensitive)");
String add = scan.next();
addAnimals(animals, add, true);
} else if (check.equals("4")) {
System.out
.println("What activity do you want to add?(Case sensitive!)");
String add = scan.next();
if (activities.contains(add)) {
System.out.println("This is already in the list!");
} else {
activities.add(add);
System.out.println("Added!");
}

}
}

}

public static void addAnimals(ArrayList list, String k,
boolean print) {
for (int m = 0; m < list.size(); m++) {
if (k.toLowerCase().equals(list.get(m).getName().toLowerCase())) {
list.get(m).amount++;
if (print)
System.out
.println("Already in the list, but added to its friends");
return;
}

}
Animal temp = new Animal(k);
temp.amount++;
list.add(temp);
if (print)
System.out.println("Added!");

}
}

Animal.java
import java.util.ArrayList;

public class Animal {
String name;
int amount;
int previous;
String current;

public Animal(String name) {
this.name = name;
amount = 0;
previous = 0;
current = "Sleeping";
}

public String getName() {
return name;
}

public int getAmount() {
return amount;
}

public String activity(ArrayList list) {
String temp = current;
int rand = (int) (Math.random() * list.size());
current = list.get(rand);
while(temp == current){
rand = (int) (Math.random() * list.size());
current = list.get(rand);
}
return current;
}
public String toString(ArrayList list){

String temp = "There is ";
if(amount > 1){
temp = "There are ";
}
temp += getAmount() + " " + getName();
if(amount > 1){
temp += "s and they are " + activity(list);
}
else{
temp += " and it is " + activity(list);
}
return temp;
}
}

Hope you guys enjoyed reading this and have a nice day!

Also, I couldn't find a function in java to clear the console so I ended up making a pretty lame boolean to prevent myself from printing out before the initial questions.

Thank you!

Everett

1 comment: