1. Use if-statements to write down a code that, given three numbers n1, n2, and n3, finds the largest of these three numbers, and assigns this value to the variable whose name is largest.
Hint: You do not need to read anything from the keyboard or from a file, you do not need to print anything to the screen or place it into a file, just compute and assign. Assume that n1, n2, and n3 have been assigned some values already, which are the numbers that you are comparing.
2. Write down a code that, given two strings s1 and s2, sorts them in alphabetic order: i.e., if s1 follows s2 in alphabetic order, your code should swap the strings. You can assume that both s1 and s2 only contain small letters.
Example: if s1 is "table" and s2 is "chair", then after your code, s1 should contain "chair" and s2 should contain "table". Trace your code, step by step, on this example: draw all the boxes and show how their values change after each operation.
For extra credit: make the code work also if some of the letters are capital letters.
3. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of the statement 3 < 5 && 2 < 4 || 4 < 2. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.
For extra credit: what is the difference between && and &? What is the advantage of each of these operations? Give an example when they lead to different results.
4. Use the while loop to write a piece of code that, given an integer n, finds the smallest natural number k for which 2^k > n. In other words, your code should find the smallest power of two which is larger than a given integer n. For example, for n = 7, we should return 3, because 2^3 = 8 > 7, and 3 is the smallest such power: 2^2 = 4 < 7. Trace your code for n = 7. Idea: start with k = 0, and while the condition 2^k > n is not satisfied, keep increasing k by 1. Trace your code step-by-step for n = 7.
Reminder: to compute the power a^b in Java, you can use the operation Math.pow(a,b).
For extra credit: when you want to increase k by 1, what is the advantage of using k++ instead of k = k+1?
5. To convert a decimal integer n to binary form, you repeatedly divide n by 2 and keep the remainder until you get 0. Reading the remainders from bottom to top provides the desired binary number. Use this algorithm to convert 13 to binary code. Check the result by converting the binary number back into the decimal code.
Reminder: 137 in decimal code means 1 * 10^2 + 3 * 10^1 + 7 * 10^0. Similarly, 101 in binary code means 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5.