| CIT591 Midterm Exam Fall, 2003 |
Name_________________________________________ |
Please keep all your answers short and to the point. Do not provide extra information that was not asked for.
int p = 1;
for (int i = 0; i < 5; i++) {
p += i;
System.out.print(p);
}
124711
5
and 8).
It contains one (major) syntax error and one logic error. Correct both.public class Product {
int x = 5, y = 8, p = 0;
while (x > 0) {
x--;
p = p + y;
}
System.out.print(x + " times " + y + " equals " + p);
}
Syntax: Not in a method; surround with public static void main(String[]
args) { } |
byte: 1 byte
|
short: 2 bytes
|
int: 4 bytes
|
long: 8 bytes
|
|
boolean: 1 byte
|
char: 2 bytes
|
float: 4 bytes
|
double: 8 bytes
|
boolean ok(int[] array, int index) { boolean legal; if (index < 0) { legal = false; } else if (index >= array.length) { legal = false; } else { legal = true; } return legal; }boolean ok(int[] array, int index) { return index >= 0 && index < array.length; } // I gave some points for making the method shorter, if it was still correct.
public class ArrayTest {
public static void main(String[] args) {
int[] test = new int[2];
test[0] = test[1] = 5;
System.out.println(test[0] + "," + test[1]);
fiddle(test, test[1]);
System.out.println(test[0] + "," + test[1]);
}
static void fiddle(int[] test, int element) {
test[0] = 10;
test[1] = 11;
element = 12;
System.out.println(test[0] + "," + test[1] + "," + element);
test = new int[2];
test[0] = 20;
test[1] = 21;
System.out.println(test[0] + "," + test[1]);
}
}
5, 5
10, 11, 12
20, 21
10, 11
break
statement may only be used within what other kinds of statements?
java.lang.NullPointerException
at Test.run(Test.java:22)
at Test.main(Test.java:6)
at __SHELL1.run(__SHELL1.java:6)
at bluej.runtime.ExecServer.suspendExecution(ExecServer.java:187)
at bluej.runtime.ExecServer.main(ExecServer.java:69)
Tell where in the program the error occurred, and what you expect
to find when you look there. quit
to true if the String
variable command
has the value "quit",
and sets quit
to false for any
other value of command.Obvious classes are Book and Patron.
A Book should have a title, author, and due date, and probably other information, such as ISBN and/or Dewey Decimal number. (By the way, a Dewey Decimal number does not uniquely identify a book.) You might also want a unique ID for this particular copy of the book.
A Patron should have a name, an address, and a list of Books that he/she has checked out.
Somebody, or something, needs to take responsibility for checking books out, accepting returned books (a lot of people forgot this!), and sending notices for overdue books. I called this class the Library; a popular name seems to be CheckoutSystem or similar.
class Book {
String title
String author
Date dueDate
// Constructor, getters for each field
}
class Patron {
String name
String address
Book[] books = new Book[20]
int numberOfBooks
// methods
getters for name and address
void takeBook(Book)
void returnBook(Book)
int getNumberOfBooks()
Book[] getOverdueBooks()
sendNotice()
}
class Library {
Book[] books // quite large
Patron[] patrons
// methods
boolean checkOut(Patron, Book) // fails if too many books
void checkIn(Patron, Book)
void checkForOverdueBooks(Patron)
}