Question

In: Computer Science

Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...

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()

Solutions

Expert Solution

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


Related Solutions

Create a class called FileSumWrapper with a method that has the signature public static void handle(String...
Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound). Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know...
Create a student class that stores name, registration number and percentage, grade. Add member functions -...
Create a student class that stores name, registration number and percentage, grade. Add member functions - Read( string n, int r, float p): Read function that accepts parameter - Display(): To display student’s information - CalculateGrade()
Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available …………………...
Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available ………………… +constructor, getters, setters, toString() Write a lambda expression showAdoptable using a standard functional interface that will display the dog’s name age and breed if adopted is false. Help please!!
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double value);     void    Clear();     void    Divide(double value);     double  GetValue() const;     void    Multiply(double value);     void    SetValue(double  newValue);     void    Subtract(double value);    private:     // data members     double  m_total; };    #endif // CCALC_HEADER int     main() {     CCalc       calculator;     char        choice;        // loop and let the user manipulate the calculator     do {         // display the menu and get the user selection         DisplayMenu();         cout <<...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
In java please create a class with a method to be able to add ints into...
In java please create a class with a method to be able to add ints into a linked List and to be able to print out all the values at the end. public class Node { int data; Node next;
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT