java
Original PRG/Animal.java
Original PRG/Animal.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
javaapplication1
;
/**
*
*
@author
Alec
*/
public
class
Animal
{
private
String
species
;
private
String
color
;
private
boolean
isVertibrate
;
private
boolean
canSwim
;
public
Animal
()
{
this
(
""
,
""
,
true
,
false
);
// calls the overloaded constructor
}
public
Animal
(
String
species
,
String
color
,
boolean
isVertibrate
,
boolean
canSwim
)
{
this
.
species
=
species
;
this
.
color
=
color
;
this
.
isVertibrate
=
isVertibrate
;
this
.
canSwim
=
canSwim
;
}
public
String
getAnimalInfo
()
{
return
"species: "
+
species
+
"\ncolor: "
+
color
+
"\nisVertibrate: "
+
isVertibrate
+
"\ncanSwim: "
+
canSwim
;
}
public
String
getSpecies
()
{
return
species
;
}
public
String
getColor
()
{
return
color
;
}
public
boolean
isVertibrate
()
{
return
isVertibrate
;
}
public
boolean
isCanSwim
()
{
return
canSwim
;
}
public
void
setSpecies
(
String
species
)
{
this
.
species
=
species
;
}
public
void
setColor
(
String
color
)
{
this
.
color
=
color
;
}
public
void
setIsVertibrate
(
boolean
isVertibrate
)
{
this
.
isVertibrate
=
isVertibrate
;
}
public
void
setCanSwim
(
boolean
canSwim
)
{
this
.
canSwim
=
canSwim
;
}
}
__MACOSX/Original PRG/._Animal.java
Original PRG/animals.txt
Dog Black and tan true true Cat Black true true Squirrel Orange true false Octopus Purple false true
__MACOSX/Original PRG/._animals.txt
Original PRG/AnimalsWithIterator.java
Original PRG/AnimalsWithIterator.java
//Alec Kozarovitsky
//PRG 421
//Week 3 Individual Assignment: Iterator Program
/*
This program is based on the week 2 Animals program. It uses the same Animal class
that the week 2 assignment used, but now there is different functionality. All user
interaction in the program has been removed - the only input this program takes is
the animals.txt file. The program uses a Scanner object to iterate through the file
and use the data from the file to create new animal objects. The scanner class implements
Iterator, so they work in the same way. The file is structured in a way that each animal
object takes up exactly 4 lines. The first line is the animal type, the second line
is the animal color, and the third and fourth lines are booleans indicating if the
animal is a vertibrate and if it can swim, respectively. Each animal object is separated
by a blank line. This is not necessary, but is recommended for organization purposes
(the program has logic to work with or without the blank lines between animal objects).
The last line of the file must be the fourth line of the last Animal object. If not,
or if there is more than one blank line separating each object, the program will
throw a NoSuchElementException, which is caught and handled. The file needs to placed
in the root folder of the project (the one that contains the build, nbproject, src,
and test folders). If the file cannot be found there or is placed in an incorrect
location, the program will notify the user that the file cannot be found. If the file
is empty (no animal data), the program notifies the user that the list is empty.
If the file reads as intended and the list of animals is poopulated correctly, the
program will them use an iterator to iterate through the ArrayList and output the
animal data for each animal.
*/
package
javaapplication1
;
import
java
.
util
.
*
;
import
java
.
io
.
*
;
/**
*
*
@author
Alec
*/
public
class
AnimalsWithIterator
{
static
ArrayList
<
Animal
>
animals
=
new
ArrayList
<
Animal
>
();
public
static
void
main
(
String
[]
args
)
{
try
{
File
file
=
new
File
(
"animals.txt"
);
Scanner
sc
=
new
Scanner
(
file
);
while
(
sc
.
hasNextLine
())
{
String
species
=
sc
.
nextLine
();
if
(
species
.
equals
(
""
))
{
//this is to skip the blank space between animals (used for organization purposes in the text file)
species
=
sc
.
nextLine
();
}
String
color
=
sc
.
nextLine
();
boolean
isVertibrate
=
Boolean
.
parseBoolean
(
sc
.
nextLine
());
boolean
canSwim
=
Boolean
.
parseBoolean
(
sc
.
nextLine
());
animals
.
add
(
new
Animal
(
species
,
color
,
isVertibrate
,
canSwim
));
}
printAnimalsList
();
}
catch
(
FileNotFoundException
e
)
{
System
.
out
.
println
(
"Sorry, the file does not exist."
);
}
catch
(
NoSuchElementException
ne
)
{
System
.
out
.
println
(
"Your file contents are not in the correct format."
);
}
}
public
static
void
printAnimalsList
()
{
System
.
out
.
println
(
"List of animals:"
);
if
(
animals
.
size
()
==
0
)
{
System
.
out
.
println
(
"empty"
);
}
else
{
Iterator
iter
=
animals
.
iterator
();
while
(
iter
.
hasNext
())
{
System
.
out
.
println
(((
Animal
)
iter
.
next
()).
getAnimalInfo
());
System
.
out
.
println
();
}
}
}
}