SCJP 1.4, SCJD 1.4, SCBCD (Preparing!)
SCJP 1.4, SCJD 1.4, SCBCD (Preparing!)
Originally posted by Min Huang:
Oh.. seems I have to use the buttons below for instead of tags. Please ignore the <code> and <pre> tags. Apologies for the inconvenience.
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.
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.
SCJP 1.4, SCJD 1.4, SCBCD (Preparing!)
Originally posted by Min Huang:
Hi Satish! Thanks for the quick reply.
Question/Concern: I assumed this as well, but my assignment doesn't require me to add or delete records in the database.
Min, here are two methods from your interface quoted:
When you implement the interface DB, you must provide a way of creating new records in the database right?
And regarding deleting records:
So you must also provide a way of deleting an existing record in db file i.e. to make the flag 1.
In fact, the database is already populated for me.
Yep, its populated for all(I think) so as to work and test our assignment probably.
So anyways, it means nothing to me whether a record is deleted or not (although it may interest future programmers working on the program).
You have to take into consideration. Please read above.
However, I did notice that there are no fields that indicate whether a record is booked or not.
One word -- "owner" Yeah, it is used to know whether the record is booked or not.
Would it be acceptable to use the valid/deleted flag to indicate whether the record is booked or not?
Not a good idea. deleted flag is to know whether a record is deleted or not. An existing record does'nt mean that it is booked right? CSR searches all the existing records according to the criteria of customer and books the record. So the answer here is NO. I think you have to use "owner" to know whether the record is booked or not.
If not, I don't see a way to indicate this, as I am not allowed to modify the db file format.
You are not allowed to modify the db file format -- that's right. But you have a way to indicate whether its booked or not as mentioned above.
But if I use the valid/deleted flag in this way, wouldn't it be bad for future programmers who need to implement add/remove record functionality?
Again, please see above.
The id value (an 8 digit number) of the customer who has booked this. Note that for this application, you should assume that customers and CSRs know their customer ids. The system you are writing does not interact with these numbers, rather it simply records them. If this field is all blanks, the record is available for sale.
SCJP 1.4, SCJD 1.4, SCBCD (Preparing!)
When I display a record that is not booked, the "owner" field should be all zero's, and when a CSR wants to book that record, s/he will be prompted for an "owner" id value, which will then be saved in that record. Any attempts to book a record with a nonzero "owner" id value should be denied. Am I correct?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
This parrot is no more. It has ceased to be. Now it's a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|