ava Help Please! The Triangle series is a mathematical sequence of integers defined by the formula: triangle_number = Example: if we start with the integer 1, the series is: 1, 3, 6, 10, 15, 21, 28, 36, … Write a TriangleSeries class that implements Iterable. The TriangleSeries must have a constructor that allows us to specify the “start” number in the (sub) series we want to calculate, and the last number in the (sub)series. Also create a TriangleSequenceIterator that returns the proper integer in the series. Assume the starting value for the series is whatever start-value the client specified when they instantiated the TriangleSeries instance. Your iterator should return false for hasNext() when the “end value” is reached. For your convenience, here is the Iterator interface: interface Iterator { boolean hasNext(); E next(); } ------ import java.util.Iterator; /** * Triangle Series Rule: t = n * (n + 1) / 2 */ public class TriangleSeries implements Iterable { // YOU write this class private class TriangleSeriesIterator ...{ // YOU write this class TOO! } } // Example public class IteratorMain { public static void main(String [] args) { TriangleSeries triangleSeries = new TriangleSeries(2, 9); Iterator iterator = triangleSeries.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } } } Produces the following:
Java Help Please!
The Triangle series is a mathematical sequence of integers defined by the formula:
triangle_number =
Example: if we start with the integer 1, the series is: 1, 3, 6, 10, 15, 21, 28, 36, …
Write a TriangleSeries class that implements Iterable. The TriangleSeries must have a constructor that allows us to specify the “start” number in the (sub) series we want to calculate, and the last number in the (sub)series.
Also create a TriangleSequenceIterator that returns the proper integer in the series. Assume the starting value for the series is whatever start-value the client specified when they instantiated the TriangleSeries instance. Your iterator should return false for hasNext() when the “end value” is reached.
For your convenience, here is the Iterator interface:
interface Iterator<E> {
boolean hasNext();
E next();
}
------
import java.util.Iterator;
/**
* Triangle Series Rule: t = n * (n + 1) / 2
*/
public class TriangleSeries implements Iterable<Integer> {
// YOU write this class
private class TriangleSeriesIterator ...{
// YOU write this class TOO!
}
}
// Example
public class IteratorMain {
public static void main(String [] args) {
TriangleSeries triangleSeries = new TriangleSeries(2, 9);
Iterator<Integer> iterator = triangleSeries.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Produces the following:
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images