vault backup: 2024-01-21 00:27:23
This commit is contained in:
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.class
Normal file
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.class
Normal file
Binary file not shown.
14
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.ctxt
Normal file
14
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.ctxt
Normal 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
|
59
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java
Normal file
59
Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java
Normal 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;
|
||||
}
|
||||
}
|
12
Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT
Normal file
12
Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT
Normal 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:
|
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.class
Normal file
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.class
Normal file
Binary file not shown.
35
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.ctxt
Normal file
35
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.ctxt
Normal 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
|
194
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java
Normal file
194
Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java
Normal 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();
|
||||
}
|
||||
}
|
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.class
Normal file
BIN
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.class
Normal file
Binary file not shown.
5
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt
Normal file
5
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt
Normal 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
|
22
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java
Normal file
22
Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java
Normal 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();
|
||||
}
|
||||
}
|
10
Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt
Normal file
10
Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt
Normal 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
|
@@ -0,0 +1,3 @@
|
||||
Bobby, Bear
|
||||
Andy, Aardvark
|
||||
Polly, African Grey Parrot
|
51
Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej
Normal file
51
Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej
Normal 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
|
Reference in New Issue
Block a user