Guide for Collatz and SecondSmallest

Collatz

Instructions

  1. Start out with a random number n. Okay, take in an int from the command line.
  2. If n is even, do something. If n is odd, do something else. If n is equal to 1, stop.
  3. Write a program which takes any positive integer and prints out the Collatz sequence.

Theory

For the first part, we need to take an int in from the command line by using Scanner (as we’ve been doing since module 1). I’d argue it’s good to name this “currentNumber”.

Over-simplified example: If we want to find the Collatz sequence of 45, we enter 45 in the console box in eclipse and press enter. Now currentNumber is equal to 45.

The nice thing is, we can use currentNumber over again. We know it’s odd right now — but if we follow the rules of the Collatz sequence we can substitute it into the formula 3n + 1… while currentNumber is greater than 1… Get my hint? While (currentNumber > 1), determine if currentNumber is odd or even and change the value of currentNumber according to the formula and don’t forget to print it out.

Skeleton

SecondSmallest

Instructions

  1. Take an unknown number of positive integers as input.
  2. First number is always smaller than the second.
  3. There are at least three integers.
  4. All integers are unique values.
  5. Print the second smallest integer.

Theory

The first number is always smaller than the second… Think about that. You can say that for the first two ints, the first number is the <?> and the second number is the second<?>.

If only two numbers were entered, the problem would be solved. Sadly we have at least three numbers. We need to think about the positions that the third can fit in. In my mind, we have three cases. The third number could be smaller than the smallest number. It could be bigger than the smallest but smaller than the second smallest. Or. It could be the larger than the second smallest.

What do we need to do in all of those cases? If input number is larger, we don’t need to do anything… What do we need to do for the other two cases?

Now you don’t need to worry about the fourth or fifth number, because the algorithm we’ve just hinted at should be able to take all input numbers and put them in the correct position.

Skeleton

Scanner Caveat

You’ll notice that line 20 might be a little confusing, this is because we’ve had trouble with people trying to “escape” the console with ctrl + d. This line bypasses the need to do that. You merely enter your input as so: “5 6 3 25 2” etc…

The code “new Scanner(System.in).nextLine()” in line 20 would return the string “5 6 3 25 2” if that’s what you entered as input. You can now use sequenceScanner to scan the String “5 6 3 25 2” for the next int.

Basically, the sequenceScanner is scanning a String instead of System.in (which is what new Scanner(System.in).nextLine() is doing). Don’t worry if you don’t understand but this is a basic explanation of what’s happening.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.