Homework 2 : Number Personalities
ESE112 Fall 2008

Purposes of this assignment:

Important:

Programming Style rules

Before you start working on the assingnment, make sure you have the the style rules. You will be also be graded on the following style rules besides having the correct outcome.

Details on the Assignment:

Part I - Personality Description

Positive integers can have the following personalities: triangular or not triangular, happy or unhappy, smug or not smug, and honest or dishonest. Implement the following peronalities described in file NumberPersonalities.java (download file, open in Dr Java and edit) .The method name are isTriangular, isHappy, isSmug, and isHonest. All the methods return boolean (i.e. methods that return a value of true or false).

Note: that the all methods currently return false.This done so that your program can compile while you work on one method at time. Also any variables you need to work with should be declared within methods, not outside any method.

1.Triangular Numbers

A traingular number is of the number of the form 1 + 2 + 3 .. + n, for some positive n. These numbers are called triangular because, if you have that many object, you can you can arrange them in an equilateral triangle (see figure).

Balls in a triangule


2. Happy Numbers

Repeatedly apply the following procedure to a number:

  1. Square each of the digits of the number.
  2. Add the squares together.

If by doing this you eventually get to 1, then the number is happy.

For example, if you start with 13:

If instead you get into an infinite loop, the number is unhappy. So if your program runs forever, the number is unhappy. This isn't a very useful test, however. Fortunately, every unhappy number will eventually get into this cycle:

4, 16, 37, 58, 89, 145, 42, 20, 4, ...

so if you get any one of these numbers, then you can stop and conclude that the number is unhappy.

 

3. Smug Numbers

A number is smug if it is the sum of two square numbers. The first few smug numbers are 2(1+1), 5 (1+4), 8 (4+4), 10 (1+9), 13 (4+9), etc.

Note: You are not allowed to use any in-built function that java provides. However, a square number Balls in a squareis a number of the form 1 + 3 + 5 + 7 + ... + n, for some odd positive n. (The figure should help you understand why this definition is the same as the usual definition of square numbers.) Hint: A method can call other methods to some of its work, so you can write your own methods to break down the work..

 

4. Honest Numbers

A number is dishonest if it "pretends" to be a square number, but isn't one. Specifically, n is dishonest if there is a number k such that n/k = k (using integer division), but k*k is not n. A number is honest if it is not dishonest.

Testing

To individually test a method you use Dr Java Interactions pane. Sample interactions are provided:

> NumberPersonalities.isTriangular(13)
false
> NumberPersonalities.isTriangular(17)
false
> NumberPersonalities.isHappy(13)
true
> NumberPersonalities.isHappy(17)
false
> NumberPersonalities.isSmug(13)
true
> NumberPersonalities.isSmug(17)
true
> NumberPersonalities.isSquare(13)
false
> NumberPersonalities.isSquare(17)
false
> NumberPersonalities.isHonest(13)
true
> NumberPersonalities.isHonest(17)
false
Also test your program with two or three large numbers (say, five or six digits), to make sure that your program can handle them.

Part II - Reuse

Now complete the static method revealPersonality that takes two arguments. The first argument is the number whose personality you want to reveal, and the second argument which also is a number but it will indicate the personality you need to test. The method should return a string which will indicate personality.Depending on the second input/parameter/argument you will return following strings:

1 - "triangular" or "not triangular "
2 - "happy" or unhappy"
3 - "smug" or "not smug"
4 - "honest" or "dishonest"

Dr Java Interaction as follows:

 > NumberPersonalities.revealPersonality(5,4) //reveal whether 5 is honest
 "dishonest"   
 > NumberPersonalities.revealPersonality(4,4) //reveal whether 4 is honest
 "honest"   
 > NumberPersonalities.revealPersonality(2,1)  
 "not triangular"   
 > NumberPersonalities.revealPersonality(4,1) //reveal whether 4 is triangular  
 "not triangular"   
 > NumberPersonalities.revealPersonality(4,5) //there are only 4 types of personalities, anything other than 1-4 is unknown    
 "unknown"  

Note: Make sure your output conforms to the interactions. Note that method is returning a String and not printing to screen. Essentially the returned value can be used as follows:

> System.out.println(NumberPersonalities.revealPersonality(4,5));
unknown //note the double quotes disappear.

Part III - StandAlone application

For each of the numbers 1 through 100, print out the numbers, one per line, along with a list of its characteristics (on the same line).

Your output should look approximately like this:

   1 triangular, happy, not smug, honest
2 not triangular, unhappy, smug, honest
3 triangular, unhappy, not smug, honest
4 not triangular, unhappy, not smug, honest
5 not triangular, unhappy, smug, dishonest ...
Hence you need to also write a main method that will declare a variable as int limit = 100; and test the numbers 1 through limit, inclusive. This makes it easy to change the limit later. To execute the main, simply compile your program and then hit the Run button in Dr Java.

Due:

Turn in your NumberPersonalities.java file use Digital Dropbox on Blackboard to submit your work. If your program does not Compile, then you will receive no credit. Make sure to follow the instructions on the Programming Info link on format your submission.