1 answer

How do I implement my own Iterator in Java?

Question:

this is what my assignment requires of me:

Requirement #2: Implementthe java.util.IteratorInterface

The java.util.Iterator interfaceis defined on-line at java.sun.com.You must implementan Iterator thatwill iterate over the items stored in your AVL tree. You couldimplement your Iterator using aclass that is nested inside of the class you use toimplementjava.util.Set.

You must fully implement the following methods:

boolean hasNext()

Object next()

Your implementation is allowed to throw UnsupportedOperationException for thefollowing method:

void remove()

Your implementation must conform to the required behavior ofeach method as defined in the on-line documentation. The definitionof java.util.Set allows anIteratorto return the items in theset in any order. Your implementation must meet an additionalrequirement. You must return the items in the order given by apreordertraversal of the AVL tree that is implementing theset. The order in which items are returned from the Iterator will be used to determine if yourtree isconstructed and balanced correctly.

The iterator method in the classthat implements java.util.Setshould return an Iterator object asdescribed in this section.

The toArray method in the classthat implements java.util.Set mustfill its array in the same order as described here for theIterator. In fact, you could useanIterator to help implement the toArray method.

--------------------------------------------------------------------------------------------------------
this is what I have so far the metho and the class are all nestedinside TreeImpl.java, may need some fixing and some implementation,any help would beappreciated:
public Iterator iterator() {
// TODOAuto-generated method stub
IteratorImpliter = new IteratorImpl();
return iter;
}
public class IteratorImplimplements java.util.Iterator<Object>{
publicIteratorImpl()
{
TreeImpl tree = new TreeImpl();
tree.iterator();
}
publicboolean hasNext() {
// TODO Auto-generated method stub
return false;
}
publicObject next() {
// TODO Auto-generated method stub
return null;
}
publicvoid remove() {
// TODO Auto-generated method stub
throw newUnsupportedOperationException();
}
}

Answers

Dear..Am giving you the sample one.
import java.util.Iterator;public class Iterator
implements Iterator
{ObjectnextObject;boolean hasNext = true;public Iterator(Object o){nextObject =o;}public booleanhasNext(){returnhasNext;}public Object next(){hasNext = false;return nextObject;}public void remove(){throw newUnsupportedOperationException();}}
.

Similar Solved Questions

1 answer
For the three second-row molecules, O2, CO, and NO, a) identify the molecular orbital that will o...
For the three second-row molecules, O2, CO, and NO, a) identify the molecular orbital that will overlap with the dz2 orbital on Fe(II) in hemoglobin b) predict the Fe-A-B bond angle (linear or bent) that forms when the ligand binds to the iron.  Do all three molecules yield the same bond a...
1 answer
If your textbook weighed 2 kg with dimensions 8.5 x 1 x 10.8 inches and was...
If your textbook weighed 2 kg with dimensions 8.5 x 1 x 10.8 inches and was lying flat on the table, what would the stress on the table top beneath it be?...
1 answer
A precision lathe costs $30,000 and will cost $40,000 a year to operate and maintain. If...
A precision lathe costs $30,000 and will cost $40,000 a year to operate and maintain. If the discount rate is 10% and the lathe will last for 5 years, what is the equivalent annual cost of the tool? (Do not round intermediate calculations. Round your answer to 2 decimal places. Enter your answer as ...
1 answer
The discovery of amber mutations by Sydney Brenner and colleagues was a group of mutations that...
The discovery of amber mutations by Sydney Brenner and colleagues was a group of mutations that were eventually shown to be.. a unusual amino acid that generated an amber color in the affected bacteria. b. a deletion mutation that creates a translational frameshift. Insertion mutation that creates a...
1 answer
Question 4 Which is a therapeutic use for verapamil? O Sick sinus syndrome Hypotension Cardiac dysrhythmias...
Question 4 Which is a therapeutic use for verapamil? O Sick sinus syndrome Hypotension Cardiac dysrhythmias Increase blood volume...
1 answer
Celsius reading varies directly on the difference between Fahrenheit reading and 32 F. If a Celsius thermometer reads 100 when a Fahrenheit reads 212°, how do you find the Celsius reading when the Fahrenheit reading is 32°?
Celsius reading varies directly on the difference between Fahrenheit reading and 32 F. If a Celsius thermometer reads 100 when a Fahrenheit reads 212°, how do you find the Celsius reading when the Fahrenheit reading is 32°?...
1 answer
What is the distance between #(3, 6, 2) # and #(0, 6, 0) #?
What is the distance between #(3, 6, 2) # and #(0, 6, 0) #?...
1 answer
4 7. A certain batch of fireflies were observed to "flash" at the rate of 10.3...
4 7. A certain batch of fireflies were observed to "flash" at the rate of 10.3 times per minute at 25°C, and at the lower rate of 5.6 times a minute at a temperature of 15°C. Assume that the flashing is the result of an overall chemical reaction that has a single rate limiting step w...
1 answer
Please solve using successive approximations. please show all steps so I may be able to follow...
please solve using successive approximations. please show all steps so I may be able to follow along Cadmium metal is added to 0.350 L of an aqueous solution in which (C13-J=1.00M. 2Cr3+(aq)+Cd(s)=2Cr2+(aq)+Cd2+(aq)KC=0.288 What is the concentration of Cr2+ at equilibrium? What is the concentration ...
1 answer
After activation by a coupled receptor, G-proteins directly regulate which of the following? 1) Ion channels...
After activation by a coupled receptor, G-proteins directly regulate which of the following? 1) Ion channels 2) Nuclear receptor superfamily members 3) Smads 4) PKC and PKA...
1 answer
2 Question: According to the following Figure, what is the value of time response for the...
2 Question: According to the following Figure, what is the value of time response for the open loop? (2 Points) R(s) Y(s) K G(s) H(s) 2 K = 10, G (s) = -549 H(s) = 3, R (s) is unit step input function Enter your answer...
1 answer
How do you graph #y=25x+150#?
How do you graph #y=25x+150#?...
1 answer
Combining 0.3140.314 mol of Al4C3 with excess water produced 96.196.1 g of Al(OH)3. Al4C3(s)+12H2O(l)⟶3CH4(g)+4Al(OH)3(s)Al4C3(s)+12H2O(l)⟶3CH4(g)+4Al(OH)3(s) What is...
Combining 0.3140.314 mol of Al4C3 with excess water produced 96.196.1 g of Al(OH)3. Al4C3(s)+12H2O(l)⟶3CH4(g)+4Al(OH)3(s)Al4C3(s)+12H2O(l)⟶3CH4(g)+4Al(OH)3(s) What is the actual yield of aluminum hydroxide in moles? What is the theoretical yield of aluminum hydroxide in moles? What is...
1 answer
12. Explain why a Claisen reaction occurs with ester E, but not with ester F. Eto...
12. Explain why a Claisen reaction occurs with ester E, but not with ester F. Eto EIO E F 13. Give the major organic product of each Claisen reaction. In some cases, no reaction is expected. a. NaOEt a. NaoMe OMe b. mild H Et01 b. mild H a. NaOMe a. NaOme. Meo MeO b. mild H MeO b. mild Hº 13 co...