Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Assignment: The Range Class Objectives Creating classes Using instance variables and methods Checking for illegal method arguments Throwing exceptions Description Write a class for

Programming Assignment:

The Range Class Objectives Creating classes Using instance variables and methods Checking for illegal method arguments Throwing exceptions Description Write a class for generating a sequence of numbers. The sequence could be increasing or decreasing. The sequence could be infinite! Instructions 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.

o0o

CODE:

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: 33 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 fewer 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(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions