Exercises 2

Many of the exercises are borrowed from Rich Pattis from Carnegie Mellon University. Many thanks to Rich for letting me use these.

  1. Assume that we declare a char grade; and guarantee that it stores a letter corresponding to a UT grade: 'A', 'B', 'C', 'D', or 'F'. Write an if statement that computes the number of quality points for that grade and stores it in int qp; an A is worth 4, a B is worth 3, a C is worth 2, a D is worth 1, and an R is worth 0.

     

  2. Assume that we declare int hours; Write an if statement that computes the pay (in cents) due a worker according the following formulas: 625*Hours if the hours worked is less than or equal to 40; 625*Hours + 725*(Hours-40) if the hours worked is greater than 40. Store the result in int centsPay; Try a few examples under, at, and over 40 hours to verify your statement is correct.

     

  3. Assume that we declare int x, y; boolean isIt; Tracethe following Java statements: one where x stores 3 and y stores 5; and another where x stores 5 and y stores 3. State whether the results are the same or different in each case.
       if (x < y )            if (x < y)
         isIt = true;          isIt = true;
       else                  isIt = false;
         isIt = false;
    Which statement side is equivalent to the expression statement isIt = (x < y);

     

  4. Assume that we declare int studentAnswer, correctAnswer, wrongCount; Explain what is wrong with the following statement (there is a syntax error).
       if (studentAnswer == correctAnswer)
       else
         wrongCount++;
    Explain how to fix this problem in a simple way.

 

  1. Assume that we declare double s, signum; Write a cascaded if statement(s) that stores into signum the value -1. if X is less than 0.; 0. if X is equal to 0.; 1. if X is greater than 0.

     

  2. Assume that we declare double min, x, max; Write a cascaded if statement(s) that stores into x the value min if x is less than min; max if x is greater than max; nothing new otherwise.

     

  3. Assume that we declare int x,y,z,min; Write an if statement(s) that stores into min the minimum of the values stored in x, y, and z. Try to do this with the minimum amount of code.

     

  4. Suppose that we modify the clock code to call emitBeeps at the bottom of its block, and also change its argument to just hour. Will this code always work as before? If not, for what hour and minute combination(s) will it fail?
         if (minute != 59)
           minute++;
         else {
           minute = 0;
           if (hour != 23)
             hour++;
           else
             hour = 0;
           emitBeeps(hour);
         }
    Note that to be correct, the code must be correct for every hour and minute. There are 24x60 = 1,440 different possiblities; which ones are crucial to check?

     

  5. Suppose that we modify the clock code as follows. Will this code always work as before? If not, for what hour and minute will it fail?
         minute++;
         if (minute == 60 {)
           minute = 0;
           hour++;
           emitBeeps(hour);
           if (hour == 24)
             hour = 0;
         }
    Note that to be correct, the code must be correct for every hour and minute. There are 24x60 = 1,440 different possiblities; which ones are crucial to check?

     

  6. Assume that we declare int hour; storing the values 0 through 23 as described above. Write an if statement(s) to display on the console the hour in a standard format: e.g., when hour stores 3 display 3am; when hour stores 15 display 3pm. When hour stores 0 display 12midnight and when hour stores 12 display 12noon. Try to do this with the simplest possible code.