Difference between revisions of "DoubleDeltaRange"
m (→iterator()) |
|||
(6 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
=Motivation= | =Motivation= | ||
− | Experience implementing the Iterable<T> | + | Experience implementing the [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable<T>] and [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator<T>] interfaces. |
=Mistakes To Avoid= | =Mistakes To Avoid= | ||
Line 6: | Line 6: | ||
=Investigate= | =Investigate= | ||
− | interface [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable<T>] | + | * interface [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable<T>] |
+ | * interface [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator<T>] | ||
− | + | <youtube>4ycpqdJ4yb4</youtube> | |
− | |||
− | <youtube> | ||
=Code to Implement= | =Code to Implement= | ||
Line 16: | Line 15: | ||
To implement this class you will need to declare and initialize fields. You should '''NOT''' create a List<Double> and fill it with the necessary values. The iterator should produce each next value live. | To implement this class you will need to declare and initialize fields. You should '''NOT''' create a List<Double> and fill it with the necessary values. The iterator should produce each next value live. | ||
− | <pre>class | + | <pre>class DoubleDeltaRange implements Iterable<Double></pre> |
− | {{CodeToImplement| | + | {{CodeToImplement|DoubleDeltaRange|constructor<br/>iterator()|iteration.doubledelta.group}} |
===constructor=== | ===constructor=== | ||
− | {{Sequential|public | + | {{Sequential|public DoubleDeltaRange(double minInclusive, double maxExclusive, double delta)}} |
===iterator()=== | ===iterator()=== | ||
Line 27: | Line 26: | ||
Although, you can create a named class, we encourage you to use this opportunity to create an instance of an [https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html anonymous class] here. | Although, you can create a named class, we encourage you to use this opportunity to create an instance of an [https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html anonymous class] here. | ||
+ | |||
+ | Note: if you type the following, and hover over the red line, IntelliJ will offer you to "Implement methods" necessary to create an anonymous inner class. You only need to override the '''hasNext()''' and '''next()''' methods. | ||
+ | <nowiki>return new Iterator<>() { | ||
+ | // Implement/Override necessary methods | ||
+ | };</nowiki> | ||
=Testing Your Solution= | =Testing Your Solution= | ||
+ | ==Client== | ||
+ | {{Client|DoubleDeltaRangeClient|iteration.doubledelta.client|main}} | ||
+ | |||
+ | <nowiki>DoubleDeltaRange range = new DoubleDeltaRange(3.0, 5.0, 0.25); | ||
+ | for (double d : range) { | ||
+ | System.out.println(d); | ||
+ | }</nowiki> | ||
+ | |||
+ | should produce the output: | ||
+ | |||
+ | <nowiki>3.0 | ||
+ | 3.25 | ||
+ | 3.5 | ||
+ | 3.75 | ||
+ | 4.0 | ||
+ | 4.25 | ||
+ | 4.5 | ||
+ | 4.75</nowiki> | ||
+ | |||
==Correctness== | ==Correctness== | ||
− | {{TestSuite| | + | {{TestSuite|_DoubleDeltaRangeTestSuite|iteration.doubledelta.group}} |
Latest revision as of 21:35, 31 January 2023
Contents
Motivation
Experience implementing the Iterable<T> and Iterator<T> interfaces.
Mistakes To Avoid
Warning: Do NOT Parallelize |
Investigate
- interface Iterable<T>
- interface Iterator<T>
Code to Implement
DoubleRange
To implement this class you will need to declare and initialize fields. You should NOT create a List<Double> and fill it with the necessary values. The iterator should produce each next value live.
class DoubleDeltaRange implements Iterable<Double>
class: | DoubleDeltaRange.java | |
methods: | constructor iterator() |
|
package: | iteration.doubledelta.group | |
source folder: | student/src/main/java |
constructor
method: public DoubleDeltaRange(double minInclusive, double maxExclusive, double delta)
(sequential implementation only)
iterator()
method: public Iterator<Double> iterator()
(sequential implementation only)
Although, you can create a named class, we encourage you to use this opportunity to create an instance of an anonymous class here.
Note: if you type the following, and hover over the red line, IntelliJ will offer you to "Implement methods" necessary to create an anonymous inner class. You only need to override the hasNext() and next() methods.
return new Iterator<>() { // Implement/Override necessary methods };
Testing Your Solution
Client
class: | DoubleDeltaRangeClient.java | CLIENT |
package: | iteration.doubledelta.client | |
source folder: | student/src/main/java |
DoubleDeltaRange range = new DoubleDeltaRange(3.0, 5.0, 0.25); for (double d : range) { System.out.println(d); }
should produce the output:
3.0 3.25 3.5 3.75 4.0 4.25 4.5 4.75
Correctness
class: | _DoubleDeltaRangeTestSuite.java | |
package: | iteration.doubledelta.group | |
source folder: | testing/src/test/java |