CS 117 Midterm 1

Due 3:00PM Friday, February 4

This is an exam. That means you may not speak with anybody other than me (Jeff Ondich) about its contents. You may, however, use your book, your notes, the books in the library, and the Internet. If you obtain information from some source other than your own brain, cite your source and use quotation marks where appropriate.

Hand your exam in on paper.

  1. (2 points) After the following code runs, what value is stored in k?

    int i = 15; int j = 2; int k = i / j;

  2. (5 points) The array returned by the getPixels1D method of the EasyBufferedImage class represents all the pixels in a rectangular image as a single list of pixels. For example, an image 200 pixels wide and 150 pixels high would be represented by an array 30,000 pixels long, with indices ranging from 0 to 29,999.

    Suppose you have one of these 1-dimensional arrays called red (obtained by code like this: int[] red = myImage.getPixels1D( EasyBufferedImage.RED )). Suppose the image in question is width pixels wide and height pixels high. If you have an index k referring to a pixel in the 1D array, show two formulas in terms of width, height, and k, that yield the row and column numbers of the pixel. For example, if the image is 200x150, and k is 645, the row formula should give 3, and the column formula should give 45.

  3. (9 points) Loops.

  4. (11 points) Boolean expressions.

  5. (4 points) When I put the following code into a main, compile it, and run it, it prints "not equal", which seems odd, given that both s and t refer to strings with value "greater kudu". Explain why this is happening and how to fix it.

    String s = "greater kudu"; String t = "greater"; t = t + " kudu"; if( s == t ) System.out.println( "equal" ); else System.out.println( "not equal" );

  6. (2 points) I'm just about to finish a book, and I'd like something interesting to read. Any suggestions?

  7. (2 points) Who were John Mauchly and Presper Eckert, and what were their roles in the history of computer science?

  8. (12 points) Designing a class.

    My grocery store has three self-service aisles that allow customers to scan, bag, and pay for their own groceries without the assistance of a checkout clerk. Each station has a touch screen with instructions on it, a barcode scanner, and a credit card reader for payment. One by one, you scan the items you want to buy, and the running total appears on the touch screen, along with instructions on how to continue scanning or finish your purchase. Once you've done it a couple times, it's a pretty easy system to use.

    Suppose you are writing Java software to control such a system. One of the abstractions you would probably want to represent in your software would be the notion of a single purchase--essentially a list of items being purchased, plus any other information that would distinguish my session with the machine from the session of the person behind me in line.

    For this exercise, I would like you to design a Purchase class for use with this kind of grocery machine. Your design should include:

    Probably the easiest way to report your class design would be to write a Java file called Purchase.java, and include in it everything (variable declarations, methods, and javadoc) except the actual contents of the methods. Note that I am not asking you to write the bodies of the methods--just their interfaces (that is, their top lines with the name, return type, and parameter list) and the corresponding javadoc descriptions.