Originally posted by Min Huang:
Hello!
I finished the SCJP last year and now I'm back for seconds.
I have the URLyBird project (it took me like 2 days to get that name - I'm a little slow), and I have a question for you Java masters.
First question: The directions don't say I need add/delete functionality. What is the purpose of valid record/deleted record? Would I use it for booking a record? The purpose of the flag is to know whether the record is deleted or not. Here the flag only indicates that we need to exclude it from the existing records list. It may present there physically though. Why is it like this? Because while creating new records, we can resuse those records which are deleted(i.e. having deleted flag 1) to create new or just write a new record at the end of database file. Probably only place you need to deal with deleted records is the create method. Atleast, that's where I used, not sure of how others are dealing with this.
My assignment file says this:
And the data file format section says:
Second question: The assignment says I must implement <code>public interface DB</code>, but the code that is given doesn't conform to Sun's Java coding conventions or use javadoc style comments. Should I format the code so it does? Or should I use it as is?
I think you can change the comments to javadoc comments if they did'nt provide already. Also I think it is OK to format. Other than that, it is not advisable to change the code in the given interface.
Here's what was given to me:
package suncertify.db;
public interface DB
{
// Reads a record from the file. Returns an array where each
// element is a record value.
public String[] read(int recNo) throws RecordNotFoundException;
// Modifies the fields of a record. The new value for field n
// appears in data[n]. Throws
SecurityException // if the record is locked with a cookie other than lockCookie.
public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException,
SecurityException;
// Deletes a record, making the record number and associated disk
// storage available for reuse.
// Throws
SecurityException if the record is locked with a cookie
// other than lockCookie.
public void delete(int recNo, long lockCookie)
throws RecordNotFoundException,
SecurityException;
// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);
// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
public int create(String[] data) throws
DuplicateKeyException;
// Locks a record so that it can only be updated or deleted by this client.
// Returned value is a cookie that must be used when the record is unlocked,
// updated, or deleted. If the specified record is already locked by a different
// client, the current thread gives up the CPU and consumes no CPU cycles until
// the record is unlocked.
public long lock(int recNo) throws RecordNotFoundException;
// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws
SecurityException.
public void unlock(int recNo, long cookie)
throws RecordNotFoundException,
SecurityException;
}
Third and final question: There is a <code>int recNo</code> argument to some methods in the <code>DB</code> interface. I don't see a <code>recNo</code> field in the sample database file provided to me. In fact, there doesn't even seem to be a primary key. Should I be worried about this?
I think recNo is a way of identifying each record in the db file. One way of doing this is to make recNo same as the physical start location of each record in the dbfile. I there are many other ways also. Regarding primary key, well, for URLyBird assignment there are many debates in the forum that there DOES'NT exist one. So I think you don't need to worry about it.
Here are the fields that the assignment file gives:
name, location, size, smoking, rate, date, owner
Any help is appreciated guys (and gals). Thanks.