CS 1401, Exam #2, MW 9-10:20 version

Date: Wednesday, October 9, 2013
Name (please type legibly, ideally in block letters): ______________________________________________________________________

On October 9, 1874, the General Postal Union was formed which allowed people to send letters from one country to another.

1. A student decided to order a textbook online. To select the cheapest way of shipping, he compares three different shipping services. Write a piece of code that decides which of them is the cheapest. The names of three of these shipping services are stored in the variables ship1, ship2, and ship3, and their costs of shipping are stored in the variables cost1, cost2, and cost3. Use if-then statements to write down a piece of Java code that prints the name of the cheapest shipping service.

Comment: There is no need to read anything, assume that all six variables have already been assigned values.







































2. A student will order a textbook only if the shipping cost is $3.99 or smaller and the expected delivery time is 7 days or less. Write down a Java statement that uses the known values cost and deliveryTime to assign, to a boolean variable order, true or false depending on whether the student will order. Draw the truth table for "and", "or", and "not". Use these truth tables to find the truth value of your expression when a company offers to deliver in 2 days at the cost of $7.50.






















































3-4. Sometimes, when many students access the same website, it runs very slow. To inform a student about the progress, let us print statements informing a student how many orders are in line before his order is processed. Write a main method that asks for the number of people waiting in line and print the corresponding statements; once the wait is over, it should print a welcome message listed below. For example, if 5 people were waiting, your program should print the following messages:
Sorry for the delay. You are No. 5 in line.
Sorry for the delay. You are No. 4 in line.
Sorry for the delay. You are No. 3 in line.
Sorry for the delay. You are No. 2 in line.
Sorry for the delay. You are No. 1 in line.
Welcome to the O'Key Textbook Services, how can we help you?
Hint: be careful with using single quotes inside the print statement.












































5. Trace, step-by-step, what will be the result of the following Java code:
int b = 3;
double a = 2.0;
double power = 1.0;
if (b >= 0)
  {int i = 1;
   while(i <= b)
     {power *= a;
      i++;}
  }
else
  {int j = 1;
   while(j <= -b)
     {power /= a;
      j++;}
  }
System.out.println(power);
Draw the boxes corresponding to all the variables, and show all the changes of their values.







































6. Once you define a new raster img by using a command
JRaster img = new JRaster();
you can make a point with coordinates (x,y) blue by using a command
img.set(x, y, JRaster.blue);
Use a for-loop to draw a blue diagonal line (corresponding to y = x).