Index
Overview


In this challenge we're going to make a smart number class.

Java provides primitive numbers -- that can handle +, - and *, and / (note the Java behavior of "/" with primitive integers is a bit non-intuitive). We'll code an enhanced number.

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

Instructions



Class


Add a class named SmartNumber.

Remember Java is case sensitive. So it must be "SmartNumber" and not "smartNumber", and not "Smartnumber", etc.

Instance Variable


Add one private ivar named "num" of type long.

Constructors


These two methods are SmartNumber'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
SmartNumber has one standard constructor which has a parameter which is type 'long'. This parameter should be used to set the ivar "num".


Static Factory Constructor
SmartNumber 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 SmartNumber from(long aNum) {
	return new SmartNumber(aNum);
}


Instance Method Definitions (Explained)


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

Instance Method Definition (Explained)


An example method definition is shown here:

isEven
Return true if our number is even, else false
Even numbers: 0, 2, 4, 6, 8, ...

//Example Usage
SmartNumber sn = new SmartNumber(12);
println("Number is even: " + sn.isEven());
//Number is even: true
sn = new SmartNumber(8097);
println("Number is even: " + sn.isEven());
//Number is even: false

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 "" + this.getNum();
}
Returns the ivar named num.
//Example Usage
SmartNumber sn = new SmartNumber(11);
println("SmartNumber is: " + sn.getNum());
//SmartNumber is: 11
Sets the value of the ivar named num.
//Example Usage
SmartNumber sn = new SmartNumber(11);
println(sn.toString());
//11
sn.setNum(1003);
println(sn.toString());
//1003
Returns true if our number is even, else false
Even numbers: 0, 2, 4, 6, 8, ...

//Example Usage
SmartNumber sn = new SmartNumber(12);
println("Number is even: " + sn.isEven());
//Number is even: true
sn = new SmartNumber(8097);
println("Number is even: " + sn.isEven());
//Number is even: false
Returns true if our number is odd, else false
Odd numbers: 1, 3, 5, 7, 9, ...

//Example Usage
SmartNumber sn = new SmartNumber(12);
println("Number is even: " + sn.isOdd());
//Number is even: false
sn = new SmartNumber(8097);
println("Number is even: " + sn.isOdd());
//Number is even: true
Returns the rounded result of dividing this number by the method parameter.

This method enhances Java's integer division. E.g., Java, for the integer division (9/5), will give us the result 1. The divideAndRound method will give us the intuitive result 2).

This method takes one method parameter of type long which is a divisor we divide by. Assume it will be a positive integer.

We'll return the quotient (division result) rounded to the nearest integer like the examples demonstrate.

This is how we divide and round ints in Java:

//Given primitive integers "a" and "b" (either int or long)
long q = Math.round((double)a / b);
Examples:
4/5 = 1
5/5 = 1
7/5 = 1
8/5 = 2
9/5 = 2
19/5 = 4

Example Use:
SmartNumber sn = new SmartNumber(9);
println("Result of 9/5 = " + sn.divideAndRound(5));
//Result of 9/5 = 2
Returns true if our number is a square number
First five squares:

  • 1
  • 4
  • 9
  • 16
  • 25
//Example Usage SmartNumber sn = new SmartNumber(9); println("Number is prime: " + sn.isSquare()); //Number is cube: true sn = new SmartNumber(10); println("Number is prime: " + sn.isSquare()); //Number is cube: false
Returns true if our number is a cube number
First five cubes:

  • 1
  • 8
  • 27
  • 64
  • 125
//Example Usage SmartNumber sn = new SmartNumber(125); println("Number is prime: " + sn.isCube()); //Number is cube: true sn = new SmartNumber(126); println("Number is prime: " + sn.isCube()); //Number is cube: false
Returns true if our number is a square AND a cube.
Returns true if our number is divisible by the method parameter.

Receives one method parameter, a long, which is a candidate factor. Assume it will be a positive integer.

Note for all candidate factors greater than 10, isDivisibleBy is false.
Examples:

NumberCandidate Factor
(Method Parameter)
isDivisibleBy
201true
202true
203false
204true
205true
206false
207false
208false
209false
2010true
2011false
20etcfalse
Returns the square of this number.
Examples:

Numbersquared
11
24
39
416
525
Returns the cube of this number.
Examples:

Numbersquared
11
28
327
464
5125
Returns the remainder of this number divided by the method parameter.

Receives one method parameter which is a divisor to divide by, it is a long type. Assume it will be a positive integer.
Examples:

Numberdivisor
Method Parameter
remainder
1010
1020
1031
1042
1050
1064
1073
1082
1091
10100
101110
101210
101310
101410
101510
The roundedRatio method behaves the same way as the divideAndRound method.

See the divideAndRound definition for a description of expected method behavior, and for method return and parameter types.
See examples for divideAndRound

Instance Method Definitions - Yes-No Questions


Returns true if our number is a prime number, otherwise false.

A prime number has no factors excluding 1 and itself (e.g., the prime 11 has no factors greater than 1 and less than 11)

A prime number does not have any factors (excluding 1 and itself).

  • The number 10 is not prime because it has factors 2 and 5
  • The number 11 is prime because it has no factors excluding 1 and itself

If a number is a not a composite, it is a prime, and vice-versa.

We should sketch our solution ideas out on paper first -- how might we use a loop to determine if a number is prime?
First eleven primes:

  • 2
  • 3
  • 5
  • 7
  • 11
  • 13
  • 17
  • 19
  • 23
  • 29
  • 31
//Example Usage SmartNumber sn = new SmartNumber(17); println("Number is prime: " + sn.isPrime()); //Number is prime: true sn = new SmartNumber(18); println("Number is prime: " + sn.isPrime()); //Number is prime: false
Returns true if our number is a composite number, otherwise false.

A composite number has factors smaller than itself and greater than 1 (e.g., composite 10 has factors 2 x 5)

A composite number is greater than 1.

If a number is a not a prime, it is a composite, and vice-versa.
First five composites:

Returns true if our number is a palindromic number, otherwise false.

A palindromic number reads the same backwards and forwards.

Careful with spelling -- isPalindrome -- best to copy-pate the method name into your code editor.
Examples:

NumberisPalindrome
1true
2true
10false
11true
107false
151true
87378true
This method takes one param (a long) which is the other number to use to determine if our num and the param form an amicable pair.

An amicable number pair is a fun combo where the sum of the proper divisors for each equal the other number.
See the examples shown.

  • The sum of the proper divisors of 220 are 284.
  • The sum of the proper divisors of 284 are 220.
  • Thus 220 and 284 are amicable numbers.

Proper divisors are simply the divisors of any number excluding the number itself (but including 1).

  • Proper divisors of 12: 1, 2, 3, 4, 6
  • Proper divisors of 20: 1, 2, 4, 5, 10
num1num2is amicable pair?
220284yes
284220yes
26202924yes
29242620yes

See:


Instance Method Definitions - Counts


Returns the count of all unique factors for our number. We include 1 and our number.

Example counts are shown.
Examples:

NumberUnique FactorsFactor Count
61, 2, 3, 64
91, 3, 93
111, 112
121, 2, 3, 4, 6, 126
301, 2, 3, 5, 6, 10, 15, 308
Returns the count of all unique prime factors for our number. We do not include 1 or our number in the count.

Example counts are shown.

Tip: Remember we can construct objects in a loop. E.g. you could construct a SmartNumber in a loop.
Examples:

NumberUnique Prime FactorsPrime Factor Count
62, 32
931
11None0
122, 32
302, 3, 53
31None0
2102, 3, 5, 74

Instance Method Definitions - Other


Returns the factorial of this number.

Do not use any Java Math library methods for your factorial computation.
A factorial is n*(n-1)*(n-2)...2*1

The factorial of 4 is:

4 * 3 * 2 * 1 = 24

The factorial of 10 is:

10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800

Note that the factorial of zero is defined to be 1
Returns the rounded ratio of our factorCount divided by our primeFactorCount.

If primeFactorCount is zero, then returns -1.
Examples can be derived using the examples provided for factorCount and primeFactorCount.
Returns a number based on a set of rules defined below.

  • If we are prime, then simply return our number as-is, otherwise, we'll calculate factorJKZ using these steps:
  • If our number is even, let factorJKZ=2, otherwise let factorJKZ=1
  • Increase factorJKZ by our factorRatio (i.e. add factorRatio to factorJKZ)
  • If we are divisible by 3, increase factorJKZ by 3 (i.e. add 3 to factorJKZ)
  • If we are divisible by 5, increase factorJKZ by 5 (i.e. add 5 to factorJKZ)
  • If we are a square, double factorJKZ
  • If we are a cube, triple factorJKZ
  • If we are a palindrome, decrease factorJKZ by 1
  • Take the result of factorJKZ at this point. If this result (factorJKZ) is divisible by our number num, then multiply factorJKZ by 0.
  • Return the final computed result for factorJKZ

Note that this method may return any integer (i.e., negative, zero, or positive)
Examples:

NumberfactorJKZ
10
22
33
49
55
60
77
817
913
109
  • Return a summary string of the SmartNumber
  • Show one property per line
  • The example here shows four properties
  • You may use this example method as-is except add at least four more properties so that eight or more properties are displayed
public String getSummary() {
	String summary = "";
	String sep = System.lineSeparator();
	summary += "Number: " + this.getNum() + sep;
	summary += "Prime: " + this.isPrime();
	summary += "Factor Count: " + this.factorCount() + sep;
	summary += "Remainder(/10): " + this.remainder(10) + sep;
	//TODO
	return summary;
	}

Grading


For this assignment, we will use these grading rules.