ArrayLists of Objects - Iterators - ListIterators
Write a class called AsciiPetOwner that inherits from the AsciiMan class whose code can be linked to from below. An AsciiPetOwner can have no pets, one pet or many pets all of types that you created in your three AsciiPet classes.
1)The AsciiPetOwner should have a one parameter constructor with just a single String for a name.
public AsciiPetOwner(String name) - the ArrayList should be initialized but it will be empty.
2)The second constructor should have two parameters, a name and an AsciiPet.
public AsciiPetOwner(String name, AsciiPet pet)
3)The third constructor should be a name and an ArrayList. The ArrayList will be assumed to contain pets.
public AsciiPetOwner(String name, ArrayList<AsciiPet> pets1)
ITERATORS MUST BE USED FOR ALL METHODS BELOW!!
4) Write a method called printTable that will print a table with the name and the sound for each of the AsciiPetOwners pets
NAME SOUND
===============
rex bark
dusty meow
duckie quack
public void printTable()
5)Write a method call countDogs() that will return and integer that is the number of dogs the AsciiPetOwner has. hint - used the sound.
public int countDogs()
6)Write a method called removeAnimal(String name) that will remove any animal in the list with the name in the parameter.
public void removeAnimal(String name)
7)Write a method called removeAllDogs() that will remove all dogs from the list of pets
public void removeAllDogs();
8)Add a method called addPet() to the AsciiPetOwner class that takes an AsciiPet as a parameter.
public void addPet(AsciiPet pet)
9)Add a method to the AsciiPetOwner class that takes two parameters of type String. The first parameter will be the type of an animal and the second will be a name. The method must construct an AsciiPet of the correct type and add it to the PetOwners ArrayList of Animals. If the type is not of a type of animal that you have give an error message.
public void addPet(String type,String name)
10)Make a file of animal that looks like the one below in notepad. Call it pets.txt and save it into the classes folder.
Brian
dog teb
cat cutie
duck duckie
dog rex
cat tabby
Write a new constructor of the AsciiPetOwner that will construct the owner given just the file name. public AsciiPet(File f) Use the Scanner class for file input. Remember to inport java.util.Scanner and java.io.* for the File class.
public AsciiPetOwner(File fileName)
{
Scanner infile=null;
try {
infile = new Scanner(fileName);
} catch (FileNotFoundException e)
{System.err.println("FileNotFoundException: "+fileName); }
etc.
}
10) a) Construct 3 AsciiPetOwners from three different files. They should each have at least 6 animals.
b) After constructing them make a table with their names and the number of pets and a list of the pet names:
Name Number of Pets Pet Names --------- ------------------------ ----------------- Brian 5 Frank, Rex, Duckie, Sue, Chomp Tom 6 Lizzie, Teb, Balki, Lou, Dust, Mikey Kathy 7 Rex, Tex, Mex, Lex, Dex, Lou, Sue11)The above will get you a 90...add more and more and more for a better grade!!!
ex - write an update method that will update the pets.txt file. In order to make sure you don't lose your original file please use a pets2.txt until you know your file is just right:)
public void updateFile(String f)
{
PrintWriter output=null;
try
{
output = new PrintWriter(new FileWriter(f));
}
catch(IOException ex)
{
System.out.println("***cannot open "+f+" ***");
System.exit(1);
}
System.out.println("File created");
HAVE FUN!!!!!