File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: URLyBird questions about database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: URLyBird questions about database" Watch "NX: URLyBird questions about database" New topic
Author

NX: URLyBird questions about database

Min Huang
Ranch Hand

Joined: Mar 17, 2004
Posts: 100
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?
My assignment file says this:
<pre>
The user interface for this assignment must satisfy the following criteria:
It must be composed exclusively with components from the Java Foundation Classes (Swing components).
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.
It must present search results in a JTable.
It must allow the user to book a selected record, updating the database file accordingly.
</pre>
And the data file format section says:
<pre>
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
</pre>
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?
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?
Here are the fields that the assignment file gives:
name, location, size, smoking, rate, date, owner
Any help is appreciated guys (and gals). Thanks.


SCJP 1.4, SCJD 1.4, SCBCD (Preparing!)
Min Huang
Ranch Hand

Joined: Mar 17, 2004
Posts: 100
Oh.. seems I have to use the buttons below for instead of tags. Please ignore the <code> and <pre> tags. Apologies for the inconvenience.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Min
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.

No need to apology to anyone. We all make mistakes first time while posting or editing. I will try to answer your post in detail in the following post.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Min..

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 best way to start is to "search" in the forum as many of the basic questions are previously answered. It does'nt mean that you should'nt post here, what I mean is there might be some posts who already explained your doubts in a more clear way than me or others who reply. You can do the search mechanism by clicking the "search" at the top of this forum.
Here is the link for your convinience.
SCJD search
Examples of search:
To start, you can enter "URLyBird" in the search and just go through them to some extent.
If you have any doubts like say on RMI server you can enter "RMI server" there and search.
And finally you are always very much welcome to post your questions and we are all more glad to help.
Good Luck.
Oops!! Forgot to welcome. OK, now "Welcome to JR-SCJD forum"
[Andrew: broke up quoted text within [code] blocks]
[ March 19, 2004: Message edited by: Andrew Monkhouse ]
Min Huang
Ranch Hand

Joined: Mar 17, 2004
Posts: 100
Hi Satish! Thanks for the quick reply.
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.

Question/Concern: I assumed this as well, but my assignment doesn't require me to add or delete records in the database. In fact, the database is already populated for me. So anyways, it means nothing to me whether a record is deleted or not (although it may interest future programmers working on the program). However, I did notice that there are no fields that indicate whether a record is booked or not. Would it be acceptable to use the valid/deleted flag to indicate 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. 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?
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Min
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.

Good Luck.
Min Huang
Ranch Hand

Joined: Mar 17, 2004
Posts: 100
Ohhhhhhhhhhhhhhhhhh... That makes sense. The reason I was confused was because my assignment had this to say about the "owner" field:

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.

...which made me think I should not edit any of those values. But upon looking at the db file, I realized they were all blanks.
Now, I think I get it, but I am in favor of asking stupid questions (because no question is too stupid), to make sure I actually get it:
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?
[Andrew: changed [code] blocks to [quote] blocks]
[ March 19, 2004: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11477
    
  94

Hi Min,
I have edited your post to put the code between [quote] and [/quote] UBB tags rather than [code] and [/code] tags.
The [code] tags use a monospaced font, and do not wrap text - these are ideal for code snippets where you want the reader to see exactly what you posted.
The [quote] tags use a proportional font and will wrap text to fit within a browser window. This is better where you are not relying on the spacing of words for imparting meaing, as having the text wrap will make it easier for others to read (no horizontal scrolling required).

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?

Yes, you are correct.
Some candidates are adding extra functionality which will allow a booking to be removed - in this case, if there is a non zero "owner" id value, the user may remove the booking. There is certainly no requirement for this in the specification, but it can be a nice feature.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: URLyBird questions about database