Difference between revisions of "PowersOf2Iterable"

From CSE231 Wiki
Jump to navigation Jump to search
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
=Motivation=
 
=Motivation=
Experience with implementing interfaces.
+
* Gain experience with implementing interfaces.
 +
* Build a utility which will come in handy later in the semester (for example, the [[Scan]] exercise).
 +
 
 +
<!--
 +
=Warm Ups=
 +
If you are struggling with this studio, consider completing one or both of the warm ups
 +
==Shape==
 +
Complete the [[Shape]] warm up to gain experience with implementing an interface, as well as declaring and using fields.
 +
 
 +
==DoubleRange==
 +
The [[DoubleRange]] warm up to gain experience implementing an Iterable<T> without simply containing a List<T> which holds the values.  Further, you can gain experience creating instances of an anonymous class.
 +
 
 +
<youtube>Dww-_5EiwA8</youtube>
 +
-->
 +
 
 
=Investigate=
 
=Investigate=
[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html interface Iterable<T>]
+
[https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html anonymous classes]
  
[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html interface Iterator<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>]
 +
 
 +
{{CollapsibleYouTube|Implementing Iterable and Iterator|<youtube>4ycpqdJ4yb4</youtube>}}
  
 
=Mistakes To Avoid=
 
=Mistakes To Avoid=
 
{{warning | Do NOT Parallelize}}
 
{{warning | Do NOT Parallelize}}
 +
 +
{{warning | Do NOT confuse [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable<T>] from [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator<T>]}}
 +
 +
=Client=
 +
{{Client|PowersOfTwoLessThanClient|iteration.powersoftwo.client|main}}
 +
 +
{{CollapsibleCode|PowersOfTwoLessThanClient|
 +
<syntaxhighlight lang="java">
 +
for (int powerOf2 : new PowersOfTwoLessThan(231)) {
 +
System.out.println(powerOf2);
 +
}
 +
</syntaxhighlight>}}
 +
 +
{{CollapsibleConsole|PowersOfTwoLessThanClient Output|<pre style="border: 0px; background: #000; color:#fff;">1
 +
2
 +
4
 +
8
 +
16
 +
32
 +
64
 +
128</pre>}}
  
 
=Code to Implement=
 
=Code to Implement=
{{CodeToImplement|PowersOfTwoLessThan|constructor<br/>iterator|slices.studio}}
+
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.
 +
 
 +
{{CodeToImplement|PowersOfTwoLessThan|constructor<br/>iterator|iteration.powersoftwo.exercise}}
  
 
==constructor==
 
==constructor==
{{Sequential|public PowersOfTwoLessThan(int maxExclusive)}}
+
{{SequentialConstructor|public PowersOfTwoLessThan(int maxExclusive)}}
  
 
==iterator==
 
==iterator==
 
{{Sequential|public Iterator<Integer> iterator()}}
 
{{Sequential|public Iterator<Integer> iterator()}}
 +
 +
You can create an [https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html anonymous inner class] or a named class if you prefer.
 +
 +
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.
 +
 +
<syntaxhighlight lang="java">
 +
return new Iterator<>() {
 +
      // Implement/Override necessary methods
 +
};
 +
</syntaxhighlight>
  
 
=Testing Your Solution=
 
=Testing Your Solution=
 
==Correctness==
 
==Correctness==
{{TestSuite|PowersOfTwoTestSuite|powersoftwo.studio}}
+
{{TestSuite|_PowersOfTwoTestSuite|iteration.powersoftwo.exercise}}
 +
 
 +
=Pledge, Acknowledgments, Citations=
 +
{{Pledge|studio-powers-of-two-iterable}}

Latest revision as of 03:17, 18 August 2023

Motivation

  • Gain experience with implementing interfaces.
  • Build a utility which will come in handy later in the semester (for example, the Scan exercise).


Investigate

anonymous classes

interface Iterable<T>

interface Iterator<T>

Video: Implementing Iterable and Iterator  

Mistakes To Avoid

Attention niels epting.svg Warning: Do NOT Parallelize
Attention niels epting.svg Warning: Do NOT confuse Iterable<T> from Iterator<T>

Client

class: PowersOfTwoLessThanClient.java CLIENT
package: iteration.powersoftwo.client
source folder: student/src/main/java
PowersOfTwoLessThanClient  
for (int powerOf2 : new PowersOfTwoLessThan(231)) {
	System.out.println(powerOf2);
}
PowersOfTwoLessThanClient Output  
1
2
4
8
16
32
64
128

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 Java.png
methods: constructor
iterator
package: iteration.powersoftwo.exercise
source folder: student/src/main/java

constructor

constructor: public PowersOfTwoLessThan(int maxExclusive)

iterator

method: public Iterator<Integer> iterator() Sequential.svg (sequential implementation only)

You can create an anonymous inner class or a named class if you prefer.

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

Correctness

class: _PowersOfTwoTestSuite.java Junit.png
package: iteration.powersoftwo.exercise
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