Question
I need help with this Java question. Please provide the final code and output if possible. Part 2: A Range class The Python programming language
I need help with this Java question. Please provide the final code and output if possible.
Part 2: A Range class
The Python programming language contains a range() function that returns a sequence of integers that start from 0 (by default), increment by 1 (by default), and stop before a specified integer. Some examples:
range(3) returns a sequence representing 0, 1, 2 (starts from 0, increments by 1, and stops before 3).
range(3, 6) returns a sequence representing 3, 4, 5 (starts from 3, increments by 1, and stops before 6).
range(3, 10, 2) returns a sequence representing 3, 5, 7, 9 (starts from 3, increments by 2, and stops before 10).
As an example usage, the following Python code prints "hello" three times:
for i in range(3):
print("hello")
As another example, the following Python code prints the numbers 3, 5, 7, and 9:
for i in range(3, 10, 2):
print(i)
The goal in this exercise is to write a class named Range that will allow for something very similar to work in Java. Here are some sample uses of the Range class:
// prints: 0 1 2 3 4 5
for (int i : new Range(6)) {
System.out.print(i + " ");
}
// prints: 7 8 9 10 11
for (int i : new Range(7, 12)) {
System.out.print(i + " ");
}
// prints: 7 9 11
for (int i : new Range(7, 12, 2)) {
System.out.print(i + " ");
}
// prints: 7 9 11
for (int i : new Range(7, 13, 2)) {
System.out.print(i + " ");
}
To make these examples work, the Range class must have appropriate constructors. The class must implement the Iterable interface, which implies that the class must have an iterator() method that returns an Iterator. The Iterator should be an object of a private inner class that implements the Iterator interface. When implementing the Iterator interface, the only methods you are required to override are hasNext() and next(), but remove() is an optional method and is not needed here.
Note: You can assume that the starting integer is less than the ending integer and that the increment integer is positive. Alternatively (this is optional), allow for things like new Range(12, 7, -2), which should represent the sequence 12, 10, 8.
Note: Do not use an array, FancyArray, ArrayList, or any kind of collection in your solution to this exercise.
Optional: research the topic of records in Java, and make Range be a record class instead of a standard class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started