Question
Need help with the following in Java. Write a class named Range. The class will have two constructors and two methods: next and hasNext. The
Need help with the following in Java.
Write a class named Range. The class will have two constructors and two methods: next and hasNext.
The first constructor for Range takes two integers as input: a starting number and an increment number. This will produce an infinite sequence, which should be represented by a boolean instance variable.
For example, this line... Range r = new Range(0,5); ...creates a range representing the values: 0, 5, 10, 15, 20, 25, ... and so on forever.
The second constructor for Range takes three integers as input: a starting number, an increment number, and an ending number. This will produce a finite sequence.
For example, this line... Range r = new Range(3,2,11); ...creates a range representing the values: 3, 5, 7, 9, 11
The next method should return the next integer value in the sequence. The first call to next should return the first value in the sequence.
The hasNext method should return true if there are more values yet to return in the sequence and false otherwise.
Your code should throw an IllegalArgumentException in the following circumstances ( Note that you will need to import java.lang.IllegalArgumentException; ):
- A start and end are given, but the increment is zero. If zero is the increment for an infinite sequence, it's no problem.
- The start is less than the end, but the increment is negative.
- The start is greater than the end, but the increment is positive.
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