vault backup: 2024-01-21 00:27:23

This commit is contained in:
2024-01-21 00:27:23 +00:00
parent 358e43950b
commit 7a6e5c1b31
46 changed files with 1226 additions and 14 deletions

View File

@@ -0,0 +1,13 @@
/**
* The stand-alone HelloWorld application.
*
* @author (D.Evans
* @version 10/02/2004
*/
public class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,2 @@
javac ./HelloWorldApp.java
java HelloWorldApp

View File

@@ -0,0 +1,40 @@
#BlueJ package file
dependency1.from=Zoo
dependency1.to=Animal
dependency1.type=UsesDependency
objectbench.height=76
objectbench.width=852
package.editor.height=400
package.editor.width=726
package.editor.x=754
package.editor.y=257
package.numDependencies=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
target1.editor.height=700
target1.editor.width=900
target1.editor.x=70
target1.editor.y=51
target1.height=50
target1.name=Animal
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=80
target1.x=70
target1.y=10
target2.editor.height=700
target2.editor.width=900
target2.editor.x=350
target2.editor.y=130
target2.height=50
target2.name=Zoo
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=150
target2.y=60

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=species\ name
comment0.target=Animal(java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ an\ animal\ of\ the\ specified\ species\ and\ with\ the\ specified\ name\n\ \n\ @param\ \ <code>species</code>\ a\ <code>String</code>\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ <code>name</code>\ a\ <code>String</code>\ specifying\ the\ \n\ name\ of\ animal\n
comment1.params=
comment1.target=java.lang.String\ getSpecies()
comment1.text=\n\ Returns\ the\ species\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ <code>String</code>\n
comment2.params=
comment2.target=java.lang.String\ getName()
comment2.text=\n\ Returns\ the\ name\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ <code>String</code>\n
comment3.params=
comment3.target=java.lang.String\ toString()
comment3.text=\n\ Returns\ a\ string\ representing\ the\ <code>Animal</code>\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ <code>String</code>\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ <code>String</code>\ representation\ of\ the\ animal\n
numComments=4

View File

@@ -0,0 +1,59 @@
/**
* class <code>Animal</code> simulates an animal and stores
* the species and name.
*
* @author D Newton
* @version Version 1, 1 November 2010
*/
public class Animal
{
// species of the animal e.g. lion, tiger
private String species;
// name of the animal e.g. Leo, Tommy
private String name;
/**
* Create an animal of the specified species and with the specified name
*
* @param <code>species</code> a <code>String</code> specifying the
* type of animal
* @param <code>name</code> a <code>String</code> specifying the
* name of animal
*/
public Animal(String species, String name)
{
this.species = species;
this.name = name;
}
/**
* Returns the species of the <code>Animal</code> object
*
* @return the species of animal, as a <code>String</code>
*/
public String getSpecies()
{
return species;
}
/**
* Returns the name of the <code>Animal</code> object
*
* @return the name of animal, as a <code>String</code>
*/
public String getName()
{
return name;
}
/**
* Returns a string representing the <code>Animal</code> object. For a
* lion with name Leo it will return the <code>String</code> "Leo, a lion"
*
* @return a <code>String</code> representation of the animal
*/
public String toString()
{
return name + ", a " + species;
}
}

View File

@@ -0,0 +1,12 @@
------------------------------------------------------------------------
This is the project README file. Here, you should describe your project.
Tell the reader (someone who does not know anything about this project)
all he/she needs to know. The comments should usually include at least:
------------------------------------------------------------------------
PROJECT TITLE:
PURPOSE OF PROJECT:
VERSION or DATE:
HOW TO START THIS PROJECT:
AUTHORS:
USER INSTRUCTIONS:

Binary file not shown.

View File

@@ -0,0 +1,35 @@
#BlueJ class context
comment0.params=
comment0.target=Zoo()
comment0.text=\n\ Create\ an\ "empty"\ zoo\n
comment1.params=fileName
comment1.target=Zoo(java.lang.String)
comment1.text=\n\ Create\ a\ zoo\ and\ populate\ it\ using\ data\ from\ a\ text\ file\n
comment10.params=fileName
comment10.target=void\ readAnimalData(java.lang.String)
comment10.text=\n\ Reads\ animal\ data\ from\ a\ file\ and\ adds\ corresponding\ animals\ to\ the\ zoo\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
comment2.params=animal
comment2.target=void\ storeAnimal(Animal)
comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ <code>animal</code>\ an\ <code>Animal</code>\ object,\ the\ animal\ to\ be\ added\n
comment3.params=listPosition
comment3.target=void\ showAnimal(int)
comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment4.params=
comment4.target=int\ numberOfAnimals()
comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n
comment5.params=
comment5.target=void\ showAllAnimals()
comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n
comment6.params=listPosition
comment6.target=void\ removeAnimal(int)
comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment7.params=
comment7.target=void\ populate()
comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n
comment8.params=
comment8.target=void\ countAnimals()
comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n
comment9.params=fileName
comment9.target=void\ writeAnimalData(java.lang.String)
comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
numComments=11

View File

@@ -0,0 +1,192 @@
import java.util.*;
import java.io.*;
/**
* class <code>Zoo</code> simulates storing animals in a collection.
*
* @author D Newton
* */
public class Zoo
{
private ArrayList<Animal> animalCollection;
private int lionCount;
private int tigerCount;
private int elephantCount;
private int otherCount;
/**
* Create an "empty" zoo
*/
public Zoo()
{
animalCollection = new ArrayList<Animal>();
lionCount = 0;
tigerCount = 0;
elephantCount = 0;
otherCount = 0;
}
/**
* Create a zoo and populate it using data from a text file
*/
public Zoo(String fileName) throws FileNotFoundException
{
this();
readAnimalData(fileName);
}
/**
* Adds an animal to the zoo
*
* @param <code>animal</code> an <code>Animal</code> object, the animal to be added
*/
public void storeAnimal(Animal animal)
{
animalCollection.add(animal);
}
/**
* Shows an animal by printing it's details. This includes
* it's position in the collection.
*
* @param <code>listPosition</code> the position of the animal
*/
public void showAnimal(int listPosition)
{
Animal animal;
if( listPosition < animalCollection.size() )
{
animal = animalCollection.get(listPosition);
System.out.println("Position " + listPosition + ": " + animal);
}
}
/**
* Returns how many animals are stored in the collection
*
* @return the number of animals in the collection
*/
public int numberOfAnimals()
{
return animalCollection.size();
}
/**
* Displays all the animals in the collection
*
*/
public void showAllAnimals()
{
System.out.println("Zoo");
System.out.println("===");
int listPosition = 0;
while( listPosition<animalCollection.size() )
{
showAnimal(listPosition);
listPosition++;
}
System.out.println(listPosition + " animals shown" ); // display number of animals shown
}
/**
* Remove an animal from the collection
*
* @param <code>listPosition</code> the position of the animal
*/
public void removeAnimal(int listPosition)
{
if( listPosition>=0 && listPosition<animalCollection.size() )
{
animalCollection.remove(listPosition);
}
else
{
System.out.println("Invalid position");
}
}
/**
* Adds a pre-defined set of animals to the current collection
*
*/
public void populate()
{
storeAnimal( new Animal("lion", "Leo") );
storeAnimal( new Animal("tiger", "Tommy") );
storeAnimal( new Animal("elephant", "Ollie") );
storeAnimal( new Animal("rat", "Roland") );
storeAnimal( new Animal("reindeer", "Rudolph") );
storeAnimal( new Animal("Lion", "Lenny") );
storeAnimal( new Animal("Elephant", "Nellie") );
storeAnimal( new Animal("tiger", "Tessa") );
storeAnimal( new Animal("eLephant", "Hetty") );
storeAnimal( new Animal("LION", "Leslie") );
}
/**
* Counts the number of lions, tigers and elephants
*
*/
public void countAnimals( )
{
lionCount = 0; tigerCount = 0;
elephantCount= 0; otherCount = 0;
Iterator<Animal> it = animalCollection.iterator();
while( it.hasNext() )
{
Animal currentAnimal = it.next();
String species = currentAnimal.getSpecies();
if( species.equalsIgnoreCase("lion") )
lionCount++;
else if( species.equalsIgnoreCase("tiger") )
tigerCount++;
else if( species.equalsIgnoreCase("elephant") )
elephantCount++;
else
otherCount++;
}
}
/**
* Writes animal data to a file
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data will be stored.
*
* @throws FileNotFoundException
*/
public void writeAnimalData(String fileName) throws FileNotFoundException
{
PrintWriter pWriter = new PrintWriter(fileName);
for(Animal a: animalCollection)
{
String lineOfOutput = a.getName() + " " + a.getSpecies();
pWriter.println(lineOfOutput);
}
pWriter.close();
}
/**
* Reads animal data from a file and adds corresponding animals to the zoo
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data is stored.
*
* @throws FileNotFoundException
*/
public void readAnimalData(String fileName) throws FileNotFoundException
{
File dataFile = new File(fileName);
Scanner scanner = new Scanner(dataFile);
while( scanner.hasNext() )
{
String name = scanner.next();
String species = scanner.next();
storeAnimal( new Animal(species, name) );
}
scanner.close();
}
}

View File

@@ -0,0 +1,10 @@
Leo lion
Tommy tiger
Ollie elephant
Roland rat
Rudolph reindeer
Lenny Lion
Nellie Elephant
Tessa tiger
Hetty eLephant
Leslie LION

View File

@@ -0,0 +1,3 @@
Bobby bear
Andy aardvark
Polly parrot

View File

@@ -0,0 +1,3 @@
#BlueJ package file
#Thu Jan 18 14:27:41 GMT 2024
project.charset=UTF-8

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=species\ name
comment0.target=Animal(java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ an\ animal\ of\ the\ specified\ species\ and\ with\ the\ specified\ name\n\ \n\ @param\ \ <code>species</code>\ a\ <code>String</code>\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ <code>name</code>\ a\ <code>String</code>\ specifying\ the\ \n\ name\ of\ animal\n
comment1.params=
comment1.target=java.lang.String\ getSpecies()
comment1.text=\n\ Returns\ the\ species\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ <code>String</code>\n
comment2.params=
comment2.target=java.lang.String\ getName()
comment2.text=\n\ Returns\ the\ name\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ <code>String</code>\n
comment3.params=
comment3.target=java.lang.String\ toString()
comment3.text=\n\ Returns\ a\ string\ representing\ the\ <code>Animal</code>\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ <code>String</code>\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ <code>String</code>\ representation\ of\ the\ animal\n
numComments=4

View File

@@ -0,0 +1,59 @@
/**
* class <code>Animal</code> simulates an animal and stores
* the species and name.
*
* @author D Newton
* @version Version 1, 1 November 2010
*/
public class Animal
{
// species of the animal e.g. lion, tiger
private String species;
// name of the animal e.g. Leo, Tommy
private String name;
/**
* Create an animal of the specified species and with the specified name
*
* @param <code>species</code> a <code>String</code> specifying the
* type of animal
* @param <code>name</code> a <code>String</code> specifying the
* name of animal
*/
public Animal(String species, String name)
{
this.species = species;
this.name = name;
}
/**
* Returns the species of the <code>Animal</code> object
*
* @return the species of animal, as a <code>String</code>
*/
public String getSpecies()
{
return species;
}
/**
* Returns the name of the <code>Animal</code> object
*
* @return the name of animal, as a <code>String</code>
*/
public String getName()
{
return name;
}
/**
* Returns a string representing the <code>Animal</code> object. For a
* lion with name Leo it will return the <code>String</code> "Leo, a lion"
*
* @return a <code>String</code> representation of the animal
*/
public String toString()
{
return name + ", a " + species;
}
}

View File

@@ -0,0 +1,12 @@
------------------------------------------------------------------------
This is the project README file. Here, you should describe your project.
Tell the reader (someone who does not know anything about this project)
all he/she needs to know. The comments should usually include at least:
------------------------------------------------------------------------
PROJECT TITLE:
PURPOSE OF PROJECT:
VERSION or DATE:
HOW TO START THIS PROJECT:
AUTHORS:
USER INSTRUCTIONS:

Binary file not shown.

View File

@@ -0,0 +1,35 @@
#BlueJ class context
comment0.params=
comment0.target=Zoo()
comment0.text=\n\ Create\ an\ "empty"\ zoo\n
comment1.params=fileName
comment1.target=Zoo(java.lang.String)
comment1.text=\n\ Create\ a\ zoo\ and\ populate\ it\ using\ data\ from\ a\ text\ file\n
comment10.params=fileName
comment10.target=void\ readAnimalData(java.lang.String)
comment10.text=\n\ Reads\ animal\ data\ from\ a\ file\ and\ adds\ corresponding\ animals\ to\ the\ zoo\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
comment2.params=animal
comment2.target=void\ storeAnimal(Animal)
comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ <code>animal</code>\ an\ <code>Animal</code>\ object,\ the\ animal\ to\ be\ added\n
comment3.params=listPosition
comment3.target=void\ showAnimal(int)
comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment4.params=
comment4.target=int\ numberOfAnimals()
comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n
comment5.params=
comment5.target=void\ showAllAnimals()
comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n
comment6.params=listPosition
comment6.target=void\ removeAnimal(int)
comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment7.params=
comment7.target=void\ populate()
comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n
comment8.params=
comment8.target=void\ countAnimals()
comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n
comment9.params=fileName
comment9.target=void\ writeAnimalData(java.lang.String)
comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
numComments=11

View File

@@ -0,0 +1,194 @@
import java.util.*;
import java.io.*;
/**
* class <code>Zoo</code> simulates storing animals in a collection.
*
* @author D Newton
* */
public class Zoo
{
private ArrayList<Animal> animalCollection;
private int lionCount;
private int tigerCount;
private int elephantCount;
private int otherCount;
/**
* Create an "empty" zoo
*/
public Zoo()
{
animalCollection = new ArrayList<Animal>();
lionCount = 0;
tigerCount = 0;
elephantCount = 0;
otherCount = 0;
}
/**
* Create a zoo and populate it using data from a text file
*/
public Zoo(String fileName) throws FileNotFoundException
{
this();
readAnimalData(fileName);
}
/**
* Adds an animal to the zoo
*
* @param <code>animal</code> an <code>Animal</code> object, the animal to be added
*/
public void storeAnimal(Animal animal)
{
animalCollection.add(animal);
}
/**
* Shows an animal by printing it's details. This includes
* it's position in the collection.
*
* @param <code>listPosition</code> the position of the animal
*/
public void showAnimal(int listPosition)
{
Animal animal;
if( listPosition < animalCollection.size() )
{
animal = animalCollection.get(listPosition);
System.out.println("Position " + listPosition + ": " + animal);
}
}
/**
* Returns how many animals are stored in the collection
*
* @return the number of animals in the collection
*/
public int numberOfAnimals()
{
return animalCollection.size();
}
/**
* Displays all the animals in the collection
*
*/
public void showAllAnimals()
{
System.out.println("Zoo");
System.out.println("===");
int listPosition = 0;
while( listPosition<animalCollection.size() )
{
showAnimal(listPosition);
listPosition++;
}
System.out.println(listPosition + " animals shown" ); // display number of animals shown
}
/**
* Remove an animal from the collection
*
* @param <code>listPosition</code> the position of the animal
*/
public void removeAnimal(int listPosition)
{
if( listPosition>=0 && listPosition<animalCollection.size() )
{
animalCollection.remove(listPosition);
}
else
{
System.out.println("Invalid position");
}
}
/**
* Adds a pre-defined set of animals to the current collection
*
*/
public void populate()
{
storeAnimal( new Animal("lion", "Leo") );
storeAnimal( new Animal("tiger", "Tommy") );
storeAnimal( new Animal("elephant", "Ollie") );
storeAnimal( new Animal("rat", "Roland") );
storeAnimal( new Animal("reindeer", "Rudolph") );
storeAnimal( new Animal("Lion", "Lenny") );
storeAnimal( new Animal("Elephant", "Nellie") );
storeAnimal( new Animal("tiger", "Tessa") );
storeAnimal( new Animal("eLephant", "Hetty") );
storeAnimal( new Animal("LION", "Leslie") );
}
/**
* Counts the number of lions, tigers and elephants
*
*/
public void countAnimals( )
{
lionCount = 0; tigerCount = 0;
elephantCount= 0; otherCount = 0;
Iterator<Animal> it = animalCollection.iterator();
while( it.hasNext() )
{
Animal currentAnimal = it.next();
String species = currentAnimal.getSpecies();
if( species.equalsIgnoreCase("lion") )
lionCount++;
else if( species.equalsIgnoreCase("tiger") )
tigerCount++;
else if( species.equalsIgnoreCase("elephant") )
elephantCount++;
else
otherCount++;
}
}
/**
* Writes animal data to a file
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data will be stored.
*
* @throws FileNotFoundException
*/
public void writeAnimalData(String fileName) throws FileNotFoundException
{
PrintWriter pWriter = new PrintWriter(fileName);
for(Animal a: animalCollection)
{
String lineOfOutput = a.getName() + "," + a.getSpecies();
pWriter.println(lineOfOutput);
}
pWriter.close();
}
/**
* Reads animal data from a file and adds corresponding animals to the zoo
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data is stored.
*
* @throws FileNotFoundException
*/
public void readAnimalData(String fileName) throws FileNotFoundException
{
File dataFile = new File(fileName);
Scanner scanner = new Scanner(dataFile);
scanner.useDelimiter("[,\n]");
while( scanner.hasNext() )
{
String name = scanner.next();
name.trim();
String species = scanner.next();
species.trim();
storeAnimal( new Animal(species, name) );
}
scanner.close();
}
}

View File

@@ -0,0 +1,10 @@
Leo, Lion
Tommy, Tiger
Ollie, Elephant
Roland, Rat
Rudolph, Reindeer
Lenny, Lion
Nellie, Elephant
Tessa, Tiger
Hetty, Elephant
Leslie, Lion

View File

@@ -0,0 +1,3 @@
Bobby, Bear
Andy, Aardvark
Polly, African Grey Parrot

View File

@@ -0,0 +1,36 @@
#BlueJ package file
dependency1.from=Zoo
dependency1.to=Animal
dependency1.type=UsesDependency
objectbench.height=76
objectbench.width=686
package.editor.height=400
package.editor.width=560
package.editor.x=774
package.editor.y=277
package.numDependencies=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
target1.height=50
target1.name=Animal
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=80
target1.x=90
target1.y=10
target2.editor.height=700
target2.editor.width=900
target2.editor.x=814
target2.editor.y=297
target2.height=50
target2.name=Zoo
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=180
target2.y=10

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=species\ name
comment0.target=Animal(java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ an\ animal\ of\ the\ specified\ species\ and\ with\ the\ specified\ name\n\ \n\ @param\ \ <code>species</code>\ a\ <code>String</code>\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ <code>name</code>\ a\ <code>String</code>\ specifying\ the\ \n\ name\ of\ animal\n
comment1.params=
comment1.target=java.lang.String\ getSpecies()
comment1.text=\n\ Returns\ the\ species\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ <code>String</code>\n
comment2.params=
comment2.target=java.lang.String\ getName()
comment2.text=\n\ Returns\ the\ name\ of\ the\ <code>Animal</code>\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ <code>String</code>\n
comment3.params=
comment3.target=java.lang.String\ toString()
comment3.text=\n\ Returns\ a\ string\ representing\ the\ <code>Animal</code>\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ <code>String</code>\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ <code>String</code>\ representation\ of\ the\ animal\n
numComments=4

View File

@@ -0,0 +1,59 @@
/**
* class <code>Animal</code> simulates an animal and stores
* the species and name.
*
* @author D Newton
* @version Version 1, 1 November 2010
*/
public class Animal
{
// species of the animal e.g. lion, tiger
private String species;
// name of the animal e.g. Leo, Tommy
private String name;
/**
* Create an animal of the specified species and with the specified name
*
* @param <code>species</code> a <code>String</code> specifying the
* type of animal
* @param <code>name</code> a <code>String</code> specifying the
* name of animal
*/
public Animal(String species, String name)
{
this.species = species;
this.name = name;
}
/**
* Returns the species of the <code>Animal</code> object
*
* @return the species of animal, as a <code>String</code>
*/
public String getSpecies()
{
return species;
}
/**
* Returns the name of the <code>Animal</code> object
*
* @return the name of animal, as a <code>String</code>
*/
public String getName()
{
return name;
}
/**
* Returns a string representing the <code>Animal</code> object. For a
* lion with name Leo it will return the <code>String</code> "Leo, a lion"
*
* @return a <code>String</code> representation of the animal
*/
public String toString()
{
return name + ", a " + species;
}
}

View File

@@ -0,0 +1,12 @@
------------------------------------------------------------------------
This is the project README file. Here, you should describe your project.
Tell the reader (someone who does not know anything about this project)
all he/she needs to know. The comments should usually include at least:
------------------------------------------------------------------------
PROJECT TITLE:
PURPOSE OF PROJECT:
VERSION or DATE:
HOW TO START THIS PROJECT:
AUTHORS:
USER INSTRUCTIONS:

Binary file not shown.

View File

@@ -0,0 +1,35 @@
#BlueJ class context
comment0.params=
comment0.target=Zoo()
comment0.text=\n\ Create\ an\ "empty"\ zoo\n
comment1.params=fileName
comment1.target=Zoo(java.lang.String)
comment1.text=\n\ Create\ a\ zoo\ and\ populate\ it\ using\ data\ from\ a\ text\ file\n
comment10.params=fileName
comment10.target=void\ readAnimalData(java.lang.String)
comment10.text=\n\ Reads\ animal\ data\ from\ a\ file\ and\ adds\ corresponding\ animals\ to\ the\ zoo\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
comment2.params=animal
comment2.target=void\ storeAnimal(Animal)
comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ <code>animal</code>\ an\ <code>Animal</code>\ object,\ the\ animal\ to\ be\ added\n
comment3.params=listPosition
comment3.target=void\ showAnimal(int)
comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment4.params=
comment4.target=int\ numberOfAnimals()
comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n
comment5.params=
comment5.target=void\ showAllAnimals()
comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n
comment6.params=listPosition
comment6.target=void\ removeAnimal(int)
comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ <code>listPosition</code>\ the\ position\ of\ the\ animal\n
comment7.params=
comment7.target=void\ populate()
comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n
comment8.params=
comment8.target=void\ countAnimals()
comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n
comment9.params=fileName
comment9.target=void\ writeAnimalData(java.lang.String)
comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ <code>fileName</code>\ a\ <code>String</code>,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n
numComments=11

View File

@@ -0,0 +1,194 @@
import java.util.*;
import java.io.*;
/**
* class <code>Zoo</code> simulates storing animals in a collection.
*
* @author D Newton
* */
public class Zoo
{
private ArrayList<Animal> animalCollection;
private int lionCount;
private int tigerCount;
private int elephantCount;
private int otherCount;
/**
* Create an "empty" zoo
*/
public Zoo()
{
animalCollection = new ArrayList<Animal>();
lionCount = 0;
tigerCount = 0;
elephantCount = 0;
otherCount = 0;
}
/**
* Create a zoo and populate it using data from a text file
*/
public Zoo(String fileName) throws FileNotFoundException
{
this();
readAnimalData(fileName);
}
/**
* Adds an animal to the zoo
*
* @param <code>animal</code> an <code>Animal</code> object, the animal to be added
*/
public void storeAnimal(Animal animal)
{
animalCollection.add(animal);
}
/**
* Shows an animal by printing it's details. This includes
* it's position in the collection.
*
* @param <code>listPosition</code> the position of the animal
*/
public void showAnimal(int listPosition)
{
Animal animal;
if( listPosition < animalCollection.size() )
{
animal = animalCollection.get(listPosition);
System.out.println("Position " + listPosition + ": " + animal);
}
}
/**
* Returns how many animals are stored in the collection
*
* @return the number of animals in the collection
*/
public int numberOfAnimals()
{
return animalCollection.size();
}
/**
* Displays all the animals in the collection
*
*/
public void showAllAnimals()
{
System.out.println("Zoo");
System.out.println("===");
int listPosition = 0;
while( listPosition<animalCollection.size() )
{
showAnimal(listPosition);
listPosition++;
}
System.out.println(listPosition + " animals shown" ); // display number of animals shown
}
/**
* Remove an animal from the collection
*
* @param <code>listPosition</code> the position of the animal
*/
public void removeAnimal(int listPosition)
{
if( listPosition>=0 && listPosition<animalCollection.size() )
{
animalCollection.remove(listPosition);
}
else
{
System.out.println("Invalid position");
}
}
/**
* Adds a pre-defined set of animals to the current collection
*
*/
public void populate()
{
storeAnimal( new Animal("lion", "Leo") );
storeAnimal( new Animal("tiger", "Tommy") );
storeAnimal( new Animal("elephant", "Ollie") );
storeAnimal( new Animal("rat", "Roland") );
storeAnimal( new Animal("reindeer", "Rudolph") );
storeAnimal( new Animal("Lion", "Lenny") );
storeAnimal( new Animal("Elephant", "Nellie") );
storeAnimal( new Animal("tiger", "Tessa") );
storeAnimal( new Animal("eLephant", "Hetty") );
storeAnimal( new Animal("LION", "Leslie") );
}
/**
* Counts the number of lions, tigers and elephants
*
*/
public void countAnimals( )
{
lionCount = 0; tigerCount = 0;
elephantCount= 0; otherCount = 0;
Iterator<Animal> it = animalCollection.iterator();
while( it.hasNext() )
{
Animal currentAnimal = it.next();
String species = currentAnimal.getSpecies();
if( species.equalsIgnoreCase("lion") )
lionCount++;
else if( species.equalsIgnoreCase("tiger") )
tigerCount++;
else if( species.equalsIgnoreCase("elephant") )
elephantCount++;
else
otherCount++;
}
}
/**
* Writes animal data to a file
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data will be stored.
*
* @throws FileNotFoundException
*/
public void writeAnimalData(String fileName) throws FileNotFoundException
{
PrintWriter pWriter = new PrintWriter(fileName);
for(Animal a: animalCollection)
{
String lineOfOutput = a.getName() + "," + a.getSpecies();
pWriter.println(lineOfOutput);
}
pWriter.close();
}
/**
* Reads animal data from a file and adds corresponding animals to the zoo
*
* @param <code>fileName</code> a <code>String</code>, the name of the
* text file in which the data is stored.
*
* @throws FileNotFoundException
*/
public void readAnimalData(String fileName) throws FileNotFoundException
{
File dataFile = new File(fileName);
Scanner scanner = new Scanner(dataFile);
scanner.useDelimiter("[,\n]");
while( scanner.hasNext() )
{
String name = scanner.next();
name.trim();
String species = scanner.next();
species.trim();
storeAnimal( new Animal(species, name) );
}
scanner.close();
}
}

Binary file not shown.

View File

@@ -0,0 +1,5 @@
#BlueJ class context
comment0.params=file
comment0.target=ZooApp(java.lang.String)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ ZooApp\n
numComments=1

View File

@@ -0,0 +1,22 @@
/**
* Write a description of class ZooApp here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
public class ZooApp
{
/**
* Constructor for objects of class ZooApp
*/
public ZooApp( String file ) throws FileNotFoundException
{
System.out.println( file );
Zoo zoo = new Zoo();
zoo.showAllAnimals();
zoo.readAnimalData( "new_animals.txt" );
zoo.showAllAnimals();
}
}

View File

@@ -0,0 +1,10 @@
Leo, Lion
Tommy, Tiger
Ollie, Elephant
Roland, Rat
Rudolph, Reindeer
Lenny, Lion
Nellie, Elephant
Tessa, Tiger
Hetty, Elephant
Leslie, Lion

View File

@@ -0,0 +1,3 @@
Bobby, Bear
Andy, Aardvark
Polly, African Grey Parrot

View File

@@ -0,0 +1,51 @@
#BlueJ package file
dependency1.from=Zoo
dependency1.to=Animal
dependency1.type=UsesDependency
dependency2.from=ZooApp
dependency2.to=Zoo
dependency2.type=UsesDependency
objectbench.height=76
objectbench.width=686
package.editor.height=400
package.editor.width=560
package.editor.x=774
package.editor.y=277
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
target1.editor.height=728
target1.editor.width=900
target1.editor.x=423
target1.editor.y=237
target1.height=50
target1.name=ZooApp
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=80
target1.x=260
target1.y=60
target2.height=50
target2.name=Animal
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=80
target2.x=100
target2.y=60
target3.editor.height=700
target3.editor.width=900
target3.editor.x=16
target3.editor.y=254
target3.height=50
target3.name=Zoo
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=80
target3.x=180
target3.y=10