In: Computer Science
Create a class DogWalker
member List <String>doggies
method: void addDog(String name ) //add dog to the List
method: void printDogList() //print all dogs in list
/* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */
Method: void findDogUsingWhile(String dogName)
/* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list use the index to remove it from the list */
Method: String removeDogUsingWhile(String dogName)
/* Iterate over your list of in dog names using an iterator. When your iterator points to the dogname in your list ‘equals’ the dogName passed in Print ‘I found my dog: ’ + dog name */
Method void findDogUsingIterator(String dogName)
/* Iterate over your list of dog names using an iterator. When your iterator points to the dogname in your list ‘equals’ the dogName passed in Use your iterator to remove the element */
Method void removeDogUsingIterator(String dogName)
In your testHarness:
- create a DogWalker instance
- add a batch of (Dog) Names to the List in your DogWalker class ( using your addDog method)
- invoke findDogUsingWhile(String dogname)
- invoke removeDogUsingWhile(String dogname)
- invoke printDogList()
- invoke findDogUsingIterator(String dogname)
- invoke removeDogUsingIterator(String dogname)
- invoke printDogList()
import java.io.*;
import java.util.*;
class DogWalker{
public List<String> doggies= new ArrayList<String>();
// List of doggies
// function adds the doggies name
public void addDog(String Name)
{
doggies.add(Name);
}
// function finds the flag whether the function is succesful or
not
public void checkFlag(int flag,String dogName)
{
if(flag==0){
System.out.println("No dog is found with dogname "+dogName);
}
}
// function print the doggies name
public void printDogList(){
int i=0;
for(;i<doggies.size()-1;i++)
{
System.out.print(doggies.get(i)+",");
}
System.out.println(doggies.get(i));
}
// function finds the doggies name using While
void findDogUsingWhile(String dogName){
int flag=0;
int i=0;
while(i<doggies.size())
{
if(doggies.get(i).equals(dogName)){
flag=1;
System.out.println("I Found my dog "+dogName);
}
i++;
}
checkFlag(flag,dogName);
}
// function removes the doggies name using While
String removeDogUsingWhile(String dogName)
{
int flag=0;
int i=0;
while(i<doggies.size())
{
if(doggies.get(i).equals(dogName)){
flag=1;
doggies.remove(i);
//i=doggies.size();
System.out.println("Dog with dogname : "+dogName+" is
removed");
}
i++;
}
checkFlag(flag,dogName);
return dogName;
}
// function finds the doggies name using Iterator
void findDogUsingIterator(String dogName){
int flag=0;
Iterator i = doggies.iterator();
String name = "";
while(i.hasNext()){
name = (String) i.next();
if (name.equals(dogName)) {
flag=1;
System.out.println("I Found my dog "+ dogName);
}
}
checkFlag(flag,dogName);
}
// function removes the doggies name using Iterator
void removeDogUsingIterator(String dogName){
try {
int flag=0;
Iterator i = doggies.iterator();
String name = "";
while(i.hasNext()){
name = (String) i.next();
if (name.equals(dogName)) {
flag=1;
i.remove();
System.out.println("Dog with name : "+ dogName+" is
removed");
}
}
checkFlag(flag,dogName);
} catch(Exception e) {
System.out.println("Exception Found");
}
}
}
public class TestHarness{
public static void main (String[] args) {
DogWalker d = new DogWalker();
// bunch of names are added
d.addDog("Charlier");
d.addDog("Max");
d.addDog("Jumper");
d.addDog("Daisy");
d.addDog("Lola");
d.addDog("Jack");
// finding using While
d.findDogUsingWhile("Jumper");
// removing using While
d.removeDogUsingWhile("Daisy");
//printing the List
d.printDogList();
// finding using iterator
d.findDogUsingIterator("Jack");
//removing using iterator
d.removeDogUsingIterator("Max");
//printing the list
d.printDogList();
}
}
When one execute above program gets the following outpiut