Question

Java. You have a hashtable menu of icecreams, and you want to implement the interface Iterator...

Java.

You have a hashtable menu of icecreams, and you want to implement the interface Iterator so you can browse all your icecreams

public class IteratorIceCream implements Iterator{
Hashtable icecreams;
  
public IteratorIceCream( Hashtable icecreams){
this.icecreams = icecreams;
}

public Object next(){
//implement
}

public boolean hasNext(){

//implement hasNext()
}
}

So you can create in class IceCreamMenu

public Iterator createIterator(){
  
}

Homework Answers

Answer #1

Simple iterator coding is utilized to build the IteratorIceCream. There are inbuilt functions for iterator such as hasNext() and next(). We can get the value of hashtable using get(key) function where key is the key of the hashtable and the values over which the iterator iterates.

class IteratorIceCream implements Iterator{
Hashtable icecreams;
Iterator<Object> itr;
public IteratorIceCream( Hashtable icecreams){
this.icecreams = icecreams;
itr = icecreams.keySet().iterator(); // get iterator over set entries
}

public Object next(){
   return itr.next();
}

public boolean hasNext(){
   return itr.hasNext();
}
}

We can create in class IceCreamMenu

public Iterator createIterator(){
return new IteratorIceCream (icecreamsHashtable); // where icecreamsHashtable is hashtable of icecreams
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This file that has the outline of an iterator which will be used to return each...
This file that has the outline of an iterator which will be used to return each char within a String. Complete the hasNext() and next() methods to enable this iteration. In case it is of use, String has two methods which may provide useful -- size() (returning the number of chars it contains) and charAt() (returning the character at the location passed as an argument). import java.util.Iterator; import java.util.NoSuchElementException; public class CharacterIterator implements Iterator<Character> { /** The String whose characters...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data of the ADT should include Java variables for the customer name, the account number, the next due date, the reward points, and the account balance. The initialization operation should set the data to client-supplied values. Include operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the account. Be...
Consider the following interface: interface Something {     boolean validate(int value); } Classes that implement “Something”...
Consider the following interface: interface Something {     boolean validate(int value); } Classes that implement “Something” interface can validate a specific value in different behaviors within some context. For example, this interface could be useful to program a candy dispenser machine to check whether the coin inserted is a valid coin such as a quarter with a diameter of 2.426 centimeters. Also, it could be useful to check if there is any candy available to dispense. Write two classes “Coin”...
Consider the following interface: interface Something {     boolean validate(int value); } Classes that implement “Something”...
Consider the following interface: interface Something {     boolean validate(int value); } Classes that implement “Something” interface can validate a specific value in different behaviors within some context. For example, this interface could be useful to program a candy dispenser machine to check whether the coin inserted is a valid coin such as a quarter with a diameter of 2.426 centimeters. Also, it could be useful to check if there is any candy available to dispense. Write two classes “Coin”...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();. public class AnimalRunner {    public static void main(String[] args)    {       Dog d1 = new Dog("Fred");       d1.speak();       Object obj = new Dog("Connie");       // obj.speak();    } } The uncommented line causes a compile error because obj is an Object reference variable and class Object doesn’t contain a speak() method. This...
Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1-...
Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the next page and returns...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class. Rules are the following: - Deck and PockerHand should be Iterable (note that you may just reuse one of the underlying iterators to provide this functionality) - Poker should implement 2 methods checking for two possible poker hands , see the comments inside the classes for details. Hint: remember how one can count the frequency of words in text? - whenever you...