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.
(2 points) After the following code runs, what value is stored in k
?
int i = 15; int j = 2; int k = i / j;
(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.
(9 points) Loops.
Write a loop that will print all the odd integers between 10 and 30.
Replace the "..."s in this code:
int j, k; for( ...; ...; ... ) { for( ...; ...; ... ) { System.out.println( j + ", " + k ); } }
to make the output produced by the code be:
3, 0 3, 1 3, 2 2, 0 2, 1 1, 0
The following code assumes a non-empty array a
of integers,
and computes the value n
using the code shown below.
int n = a[0]; for( k=0; k < a.length; k++ ) { if( a[k] > n ) n = a[k]; } System.out.println( n );
In a brief English sentence, explain the meaning of the value this code printe. (If you find yourself essentially repeating the code in English instead of Java, you haven't gotten it. Try tracing the code by hand using a sample array containing data of your own choosing. Stepping through an example this way should give you a better idea of the purpose of this code.)
(11 points) Boolean expressions.
Write a Boolean expression that is true if the integer k
is between 3 and 7 (inclusive), and false otherwise.
Write a Boolean expression that is true if the intever k
is not between 3 and 7 (inclusive), and false otherwise.
The integer y
represents a leap year if it is divisible
by 4 and not by 100, or if it is divisible by 400. For example, 1776, 1812, and 2000
were leap years, while 1789 and 1900 were not. Write a Boolean expression
that is true if y
represents a leap year, and false if it does not.
Read about "short-circuited && and ||" (on pages 205-206 of your textbook, for example). Then explain the difference in behavior between these two pieces of code:
if( y != 0 && x / y < 0.1 ) { ... }
and
if( x / y < 0.1 && y != 0 ) { ... }
(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" );
(2 points) I'm just about to finish a book, and I'd like something interesting to read. Any suggestions?
(2 points) Who were John Mauchly and Presper Eckert, and what were their roles in the history of computer science?
(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:
A list of all the private variables stored in each instance of Purchase
.
A list of all the public methods, including at least one constructor. Each method should be shown with return type, method name, and parameter list, as well as documentation describing what the method does.
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.