3.8 Review Exercises 3

Exercise 3.8

    1. Assume that the following declarations and assignments have been made:
         int age = 16;
         int height = 175;
         int weight = 70;
         char sex = 'M';
         boolean healthy = true;
    2. Using these values, evaluate each expression.
      1. age >= 16 && healthy
      2. !(weight <= 75) && height >= 180
      3. age > 10 || sex 'F' && height < 170
      4. !(height < 160) && (weight< 60)

    3. For each legal expression, state its value. For each illegal expression, state the reason that it is not legal.
      a) !(3 < 4.5)b) 'r' < 'q'
      c) !(17 / 5 = 3.4)d) 'G' != 'g' || 1 + 2 <= 3
      e) 5 < Math.abs(3-2*5) < 10f) '6' - '2' == '4'

    4. Given three strings s, t, and u, write a fragment that would print the string that would be first if they were to be printed in lexicographic order.
    5. In Question 3, the ordering of the strings is not necessarily the order in which they would be printed in a dictionary. Explain.
    6. Study this program and then answer the questions that follow it.
       class Chipchoice
         {
            public static void main (String[] args)
            {
               System.out.println("Preferred flavour?");
               char flavour = In.getChar();
               System.out.println("Preferred style?");
               char style = In.getChar();
               
               System.out.print("The client preferred ")
               if (flavour == 'B')
                  System.out.print("bar-b-que");
               else if (flavour == 'V')
                  System.out.print("vinegar");
               else
                  System.out.print("other");
               if (style == 'e')
                  System.out.print(", crinkled");
               else if (style == 'R')
                  System.out.print(", regular");
               System.out.println(" chips. ");
            }
         }      
      What would the program print given each set of data as input?
      a)
      V
      C
      b)
      B
      R
      c)
      V
      T
      d)
      B
      C
      e)
      C
      V
      f)
      v
      r

    7. How would you modify the program of the previous question so that it would accept both upper case and lower case letters in the responses from the user?
    8. Rewrite as a single if statement in as concise a form as possible. Assume that flag is of type boolean and n is of type int.
      if (true)
         if (! flag)
            n = 1;
         else
            n = 0; 
    9. Rewrite using nesting.
         if (p && q)
            System.out.println("Both true");
         if (p && !q)
            System.out.println("0nly first true");
         if (!p && q)
            System.out.println("Only second true");
         if (!p && !q)
            System.out.println("Neither true"); 
    10. Consider the following statement.
         if (a <= b && b <= c) || (a >= b && b >= c))
            System.out.println("Values in order");
         else
            System.out.println("Values out of order"); 
      1. Describe in a few words the effect of the statement.
      2. Write a statement that has the same effect but uses neither && nor ||.

    11. In the country of Rahmania, the cost of mailing a letter is 40 sinas for letters up to 30 g, 55 sinas for letters over 30 g and up to 50 g, 70 sinas for letters over 50 g and up to 100 g, and then an additional 25 sinas for each additional 50 g or part thereof. Write a program that prompts the user for a mass and then gives the cost of mailing a letter having that mass.
    12. One way of giving a direction is to simply use one of the letters N, E, S, or W indicating the nearest compass point. Another way is to give a bearing as a number from 0 to 359. A bearing of 0 corresponds to N, a bearing of 90 corresponds to E, and so on. Write a program that prompts the user for a bearing from 0 to 359 and prints the corresponding letter of the compass point nearest to that bearing. For example, input of 73 should produce output of E. Bearings half way between two compass points should be taken to be either N or S, as appropriate. Your program should make decisions efficiently.
    13. Suppose that two line segments on a number line are represented by the values of their endpoints: segment s1 is represented by left1 and right1 while segment s2 is represented by left2 and right2. For example, the values
      leftl = -1
      right1 = 4
      left2 = 6
      right2 = 9  
      would define the segments shown in the diagram.


      1. s1 is longer than s2.
      2. s1 is entirely contained within s2.
      3. s1 and s2 have no points in common
      4. s1 and s2 overlap partially (but not completely).
    14. Projects

    15. Write a program to compute an employee's weekly pay and produce a pay slip showing name, gross pay, deductions, and net pay. The program should first prompt the user for:
      1. family name
      2. given name
      3. hourly rate of pay
      4. number of hours worked that week (Any hours over 40 are paid at double the normal hourly rate.)
      5. a letter indicating the employee's tax category
        1. no tax deduction
        2. tax is 10% of gross pay
        3. tax is 20% of gross pay
        4. tax is 29% of gross pay
        5. tax is 35% of gross pay
      6. either a Y or an N to indicate whether or not the employee wants $20 deducted from the weekly pay as a contribution to the United Way Charity
    16. Write a program that makes change for amounts less than one dollar. Input to the program should be a positive integer less than 100, representing an amount of money, in cents. Output should be the original amount of money together with a set of coins (quarters, dimes, nickels, cents) that could make up that amount. The program should produce change containing the minimum number of coins required for the given amount. The output should be in a natural, non-stilted form. For example, input of 58 should produce output something like
      58 cents: 2 quarters, 1 nickel, and 3 cents.
      rather than
      58 cents: 2 quarters, 0 dimes, 1 nickels, 3 cents. 
    17. The government of Simpleton has devised what it thinks is an easy income tax system, but its citizens still need help. They have commissioned you to write a program to ask a citizen a few simple questions and compute the tax that is payable or the refund that is due. You must write your program as clearly as possible so that the government can verify it easily. Your program should first ask a citizen for his/her income (income), housing cost (houseCost), number of children (totalChildren), and number of children that are in school (schoolChildren). It should then compute and print the tax payable or the refund due. The tax rules are as follows. The Simpleton tax rate is 18% but citizens are not taxed on the first $10 000 of income unless they pay more than $8 000 for housing. For every child, a Simpleton citizen gets a $500 tax reduction, or $1 000 if the child is in school. This reduction never results in citizens getting refunds unless their housing costs are less than $6 000 and they have more than two children, at least one of whom is in school. Finally, if the tax payable is more than $2 000, then it is increased by an additional 15% surtax.