On October 12, 1492, Chistopher Columbus landed in the Caribbean -- mistakenly believing that he is in Asia.

1. To better estimate his latitude, Columbus crew made three
measurements of the same latitude. Since the results were slightly
different, they selected the middle value.
Use *if-statements* to write down a code
that, given three numbers lat1, lat2, and lat3 describing the three
measurement results, computes and **prints** the middle value.

*Hint:* You do not need to read anything from the keyboard or
from a file, just compute and print. Assume that lat1, lat2, and lat3 have
been assigned some values already.

Solution: int middle; if ((lat1 <= lat2 && lat2 <= lat3) || (lat3 <= lat2 && lat2 <= lat1)) middle = lat2; else if ((lat1 <= lat3 && lat3 <= lat2) || (lat2 <= lat3 && lat3 <= lat1)) middle = lat3; else middle = lat1; System.out.println(middle);2. Columbus was original trying to get financial support from Italy, but ended up supported by Spain. Just in case, he has originally prepared several proclamations proclaiming the new lands to be the property of Italy and Spain. Write down a code that, given two strings s1 and s2 and the name of the country (stored in the string

*Example:* if s1 is "I proclaim these lands the property of Italy",
s2 is "I proclaim these lands the property of Spain", and
*country* is "Spain", then after your code, s1 should contain
"I proclaim these lands the property of Spain" and s2 should contain
"I proclaim these lands the property of Italy".

**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 the name is
spelled in all caps.

Solution: if (s2.indexOf(country) >= 0) {String temp = s1; s1 = s2; s2 = temp; } Tracing: originally -------- ------- ------- ------- | 22 | | 33 | | 44 | | Spain | -------- ------- ------- ------- s1 s2 country 44 ---------------------------------------------- | I proclaim these lands the property of Italy | ---------------------------------------------- 22 ---------------------------------------------- | I proclaim these lands the property of Spain | ---------------------------------------------- 33 after the line String temp = s1: -------- ------- ------- ------- ------ | 22 | | 33 | | 44 | | Spain | | 22 | -------- ------- ------- ------- ------ s1 s2 country 44 temp ---------------------------------------------- | I proclaim these lands the property of Italy | ---------------------------------------------- 22 ---------------------------------------------- | I proclaim these lands the property of Spain | ---------------------------------------------- 33 after the line s1 = s2: -------- ------- ------- ------- ------ | 33 | | 33 | | 44 | | Spain | | 22 | -------- ------- ------- ------- ------ s1 s2 country 44 temp ---------------------------------------------- | I proclaim these lands the property of Italy | ---------------------------------------------- 22 ---------------------------------------------- | I proclaim these lands the property of Spain | ---------------------------------------------- 33 after the line s2 = temp: -------- ------- ------- ------- ------ | 33 | | 22 | | 44 | | Spain | | 22 | -------- ------- ------- ------- ------ s1 s2 country 44 temp ---------------------------------------------- | I proclaim these lands the property of Italy | ---------------------------------------------- 22 ---------------------------------------------- | I proclaim these lands the property of Spain | ---------------------------------------------- 33 Extra credit: if (s2.toLowerCase().indexOf(country.toLowerCase()) >= 0) {String temp = s1; s1 = s2; s2 = temp; }3. In Spanish empire, two conditions must be satisfied for person to have the right to wear his hat in the presence of the King. First, this person must be either a Spanish nobleman

Solution: (spanishNobleman || indianNobleman) && !heretic ------------------------------------ | A | B | A and B | A or B | not A | ------------------------------------ | F | F | F | F | T | ------------------------------------ | F | T | F | T | T | ------------------------------------ | T | F | F | T | F | ------------------------------------ | T | T | T | T | F | ------------------------------------ spanishNobleman is False, indianNobleman is False, heretic is False, so the above expression becomes (F || F) && !F. According to the truth table, F || F is F and !F is T. Thus, the formula is F && T, i.e., False. Precedence: negation ! has the highest precedence, then &&, then ||. Inside the computer, True is 1, False is 0, so && is actually multiplication. Or is almost addition, and precedence for && and || is the same as for multiplication and addition.4. Suppose that Columbus diaries are stored in the file

*Reminder*: to create an object for reading from a file, you
need to use the following code:

Scanner fromFile = new Scanner(new FileReader("columbus.txt")); Solution: Scanner fromFile = new Scanner(new FileReader("columbus.txt")); Boolean found = false; String line; while(!found && fromFile.hasNext()){ line = fromFile.next(): if (line.indexOf("1492") >=0){ System.out.println(line); found = true; } } At first, "found" is false. Since !found is true and there is something in the file (i.e., fromFile.hasNext() is true), we go into the file and read the first line of the file into the variable "line". The value of "line" is now the first line of the file, i.e., "1491: looking for funding". This string does not contain a substring "1492", so line.indexOf("1492") is -1. The condition line.indexOf("1492") >=0 is not satisfied, so we do not do anything on this iteration and continue looping. The value of found is still false, there is still something in the file, so we read the next line "1492: found America" into the variable "line". This time, the string contains a substring "1492", and the value of line.indexOf("1492") is 0. The condition line.indexOf("1492") >=0 is satisfied, so we print the string "1492: found America" and change the value of the variable "found" to "true". On the next iteration, !found is false, so the condition for continuing is false, so we get out of the loop.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

*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.

Answer: 92 / 2 = 46 rem 0 46 / 2 = 23 rem 0 23 / 2 = 11 rem 1 11 / 2 = 5 rem 1 5 / 2 = 2 rem 1 2 / 2 = 1 rem 0 1 / 2 = 0 rem 1 When we read the remainders from bottom to top, we get 1011100. In binary code, it means 1 * 2^6 + 0 * 2^5 + 1 * 2^4 + 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0 = 1 * 64 + 0 * 32 + 1 * 16 + 1 * 8 + 1 * 4 + 0 * 2 + 0 * 1 = 64 + 16 + 8 + 4 = 92.