Homework 2 : Number Personalities
ESE112 Fall 2008Purposes of this assignment:
- To give practice programming with Java
- To practice java fundamentals - types, variables, operators, expressions, conditional statements, loops, and static methods (craft of programming)
- To think algorithmically (art of programming)
Important:
- This assignment is to be done alone. No collaboration of what so ever is allowed!!
- Any sort of cheating will result in zero grade for assignment and reported to Student of Office Conduct
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 oftrueorfalse).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).
2. Happy Numbers
Repeatedly apply the following procedure to a number:
- Square each of the digits of the number.
- 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:
12 + 32 = 1 + 9 = 1012 + 02 = 1and the number is happy.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
is 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
ksuch that(using integer division), butn/k = k k*kis notn. 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)Also test your program with two or three large numbers (say, five or six digits), to make sure that your program can handle them.
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
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, honestHence you need to also write a main method that will declare a variable as
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 ...and test the numbersint limit = 100; 1throughlimit, 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.javafile 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.