Elections are coming, so let us help maintain the elections.
1. The names of the three candidates are stored in the variable name1, name2, and name3; the number of votes that each of them received is stored, correspondingly, in the variables votes1, votes2, and votes3. Use if-statements to write down a code that, given this information, prints the name of the winning candidate and the number of votes received by this candidate.
Hint: You do not need to read anything from the keyboard or from a file, just compute and print. Assume that the variables name1, name2, name3, votes1, votes2, and votes3 have been assigned some values already.
2. Suppose that the name of the currently winning candidate is stored in the variable name1, and the name of the currently second candidate (who has so far got fewer votes) is stored in the variable name2. After the Texans voted, the total numbers of votes changed. These new numbers of votes are stored in the variable votes1 for the previously winning candidates, and in the variable votes2 for the other candidate. If the number of votes for the previously second candidate is now larger than the number of votes for the previously winning candidate, we need to swap their names, i.e., place the name of the previously second candidate into name1, place the name of the previously first candidate into name2, and similarly swap the variables votes1 and votes2. Write down a code that performs this conditional swap.
Example: if name1 is "Nixon", name2 is "Kennedy", votes1 = 200, and votes2 = 300, then after your code, name1 should contain "Kennedy", name2 should contain "Nixon", votes1 should contain 300, and votes2 should contain 200.
Trace your code, step by step, on this example: draw all the boxes and show how their values change after each operation.
3. A person is eligible to vote if he or she is a US citizen, at least 18 years old, and not a convicted felon. Assume that we have a boolean variable usCitizen, an integer variable age, and a boolean variable felon. Write down a Java expression for being eligible to vote. Draw the truth tables for "and" and "not" operations. Use these truth tables to find the truth value of your expression for the University President Diane Natalicio who is a US citizen, older than 18, and is not a convicted felon. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.
4. Many voters do not understand the important of education. As a result, the state funding for universities goes down, and because of that, tuition has to increase. To avoid this problem, the students proposed a new law according to which only those who know math can vote. To show the knowledge of math, voters must print the squares of all the numbers from 5 to 8. Write down a program that use the for-loop for this computation. Trace your program step by step.
Comment: no need to be too wordy, just draw the boxes corresponding to all the variables, and show how the values of these variables change and what values will be printed.
5. The following algorithm can be used to convert a decimal positive integer n into the binary form: Divide n by 2, keep the remainder. Divide the result by 2, keep the remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired binary number.
Example of conversion: for n = 13, we have
13 / 2 = 6 rem 1 6 / 2 = 3 rem 0 3 / 2 = 1 rem 1 1 / 2 = 0 rem 1When we read the remainders from bottom to top, we get 1101, which is exactly the binary representation of 13 -- since 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 1 = 13.
Use this algorithm to convert number 82 into the binary form. Check the result by converting the binary number back into the decimal code.
Reminder: 137 in the decimal form means 1 * 10^2 + 3 * 10^1 + 7 * 10^0. Similarly, 101 in the binary form means 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5.
For extra credit: write down a code that converts a decimal number into the binary form (and returns this binary form as a string).