Index
Overview


In this challenge we're to follow up our SmartyNumber work and code a class that models a number range.

NOTE WELL -- Java has multiple types of primitive integers -- for NumberRange all ivars, numeric return data types, and numeric parameters will be type long, unless otherwise noted

Instructions



Class


Add a class named NumberRange.

Instance Variables


Add two private instance variables named "start" and "stop" (both of type long).

These numbers will be used by NumberRange's methods that do logic and calcs (below).

Constructors


These two methods are NumberRange's most important methods. The reason why is that object users will use these methods to construct SmartNumber objects -- we must be able to construct objects first to use them.

Standard Constructor
NumberRange has one standard constructor which has two parameters of type 'long'.

The first parameter should set the ivar "start" and the second parameter should set the ivar "stop".


Static Factory Constructor
NumberRange also has one static factory constructor.

It can be coded as shown here (copy-paste) -- you will learn details about these methods in advanced programming.
/** static factory constructor */
public static NumberRange fromStartStop(long start, long stop) {
	return new NumberRange(start, stop);
}


Range Rules and Assumptions


The start and stop are included in the range. E.g. if we have a range from start=10 and stop=12 the numbers in the range would be 10, 11 and 12.

You may assume the first param (passed to the constructor) will be greater than zero, and assume the second param (passed to the constructor) will be equal or greater than the first.

Instance Method Definitions (Explained)


Each Method Definition provides a definition of an instance methods that to be added (coded) to NumberRange.

Instance Method Definition (Explained)


An example method definition is shown here:

size
Returns the size (a long) of the range
Examples:

start=10 stop=12 then size=3
The size is 3 because the range includes:
10, 11 and 12

start=20 stop=20 then size=1
The size is 1 because the range includes:
20

Notes Pertaining to the Method Definition Above:


Instance Method Definitions - Core


This is the standard "toString" method. We add to help with debugging.

We can code it just like shown here.
public String toString() {
	return String.format("[%d, %d]", this.getStart(), this.getStop());
}
Returns the ivar named start
//Example Usage
NumberRange range = new NumberRange(10, 20);
println('Start: ' + range.getStart());
//Start: 10
Returns the ivar named stop
//Example Usage
NumberRange range = new NumberRange(10, 20);
println('Stop ' + range.getStop());
//Stop: 10
Sets the ivar named start with the passed parameter.

Receives one parameter, of data type long.
//Example Usage
NumberRange range = new NumberRange(10, 20);
println('Start: ' + range.getStart());
//Start: 10
range.setStart(1000);
println('Start: ' + range.getStart());
//Start: 1000
Sets the ivar named stop with the passed parameter.

Receives one parameter, of data type long.
//Example Usage
NumberRange range = new NumberRange(10, 20);
println('Stop: ' + range.getStop());
//Stop: 10
range.setStop(5000);
println('Stop: ' + range.getStop());
//Stop: 5000
Returns the size (an long) of the range
Examples:

start=10 stop=12 then size=3
The size is 3 because the range includes:
10, 11 and 12

start=20 stop=20 then size=1
The size is 1 because the range includes:
20



Returns true if our range includes the method parameter (which is type long).

Recall that our range includes the start and stop values.
Returns a new NumberRange object that is the intersection between this range and the parameter (which is another NumberRange).

For example if our range is 1-100, and the parameter's range is 91-200, then we would return a new NumberRange with a range of 91-100.

If there is no intersection, return a range with start and stop both equal to 0.

These Java methods may be handy to use:

Math.max(a, b); //returns max of a and b
Math.min(a, b); //returns min of a and b

Instance Method Definitions - Counts


Returns the count of even numbers in our range.
For a range from 0 to 100, then we would expect evenCount to be 51
Returns the count of odd numbers in our range.
For a range from 0 to 100, then we would expect oddCount to be 50
Returns the count of prime numbers in our range.

Several example counts are shown.
Examples:
StartStopPrimes Count
1104
110025
11000168
This method takes three parameters: (long factor1, long factor2, boolean mustBeBoth)

The param mustBeBoth drives the method logic like this:

mustBeBothDescription
trueReturn count of nums in our range that are divisible by both factor1 AND factor2
falseReturn count of nums in our range that are divisible by both factor1 OR factor2


Examples:

Range StartRange Stopfactor1factor2mustBeBothcountDivisibleBy (Resulting Count)
11025true1
11025false6
1100025true100
1100025false600


Returns a count of the numbers in our range that have a factorJKZ greater or equal to the parameter value passed into this method (the threshold/minimum).

Receives one method parameter that is a long type indicating the threshold (minimum) that will dictate if we include a number in the count.

The definition of factorJKZ can be found in SmartNumber.
Returns a count of the numbers in our range that have a factorRatio greater or equal to the parameter value passed into this method (the threshold/minimum).

Receives one method parameter that is a long type indicating the threshold (minimum) that will dictate if we include a number in the count.

The definition of factorRatio can be found in SmartNumber.
Returns a count of the numbers in our range that are palindromes.

The definition of isPalindrome can be found in SmartNumber.
Returns a count of the numbers in our range that both a square and a cube.

More information on square and cube numbers can be found in SmartNumber.

Instance Method Definitions - Other


Returns the sum of prime numbers in our range.

Several example counts are shown.
Range StartRange StopPrimes Sum
11017
11001,060
1100076,127
This method receives no params and returns the size (count) of the largest consecutive "gap" of non-primes in our range that are between two prime numbers (that also must be in our range).

For example, given the range 1 to 10, the largest non-prime consecutive gap is:

4
(count = 1)

Note that we do NOT count the non-primes 8, 9 and 10 (length 3) because they are not bound, on the right side, by a prime number (that must be in our range).

Another example is given the range 10 to 20, the largest non-prime consecutive gap is:

14, 15, 16
(count = 3)

Several example counts are shown.
Example Results:
StartStopLargest Prime Gap
330
440
1101
10203
1100019
10,00011,00031
100,000101,00053
Returns a string for the largest prime gap. Also see method description for largestPrimeGap. The string is:

prime1 + "-" + prime2

There are no spaces in the string.

For example if our range is 10-20, then the primes are 11, 13, 17, and 19. So the largest gap between any two primes in our range is between 13 and 17. So we would return:

“13-17”

If our range does not have any gap (i.e., zero or one primes), then return a string "0-0".

if our range has multiple gaps with the "largest prime gap" return the first.
StartStopLargest Prime
Gap Pair
1103-5
1207-11
102013-17
No return value.

Receives one parameter of type long which is a factor to use to shrink our range.

  • Shrink the range size (length) by the passed in factor (compute a new length using the factor)
  • Preserve our start position
  • Adjust the stop position to accommodate the newly computed length
Example:

Given a passed in parameter shrink factor of 3
Take our length and divide it by 3 to compute new length (round result)


Returns a sum of the factorJKZs for the numbers in our range.

The definition of factorJKZ can be found in SmartNumber.


  • Return a summary string of the NumberRange
  • Show one property per line
  • The example here shows four properties
  • You may use this example method as-is except add at least five more properties so that nine or more properties are displayed
public String getSummary() {
	String summary = "";
	String sep = System.lineSeparator();
	summary += "Start: " + this.getStart() + sep;
	summary += "Stop: " + this.getStop() + sep;
	summary += "Prime Count: " + this.countPrimes() + sep;
	summary += "Odd Count: " + this.oddCount() + sep;
	//TODO
	return summary;
	}

Grading


For this assignment, we will use these grading rules.