Quick Index
Overview


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

We're going to code a number that can do some brainy things.

Instructions



Class


Add a class named BrainyNumber.

Remember Java is picky and case sensitive. So it must be "BrainyNumber" and not "brainynumber", and not "Brainynumber", etc.

Instance Variable


Add one private instance variable named myNumber of type int.

This ivar will be used by Brainy's methods that do logic and calcs (below).

Assume that myNumber will only be a positive int (i.e. do not worry about zero or negative numbers)

Constructors


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

Standard Constructors:


Reminder: arg and parameter are used interchangeably.

Static Factory Constructor
BrainyNumber 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 BrainyNumber from(int n) {
	return new BrainyNumber(n);
}


Instance Method Definitions (Explained)


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

Instance Method Definition (Explained)


An example method definition is shown here:

tripled
Returns our number tripled. Return type is int.
//Example Usage

BrainyNumber bn = new BrainyNumber(6);
prn("tripled -- 6: " + bn.tripled());
//tripled -- 6: 18

Notes Pertaining to the Method Definition Above:


Instance Methods


The following lists the methods to code into the BrainyNumber 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.
public String toString() {
	return String.format("BrainyNumber(%d)", this.myNumber);
}
Returns the value of the ivar named "myNumber". This simple method is called a "getter" because it gets the ivar.
//Example Usage

BrainyNumber bn = new BrainyNumber(11);
prn("BrainyNumber is: " + bn.getMyNumber());
//Output should be:
// BrainyNumber is: 11
Returns true if our number is greater than the passed method parameter (an int).
//Example Usage

BrainyNumber bn = new BrainyNumber(12);
prn("Is BrainyNumber greater than 5: " + bn.isGreaterThan(5));
//Output should report true
Returns our number tripled. Return type is int.
BrainyNumber bn = new BrainyNumber(6);
prn("tripled -- 6: " + bn.tripled());
//tripled -- 6: 18
Returns true if this number is easily divisible.
We define "easily divisible" as divisible by any factor between 2 and 7 */
//Example Usage

BrainyNumber bn = new BrainyNumber(15);
prn("EasilyDivisible -- 15: " + bn.isEasilyDivisible());
//EasilyDivisible -- 15: true
bn = new BrainyNumber(11);
prn("EasilyDivisible -- 11: " + bn.isEasilyDivisible());
//EasilyDivisible -- 11: true
Returns the abcFactor of myNumber

We compute an abcFactor as follows (in order):

  • Initialize abcFactor to 2 if we are greater than 2, else initialize abcFactor to 1
  • If we are greater than 10, then increment abcFactor by 10
  • If we are greater than 100, then increment abcFactor by 100
  • If our power to the exponent 3 is greater than 30, increment abcFactor by 30
  • If we are easily divisible, decrement abcFactor by 50
  • If abcFactor is negative,
    set abcFactor to -abcFactor (i.e., invert abcFactor)
  • Return abcFactor
Example:
	What is the abcFactor of the number 15?
Answer:
	abcFactor is 8 (using the given computation steps)
Example:
	What is the abcFactor of the number 1?
Answer:
	abcFactor is 1 (using the given computation steps)
This method receives one param which is the exponent to power this number by.

Return the power of my number to the param (exponent).

The exponent is an abbreviation for repeated multiply.
Example:
	base (my number) = 2
	exponent (param) = 5
Answer
	power = 2 * 2 * 2 * 2 * 2 = 32
Return sum of all positive numbers less than me (my number)
Example:
	my number = 5
Answer
	sum = 1 + 2 + 3 + 4 = 10;


Download Source Code