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 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()
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);    } }...
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...
Create a UEmployee class that contains member variables for the university employee name and salary. The...
Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title. Write a runner program that creates one instance of each class and prints all of...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT