Difference between revisions of "PowersOf2Iterable"
Line 54: | Line 54: | ||
=Testing Your Solution= | =Testing Your Solution= | ||
==Correctness== | ==Correctness== | ||
− | {{TestSuite| | + | {{TestSuite|_PowersOfTwoTestSuite|powersoftwo.studio}} |
=Pledge, Acknowledgments, Citations= | =Pledge, Acknowledgments, Citations= | ||
{{Pledge|studio-powers-of-two-iterable}} | {{Pledge|studio-powers-of-two-iterable}} |
Revision as of 18:59, 28 January 2022
Contents
Motivation
Experience with implementing interfaces.
Investigate
interface Iterable<T>
interface Iterator<T>
Mistakes To Avoid
Warning: Do NOT Parallelize |
Warning: Do NOT confuse Iterable<T> from Iterator<T> |
How To Use
for(int v : new PowersOfTwoLessThan(71)) { System.out.println(v); }
Code to Implement
It is important to consider what state each class should encapsulate. The class PowersOfTwoLessThan, which is Iterable, should be have need to store little (if anything) beyond the maxExclusive passed in to its constructor. Further, PowersOfTwoLessThan should be immutable. There is no need to change any of the data during its lifetime.
The Iterator<Integer> instances created whenever one invokes the iterator() method? Now, the data for those instances are going to need to mutate.
One should NOT need to create a List to support this class. A single private final int instance variable is all you need for PowersOfTwoLessThan and a single (mutable) int instance variable is all you need for its Iterator.
class: | PowersOfTwoLessThan.java | |
methods: | constructor iterator |
|
package: | powersoftwo.studio | |
source folder: | student/src/main/java |
constructor
method: public PowersOfTwoLessThan(int maxExclusive)
(sequential implementation only)
iterator
method: public Iterator<Integer> iterator()
(sequential implementation only)
Investigate: Anonymous Inner Classes
Testing Your Solution
Correctness
class: | _PowersOfTwoTestSuite.java | |
package: | powersoftwo.studio | |
source folder: | testing/src/test/java |
Pledge, Acknowledgments, Citations
file: | studio-powers-of-two-iterable-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge