CS636 Midterm Exam Solution, for practice, S21    NAME:_________________________                              

1.       (12) Suppose you are automating operations for a store selling PCs.   All PCs of a certain model number are equivalent as far as buyers are concerned, but the store wants to sell them in FIFO (first-in, first-out) order so that no box gets too worn-looking.  Each PC has a model number (int), serial number (10-char string), vendor id (int), and delivery date (int day number.) 

  1. Using our client-server database applications technology, how should we hold the data on all the PCs to support this application?  Write the SQL that the program needs to use to create an empty version of the data holder. 

All long-term changeable knowledge should be in the DB (except per-user data). This means we need a table to hold the PC information:

create table pcs(model int, serial_no int, vendor int, delivery_date int);

(normally we would have a PK set up, probably serial_no, but it's not required by the problem statement)

  1. Given a model number, say 1234, for the next PC to sell, write the SQL query that determines the serial number of the PC that should be selected for the customer.

  select serial_no from pcs
   where model=1234 and delivery_date = (select min(delivery_date) from pcs where model=1234)

This may result in several serial_nos. To make it unique we could select for min(serial_no) instead of just serial_no.

c.    Design a domain class and DAO API (method signatures only) for this app that supports insert as well as the retrieval as described above.

domain class PC: just a POJO with the attributes as fields:  you don’t have to write out the getters and setters, just indicate them

public class PC {
   private String serialNo;
   private int model;

   private int vendor;
   private int deliveryDate;
  

  // getters and setters for the 4 fields
}

DAO API:

void initializeDB() throws SQLException;

void insertPC(PC pc) throws SQLException;

PC findOldestPC(int model) throws SQLException;

(also close() is a good idea, to close the Connection)


2.       (7) In the same scenario as problem 1, once a PC is sold, we need to keep track of the customer for each individual PC.  Each PC has a unique customer but a single customer can purchase multiple PCs. Each customer has a string name and a customer id, custid (a unique integer).

  1. How should we hold the data on customers?  As in 1a, show the creation of the data holder using SQL.

     create table customer( name char(20), custid int primary key);

    b. How do we specify the customer for each PC?

        We need to add a custid column to table pcs.

  1. What foreign key constraint can be added to this system to improve its integrity?

    The FK needs to go from PCs to customer, because a PC has a unique customer, the target of the FK:

Answer: Add FK from custid of pcs to custid of customer.

3.  (12) Web Background

  1. Write two simple web pages (i.e., provide the HTML code for) a.html and b.html that reside in the same directory with local path /testmsg on a certain web server.  Page a links to page b, and page b has a form with a single text input with name “msg”, no ACTION specified, but with a submit button that POSTs the form to the server..

 -------------

a.html

<html>
  <head>
    <title>Page A</title>
  </head>
  <body>
     <a href="b.html">Link to B</A><br>
  </body>
</html>
 

b.html

<html>
  <head>
    <title>Page B</title>
  </head>
  <body>
    <form method='POST'>
      <input type="text" name="msg">
      <input type="submit">
    </form>
  </body>
</html>

  1. Suppose a user browses to page a.html as served by the web server, follows the link to b.html, and then fills in “hello” in the text box and clicks the submit button.  Give the sequence of HTTP request and responses (first line of request and any body it has, content of response) to this web server that would happen, stopping after the request that carries the “hello” data back to the server (using b.html’s local path, the default ACTION URI.) Here a.html and b.html have no images or CSS files.

Since headers were not asked for, you can omit them--
GET /testmsg/a.html HTTP/1.1
response body: text of a.html
GET /testmsg/b.html HTTP/1.1
response body: text of b.html
POST /testmsg/b.html HTTP/1.1
request body has msg=hello

response body: not clear: may depend on web server or be b.html


Note: b.html does not know how to process the msg=hello information, so all that happens is that the text of b.html gets returned again here. We need a servlet or JSP (which compiles to a servlet) to actually process form data.

4.  (7) Maven and command line tools.

  1. Consider two copies of the same Maven project, in top-level directories dir1 and dir2. After "mvn package" has been executed in the first copy, "mvn package" executes for the second copy without any downloads of jar files. Explain why.

    Answer: Maven projects share one repository (in ~/.m2) for downloaded jars, so once one project downloads a jar, other projects can use it without downloading.

  2. Suppose a Maven project has one JUnit test that fails, but the main sources are fine (the problem is only in the unit test). What will happen when "mvn package" is executed?  Will this command succeed? Explain why or why not.

    Answer: mvn package runs the JUnit tests before building the main jar. In this case the failing JUnit test will fail the whole command, so no jar will be built.

  3. Consider JdbcCheckup.java. Suppose your current directory is your own jdbc directory on pe07 containing JdbcCheckup.java and the driver jar files, previously copied from /data/htdocs/cs636/jdbc. Give the command to compile JdbcCheckup.java, and then the command to run it to connect to Oracle on dbs3. You don't need to show how to enter information into the running program.

     javac JdbcCheckup.java
   java -cp ojdbc6.jar:. JdbcCheckup

    (12) System implementation.   In pizza1 consider the service method listed here:  

    // return all orders for this room, for today, in order by id
    public List<PizzaOrderData> getOrderStatus(int roomNumber) throws ServiceException {
        List<PizzaOrder> pizzaOrders = null;
        List<PizzaOrderData> pizzaOrders1 = new ArrayList<PizzaOrderData>();
        try {
            pizzaOrders = pizzaOrderDAO.findOrdersByRoom(roomNumber, adminDAO.findCurrentDay());    
            for (PizzaOrder order: pizzaOrders) {
                pizzaOrders1.add(new PizzaOrderData(order));
            }    
        } catch (SQLException e) {
            throw new ServiceException("Error in getting status" + e, e);
        }
        return pizzaOrders1;
    }

  1. What calls into the DAO layer occur during the execution of this method?  List their method names in execution order.
  2. findCurrentDay  (called first as arguments are evaluated)

    findOrdersByRoom

  3. Explain why this method does not qualify as a POJO “getter”, even though its name starts with “get”.
  4. It has a method parameter in its signature. A proper POJO getter is a no-args method like int getId().

  5. How do we know that there is no UI (user interface action) during the execution of this call?
  6. This is a layered system, with no upcalls, that is, calls from a lower layer to an upper layer.  This service-layer code only calls down to the DAO and also to the domain object methods (plus library methods).  The domain objects are all self-contained, or at worst, calling other related domain objects, and thus never calling any app methods outside of their package. The DAO never calls up the layers, so in particular not to the top layer, the presentation layer, which has all the UI code. Thus there is no way for execution to reach the presentation code while executing this method.

  7. In this code, what code takes a PizzaOrder object and returns the corresponding PizzaOrderData object?   
  8.      new PizzaOrderData(order);

  9. What kind of code (what layer) calls this getOrderStatus method?   
  10.             Presentation

  11. This code has a "new" operation (new PizzaOrderData(...)). We are now sensitized to seeing "new" in method code, worrying about DI concerns. Is this a case of a new that causes an object dependency (a case where object A has a field that refs object B)? Explain.

Answer. No, here we have a new PizzaOrderData object and the only other object involved is order, a PizzaOrder object. But the PizzaOrderData object does not end up with a ref to the PizzaOrder object. Instead, the various fields of the PizzaOrder are just copied to the new PizzaOrderData object, and no other object is created in the PizzaOrderData constructor. So this is not an object dependency problem.