Quick Index
Overview


In this example we're going to make a class named BrainyNumberSpan.

Instructions



Class Description


* Hello I'm class BrainyNumberSpan
*
* I represent a span starting at number 'left'
* and ending on number 'right'
*
* For example, if my 'left' = 10
* and my 'right' = 15
*
* Then I span five numbers (10, 11, 12, 13, and 14)
*
* Note that the 'left' boundary is *included* in the span.
* Note that the 'right' boundary is *excluded* in the span.
*
* My purpose is to provide a user friendly methods to
* operate over my span
*
* The ivars left and right are non-negative integers

Class


Add a class named BrainyNumberSpan.

Remember Java is picky and case sensitive. So it must be "BrainyNumberSpan" and not "brainyNumberSpan", and not "brainynumberspan", etc.

Instance Variables


Add two private ivars named left and right, both of type int.

Also see section "Class Description" above

Constructors


Brainy number has two standard constructors and one static factory constructor.

Standard Constructors:


Reminder: arg and parameter are used interchangeably.


Static Factory Constructor
BrainyNumberSpan 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 BrainyNumberSpan fromLeftRight(int aLeft, int aRight) {
	return new BrainyNumberSpan(aLeft, aRight);
}


Instance Method Definitions (Explained)


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

Instance Method Definition (Explained)


An example method definition is shown here:

countEasilyDivisible
Returns the count of easily divisible numbers within this span of numbers.

The definition of easily divisible can be found in BrainyNumber.
Example:

For span (1, 10), countEasilyDivisible = 8

Notes Pertaining to the Method Definition Above:


Instance Methods


The following lists the methods to code into the BrainyNumberSpan class.

Reminder: the method headers are more important than the code itself. Here are tips for the method headers:


This is the standard toString method. Very helpful for debugging and more.

We can code it just like shown here.

We should add toString to all classes we code.
/** Simply return nice string */
@Override
public String toString() {
	return String.format("[%d - %d]", this.getLeft(), this.getRight());
}
Returns the value of the ivar left. This simple method is called a getter because it gets the ivar.
//Example Usage

BrainyNumber span = new BrainyNumberSpan(1, 10);
prn("Left: " + span.getLeft());
//Left: 1
Returns the value of the ivar right. This simple method is called a getter because it gets the ivar.
//Example Usage

BrainyNumber span = new BrainyNumberSpan(1, 10);
prn("Right: " + span.getRight());
//Right: 10
Returns the count of easily divisible numbers within this span of numbers.

The definition of easily divisible can be found in BrainyNumber.
Example:

For span (1, 10), countEasilyDivisible = 8
Returns the count of numbers within this span of numbers that have abcFactor greater than parameter threshold.

Receives one method parameter, threshold, an int.

The definition of abcFactor can be found in BrainyNumber.
Example:

For span (1, 101), countAbcFactorsGreaterThan(20) = 23
Returns the count of numbers within this span of numbers that have abcFactor less than parameter threshold.

Receives one method parameter, threshold, an int.

The definition of abcFactor can be found in BrainyNumber.
Example:

For span (1, 101), countAbcFactorsLessThan(5) = 1
Returns the sum of abcFactors within this span of numbers.

The definition of abcFactor can be found in BrainyNumber.
Example:

For span (11, 20), sumAbcFactors() = 208


Download Source Code