Difference between revisions of "Iterable Range Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Code To Investigate=
+
=Java=
==Iterable==
+
[https://www.w3schools.com/java/java_methods_overloading.asp Method Overloading]
:[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html interface java.lang.Iterable<T>]
 
::[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#iterator-- Iterator<T> iterator()]
 
  
==Iterator==
+
[https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html Nested Inner Classes]
:[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html interface java.util.Iterator<T>]
+
 
::[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#hasNext-- boolean hasNext()]
+
[https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html Anonymous Classes]
::[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#next-- T next()]
+
 
 +
[https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html Lambda Expressions]
 +
 
 +
interface [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html java.lang.Iterable<T>]
 +
:[https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#iterator-- iterator()]
 +
 
 +
interface [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html java.util.Iterator<T>]
 +
:[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#hasNext-- hasNext()]
 +
:[https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#next-- next()]
  
 
=Code To Implement=
 
=Code To Implement=
==minToMaxExclusive==
+
{{JavaToImplement|Iterables|range<br/>range<br/>range<br/>range|range.warmup}}
{{JavaToImplement|Ranges|minToMaxExclusive|range.assignment}}
+
 
 +
Each of the overloaded range methods must return an Iterable of the appropriate generic type.  We have provided nested inner classes for IntegerRangeIterator and IntegerRangeIterable.  Feel free to implement these or delete them if you would prefer to build your Integer range methods with anonymous inner classes and or a lambda and an anonymous inner class.  For the Double range methods, you can define your own named nested inner classes or got the anonymous inner class/lambda route as you wish.
 +
==Integers==
 +
===range(min, maxExclusive, step)===
 +
public static Iterable<Integer> range(int min, int maxExclusive, int step)
 +
 
 +
===range(min, maxExclusive)===
 +
public static Iterable<Integer> range(int min, int maxExclusive)
 +
 
 +
range with a step of 1
 +
==Doubles==
 +
===range(min, maxExclusive, step)===
 +
public static Iterable<Double> range(double min, double maxExclusive, double step)
 +
===range(min, maxExclusive)===
 +
public static Iterable<Double> range(double min, double maxExclusive)
  
<nowiki>public static Iterable<Integer> minToMaxExclusive(int min, int maxExclusive)</nowiki>
+
range with a step of 1.0
  
=Example=
+
=Client=
{{Example|RangesExample|range.example}}
+
{{Example|RangeClient|range.client}}
  
 
The code:
 
The code:
  <nowiki>for(int i : Ranges.minToMaxExclusive(4, 12)) {
+
  <nowiki>for (int i : Iterables.range(4, 12)) {
 
System.out.println(i);
 
System.out.println(i);
 +
}
 +
System.out.println();
 +
for (double d : Iterables.range(3.0, 7.1, 0.25)) {
 +
System.out.println(d);
 
}</nowiki>
 
}</nowiki>
  
Line 31: Line 55:
 
9
 
9
 
10
 
10
11</nowiki>
+
11
 +
 
 +
3.0
 +
3.25
 +
3.5
 +
3.75
 +
4.0
 +
4.25
 +
4.5
 +
4.75
 +
5.0
 +
5.25
 +
5.5
 +
5.75
 +
6.0
 +
6.25
 +
6.5
 +
6.75
 +
7.0</nowiki>
  
 
=Test=
 
=Test=
{{TestSuite|RangesTestSuite|range.assignment}}
+
{{TestSuite|IterableRangeTestSuite|range.warmup}}

Latest revision as of 03:52, 26 November 2022

Java

Method Overloading

Nested Inner Classes

Anonymous Classes

Lambda Expressions

interface java.lang.Iterable<T>

iterator()

interface java.util.Iterator<T>

hasNext()
next()

Code To Implement

class: Iterables.java Java.png
methods: range
range
range
range
package: range.warmup
source folder: src/main/java

Each of the overloaded range methods must return an Iterable of the appropriate generic type. We have provided nested inner classes for IntegerRangeIterator and IntegerRangeIterable. Feel free to implement these or delete them if you would prefer to build your Integer range methods with anonymous inner classes and or a lambda and an anonymous inner class. For the Double range methods, you can define your own named nested inner classes or got the anonymous inner class/lambda route as you wish.

Integers

range(min, maxExclusive, step)

public static Iterable<Integer> range(int min, int maxExclusive, int step)

range(min, maxExclusive)

public static Iterable<Integer> range(int min, int maxExclusive) 

range with a step of 1

Doubles

range(min, maxExclusive, step)

public static Iterable<Double> range(double min, double maxExclusive, double step)

range(min, maxExclusive)

public static Iterable<Double> range(double min, double maxExclusive)

range with a step of 1.0

Client

class: RangeClient.java Presentation icon-72a7cf.svg
package: range.client
source folder: src/main/java

The code:

for (int i : Iterables.range(4, 12)) {
	System.out.println(i);
}
System.out.println();
for (double d : Iterables.range(3.0, 7.1, 0.25)) {
	System.out.println(d);
}

produces the output:

4
5
6
7
8
9
10
11

3.0
3.25
3.5
3.75
4.0
4.25
4.5
4.75
5.0
5.25
5.5
5.75
6.0
6.25
6.5
6.75
7.0

Test

class: IterableRangeTestSuite.java Junit.png
package: range.warmup
source folder: src/test/java