Question: I'm not sure how to fix the error that this is currently throwing at me. The instructions for it are below! Write a class named
I'm not sure how to fix the error that this is currently throwing at me. The instructions for it are below!
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.
Main.java
public class Main { public static void main(String[] args) { //Finite up System.out.println(" Finite up 3 5 7...11"); Range r = new Range(3,2,11); printRange(r); //Finite down System.out.println(" Finite down 2 0 -2...-6"); r = new Range(2,-2,-6); printRange(r);
//Infinite up System.out.println(" Infinite up by 5s 3 8 13..."); r = new Range(3,5); printRange(r);
//Infinite down System.out.println(" Infinite down by 100s 1000 900 800..."); r = new Range(1000,-100); printRange(r);
//Infinite zero increment System.out.println(" Infinite zero increment 3 3 3 3..."); r = new Range(3, 0); printRange(r);
//Error bad sequence up System.out.println(" Error bad sequence up: 0 2 1"); try { r = new Range(33, 2, 1); printRange(r); System.out.println("PROBLEM: This line of code should never be reached."); } catch (IllegalArgumentException e) { System.out.println("Successfully caught exception."); } //Error bad sequence down System.out.println(" Error bad sequence down: 44 -12 80"); try { r = new Range(44, -12, 80); printRange(r); System.out.println("PROBLEM: This line of code should never be reached."); } catch (IllegalArgumentException e) { System.out.println("Successfully caught exception."); } //Error zero increment System.out.println(" Error zero increment"); try { r = new Range(44, 0, 80); printRange(r); System.out.println("PROBLEM: This line of code should never be reached."); } catch (IllegalArgumentException e) { System.out.println("Successfully caught exception."); } } private static void printRange(Range r) { //Print 30 or ferer items in sequence for(int i=0; i<30; i++) { if(r.hasNext()) { System.out.printf("%7d", r.next()); } else { break; } } if(r.hasNext()) { System.out.printf("%7s","..."); } System.out.println(); } }
Range.java
import java.lang.IllegalArgumentException;
public class Range { private int current; private final int increment; private final int end; private boolean infinite;
public Range (int start, int increment) //first constructor with two integers as input { this.current = start; this.increment = increment; this.infinite = true; }
public Range (int start, int increment, int end) //second constructor with three integers as input { if(increment == 0) { throw new IllegalArgumentException("Increment cannot be zero."); } else if (start < end && increment < 0) { throw new IllegalArgumentException("Increment cannot be negative when start is less than the end."); } else if (start > end && increment > 0) { throw new IllegalArgumentException("Increment cannot be positive when start is greater than end."); } this.current = start; this.increment = increment; this.end = end; this.infinite = false; } public int next() //method to get the next value in the sequence { int next = current; current += increment; return next; } public boolean hasNext() //method to check if there are additional values //yet to return in the sequence T/F { return infinite || current <= end; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
