Class Work (CS 312) 25 Feb 2012

Computer Programs

Write the following programs from Chapters 2 in your book and get them to work.

These programs have to be completed by the end of Monday, 25 Jun 2012. Do not turn them in. They will not be graded. Use a text editor on any public CS Linux machines to create the programs. Compile and run them on the command line. Indent by two spaces and vertically align your curly braces.

Logic Problems

  1. Three men who had a monkey bought a pile of mangoes. At night one of the men came to the pile of mangoes while the others slept and, finding that there was just one more mango than could be exactly divided by three, tossed the extra mango to the monkey and took away one third of the remainder. Then he went back to sleep.

    Presently another of them awoke and went to the pile of mangoes. He also found just one too many to be divided by three so he tossed the extra one to the monkey, took one third of the remainder, and returned to sleep.

    After a while the third rose also, and he too gave one mango to the monkey and took away the whole number of mangoes which represented precisely one third of the rest.

    Next morning the men got up and went to the pile. Again they found just one too many, so they gave one to the monkey and divided the rest evenly. What is the least number of mangoes with which this can be done?

  2. You are an archaeologist that has just unearthed a long-sought triplet of ancient treasure chests. One chest is plated with silver, one with gold, and one with bronze. According to legend, one of the three chests is filled with great treasure, whereas the other two chests both house man-eating pythons that can rip your head off. Faced with a dilemma, you then notice that there are inscriptions on the chests:

    Silver Chest: Treasure is in this Chest.

    Gold Chest: Treasure is not in this Chest.

    Bronze Chest: Treasure is not in the Gold Chest.

    You know that at least one of the inscriptions is true, and at least one of the inscriptions is false. Which chest do you open?

  3. Alicia, Betty, and Carol went on a vacation. One went to Amsterdam, one went to Bombay, and one went to Cairo. Only one of the following four statements is true.

    1. Carol went to Amsterdam.
    2. Carol did not go to Bombay.
    3. Alicia did not go to Bombay.
    4. Alicia did not go to Cairo.
    Where did each go on a vacation?

Character Representation

Translate the following ASCII codes into strings of characters by interpreting each group of eight bits as an ASCII character. Each digit is a hexadecimal digit.

  1. 48656c6c6f21
  2. 436f6d70757465727321

Translate the string Java is Fun! into its ASCII representation and give your answer in hexadecimal digits. Remember even spaces have an ASCII representation.

Numerical Computation and Java Operators

  1. Assume that int a = 1 and double d = 1.0 and that each expression is independent. What are the resuls of the following expressions?

  2. Prove the following Boolean identities:
  3. What is the output of the following code:
    int m = 1;
    int n = ~m;
    System.out.println (n);
    
  4. If p, q, and r are Boolean variables prove that the Boolean expression (p && q) || r is not the same as p && (q || r) using a truth table.
  5. What is output by the code below?
    int w = 4;
    int z = 2;
    z = 2 + 2 * ++w;
    System.out.println (z);
    
  6. What is output by the code below?
    int ax = 30;
    int bx = ax << 3;
    System.out.println (ax + "  " + bx);
    
  7. What is output by the code below?
    int x = 10;
    int y = 20;
    System.out.println (x % y > y % x);
    System.out.println (x / y > y / x);
    
  8. What is the output of the code below?
    int x = 3;
    int y = 7 / 2;
    int z = (x > y) ? (x < y) ? 1 : 2 : 3;
    System.out.println (y + " " + z);
    
  9. What is the output of the code below?
    boolean b = true;
    boolean oldB = b;
    b = (b == false);
    System.out.println (b == oldB);
    
    Will the output be different if b was initialzed to false?