I have this assignment for school that I’ve been staring at for a week and I just can’t understand what to do. I take notes on everything, I complete the corresponding codecademy assignments. Individually, I think I understand, but I can never put anything together to make a functioning program. I am so stressed and frazzled, please tell me it gets a little easier with time.
These are the problems on my assignment. No matter what I do with any of them, I get an error:

(Decimal to binary) Write a method that parses a decimal number into a binary number as a string.
The method header is:
public static String decimalToBinary(int value)
Write a test program that prompts the user to enter a decimal integer value and displays the corresponding binary value.
Here is a sample run:
Enter an integer: 1451
The binary value is 10110101011

(Capitalize first letter of each word) Write the following method that returns a new string in which the
first letter in each word is capitalized.
public static void title(String s)
Write a test program that prompts the user to enter a string and invokes this method, and displays
the return value from this method. Here is a sample run:
Enter a string: london england 2015
The new string is: London England 2015
Note that words may be separated by multiple blank spaces

(Hours, minutes, and seconds) Write a method that returns a string in the form of hour:minute:second
for a given total seconds using the following header:
public static String format(long seconds)
Here is a sample run:
Enter total seconds: 342324
The hours, minutes, and seconds for total seconds 342324 is 23:05:24
Note that a zero is padded to hour, minute, and second if any of these values is a single digit.

(Pascal triangle) Write a program that displays a Pascal triangle. The program prompts the user to
enter the number of rows and displays the triangle. Here is a sample run:
1
Enter the number of rows: 8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
For information on Pascal triangle, see http://www.mathsisfun.com/pascals-triangle.html. You
need to write a method that computes C(m, n) = m!
(m−n)!n!

For question 1, how well do you understand Bitwise operators and their uses? That’s one approach.

Consider this algo written in Python…

>>> from math import log2, floor
>>> x = 2019
>>> n = floor(log2(x))
>>> m = '0b'
>>> while n >= 0:
m += str(x >> n & 1)
n -= 1
>>> m
'0b11111100011'
>>> int(m, 2)
2019
>>>

To see how right-shifting can be applied to your number, let’s carry out the steps manually starting with zero and going until we reach a quotient of 1.

>>> x = 1451
>>> x >> 0
1451
>>> x >> 1
725
>>> x >> 2
362
>>> x >> 3
181
>>> x >> 4
90
>>> x >> 5
45
>>> x >> 6
22
>>> x >> 7
11
>>> x >> 8
5
>>> x >> 9
2
>>> x >> 10
1

Once we reach 1 we can stop. That is the lowest order bit in the binary. Now let’s do it again, except this time we will AND it with 1 to see if the bit is set or not. We know that there will be 11 shifts from the above divide by 2 action (a right shift of 1 is the equivalent of dividing by 2 and dropping the decimal fraction, if there is one; i.e., floor). Our first shift will be 10 since we will be shifting zero at the end, making 11 shifts in all.

>>> x >> 10 & 1
1
>>> x >> 9 & 1
0
>>> x >> 8 & 1
1
>>> x >> 7 & 1
1
>>> x >> 6 & 1
0
>>> x >> 5 & 1
1
>>> x >> 4 & 1
0
>>> x >> 3 & 1
1
>>> x >> 2 & 1
0
>>> x >> 1 & 1
1
>>> x >> 0 & 1
1
>>>

The top number is the highest order bit, and the bottom is the lowest order bit.

10110101011

Compare that with the expected result. If none of this made any sense then you have your work cut out for you. Don’t even think about writing code until you know what you want it to do, and why. Set out the tools you intend to use (or write) and test your logic on paper. These problems are not as simple as they may look, especially if your tool box is light. Expect to spend enough time on each one to gain some insight and add to your tool chest.

Another approach is to subtract the largest power of 2 that is less than or equal to the number, in succession so the number gets smaller and smaller. The powers of 2 that we get are the 1s in their respective order. The missing degrees are zeros.

The only degrees with no value are 2, 4, 6 and 9 so those are all zeros.

10 9 8 7 6 5 4 3 2 1 0
1 0 1 1 0 1 0 1 0 1 1

Notice that when we tried to compute (subtract) 2 ** 9 we got a negative number? That’s a deal breaker, so we won’t use that degree again.

>>> from math import log2, floor
>>> x = 1451
>>> n = floor(log2(x)) # 1024 => 2 ** 10
>>> m = '0b'
>>> while n >= 0:
u = x - 2 ** n
if u < 0:
m += '0'
else:
m += '1'
x -= 2 ** n
n -= 1
>>> m
'0b10110101011'
>>>