In: Computer Science
In Java, we typically iterate over a sequence of integers as follows:
for (int i=0; i<10; i = i + 2) { ... }
Suppose we use a version of Java that only supports for-each
loops, i.e. it only allows you to iterate through the elements of
an Iterable
In order to still be able to iterate over a sequence of numbers,
you need to create such an Iterable, which we will call Range.
Modify the class Range.java so that it can be used as illustrated in TestRange.java (in the bottom).
The arguments passed to the constructor of Range are new Range(int start, int stop, int step).
Range.java:
import java.util.Iterator;
/**
* A Range iterable that can be used to iterate over a sequence of
integers
* (similar to Python's range function).
*/
public class Range implements Iterable<Integer> {
// you probably need some variables here and an inner class.
public Range(int start, int stop, int step) {
// change this
}
public Iterator<Integer> iterator() {
return null; // change this
}
}
TestRange.java
public class TestRange {
public static void main(String[] args) {
System.out.println("Range(1,8,1) should print 1 2 3 4 5 6
7");
for(Integer j : new Range(1,8,1)) {
System.out.print(j);
System.out.print(" ");
}
System.out.println("\n");
System.out.println("Range(1,8,2) should print 1 3 5 7");
for(Integer j : new Range(1,8,2)) {
System.out.print(j);
System.out.print(" ");
}
System.out.println("\n");
System.out.println("Range(8,0,-1) should print 8 7 6 5 4 3 2
1");
for(Integer j : new Range(8,0,-1)) {
System.out.print(j);
System.out.print(" ");
}
System.out.println("\n");
}
}
//Java code
import java.util.Iterator; import java.util.NoSuchElementException; /** * A Range iterable that can be used to iterate over a sequence of integers * (similar to Python's range function). */ public class Range implements Iterable<Integer> { // you probably need some variables here and an inner class. private int start, stop,step; public Range(int start, int stop, int step) { this.start =start; this.stop =stop; this.step = step; } public Iterator<Integer> iterator() { return new MyRangeIterator(); } //inner class private class MyRangeIterator implements Iterator<Integer> { private int position; public MyRangeIterator() { this.position = Range.this.start; } /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ @Override public boolean hasNext() { if(Range.this.step>0) return this.position <Range.this.stop; else return this.position >Range.this.stop; } /** * Returns the next element in the iteration. * * @return the next element in the iteration * @throws NoSuchElementException if the iteration has no more elements */ @Override public Integer next() { if(this.hasNext()) { int current = position; position = position +Range.this.step; return current; } throw new NoSuchElementException(); } } }
//==========================================
public class TestRange { public static void main(String[] args) { System.out.println("Range(1,8,1) should print 1 2 3 4 5 6 7"); for(Integer j : new Range(1,8,1)) { System.out.print(j); System.out.print(" "); } System.out.println("\n"); System.out.println("Range(1,8,2) should print 1 3 5 7"); for(Integer j : new Range(1,8,2)) { System.out.print(j); System.out.print(" "); } System.out.println("\n"); System.out.println("Range(8,0,-1) should print 8 7 6 5 4 3 2 1"); for(Integer j : new Range(8,0,-1)) { System.out.print(j); System.out.print(" "); } System.out.println("\n"); } }
//Output
//If you need any help regarding this solution .......... please leave a comment ........ thanks