# Ulam numbers (calculates your life expectancy if you want to)

I'm new to Java and wanted to present to you my latest project: Ulam Numbers

/**

* This class consists of some instance variables a constructor and some getter and setter methods
* It is supposed to implement the Ulam numbers which are named after Stanislav Ulam
* Furthermore we will make use of the numbers to calculate/foresee how old you or me will become
* If this made you curious just enter your own birthday or that of a relative but be warned it might
* shock you to death.. Hehe..
*
* @author  Jae
* @version 1.0
*/
public class UlamNumber {

private int number;

/**
* This is a constructor which is pretty self-explaining
*
* @param number
* describes the number the user can input like for example his/her birthday
*/
public UlamNumber(int number) {
this.number = number;
}

/**
* This is a method without parameters which implements the ulam algorithm
* Furthermore it calculates your "age" by counting how many steps are needed till
* you reach the number 1 and gives it out on the console
*/
public void ulamAlgorithm() {

int counter = 0;

while (number != 1) {
if (number % 2 == 0) {
number = number / 2;
counter++;
} else if (number % 2 != 0) {
number = (number * 3) + 1;
counter++;
}
}

if (counter == 1) {
System.out.println("Unfortunately you will only become: " + counter + "  years old >:-)");
} else if (counter < 100) {
System.out.println("Unfortunately you will only become: " + counter + " years old =(");
} else {
System.out.println("Congratulation you will become " + counter + " years old =)");
}
}

/**
* This is a getter method which is pretty self-explaining
*
* @return number
* is from the type integer which returns the current value of number
*/
public int getNumber() {
return number;
}

/**
* This is a setter method which is pretty self-explaining
*
* @param number
* is from the type integer which an user can input to change the current value of number
*/
public void setNumber(int number) {
this.number = number;
}
}


I'm sorry but I didn't write an explanation to the above stated things. You can also see this in my bad javadocs. If you have any hints/tips for improvements, please let me know.

• Your title suggests that you intend to calculate Ulam numbers, but your code actually counts steps in the Ulam conjecture, a.k.a Collatz conjecture. – 200_success Mar 23 '15 at 15:41
• I'm sorry about my unprecise topic name but I couldn't think of one to describe my java programm (actually it's a "fun" programm where you can enter your birthday (at your own danger) and you get in return your life expectancy.. and I just used the ulam numbers as "algorithm" ) So my real question is.. how is my programming style ( I use checkstyle) ? Is it good, bad, normal, how can I improve it.. etc.. ? Thanks bro – jhyoneville Mar 23 '15 at 15:47

This is a senseless use of objects. What exactly is an UlamNumber object? Consider how you might use this class:

 UlamNumber ulam = new UlamNumber(0);
ulam.setNumber(20);
ulam.getNumber();        // ← returns 1, not 20


That code tells you nothing about the task that it performs (if you ignore the comments). In particular, ulamAlgorithm() does several things (count steps, compose a remark, and print it) — none of which is obvious from the name of the function.

What you want to write is

System.out.println(AgeCommenter.commentAbout(UlamSequence.countSteps(20)));


To make that happen:

public class UlamSequence {
public static int countSteps(long number) {
int steps;
for (steps = 0; number > 1; steps++) {
number = (number % 2 == 0) ? number / 2
: 3 * number + 1;
}
return steps;
}
}


Note the use of the for-loop for counting.

And the AgeCommenter:

public class AgeCommenter {
public static String commentAbout(int age) {
String fmt =
(age ==  1) ? "Unfortunately you will only become: %d year old >:=)"
: (age < 100) ? "Unfortunately you will only become: %d years old =("
:               "Congratulations, you will become %d years old =)";
return String.format(fmt, age);
}
}

• I could write an essay to thank you now.. but I will just keep it short.. thank you.. I will try to take what you just answered me into further projects of mine.. I don't have any real questions left so maybe you will review my further projects if you want to.. (though that might have to wait a little.. since I'm still kinda just doing java as little hobby and nothing serious yet) – jhyoneville Mar 23 '15 at 21:21

I can't figure out why you need a separate class for this algorithm so I recommend making the class and the function static:

public static class UlamNumber {
public static void ulamAlgorithm(int number) {


Then you can also simplify your if -statement by extracting the 2 counter++s and removing the if (number % 2 != 0) { from the else, because the else already covers that:

if (number % 2 == 0) {
number = number / 2;
} else {
number = (number * 3) + 1;
}
}
counter++;


If you want to do more with one Ulam-Number, you can keep it non-static, otherwise it is highly recommended to keep the functions static.

• Thanks alot for your input.. Could you give me a hint when to use several classes ? Like there were many methods to implement what I wanted to do but I didn't know which one I should use..(in the end I obviously chose this one).. Furthermore I'm still kinda new to java and just wanted to practice a little.. any tips..(sites with beginner friendly stuff.. and yes I googled it before I'm asking you this..) I visited some sites and asked some friends who are more experienced than me in the hope of improving myself.. but so far I still didn't make alot of progress.. Thank you – jhyoneville Mar 23 '15 at 17:35
• Use multiple (non-static) classes when you need to create several, different instances of something and need to apply different modifications to them. Make sure you also save such an instance and use it later on. If you see that you can use a method without instantiating (like yours) keep it static, so you don't need to create multiple instances of it (this is more recognizable when working with complex programs that need to track many different instances of an object). I'm not the best programmer out here, but this is basically what I have learned reading through some questions here. – GiantTree Mar 23 '15 at 17:56