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 URLyBird interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird interface?" Watch "URLyBird interface?" New topic
Author

URLyBird interface?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,
In my assignment the following sentence maze me: Your data access class must be called "Data.java", must be in a package called "suncertify.db", and must implement the following interface.


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;
}


Can anyone tell me how this interface to be used?


SCJP 1.4 SCJD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Zhixiong,

Are you asking about how to use interfaces in general, or about how this particular interface should be used?

If you are asking about using interfaces in general, then Sun have a "Creating and Using Interfaces" tutorial that might help.

Assuming that you know how to create and use interfaces in general, then the instructions are telling you that your Data class must implement the DB interface.

Once you have done that, you have (strictly speaking) complied with the specific instructions. However it is good programming practice to program to the interface rather than to the specific class. So in general you should write code such as:rather than:Does this answer your question, or have I misinterpreted what you are asking?

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Andrew,
Firstly thank you.
Could you tell me why interface DB be in package suncertify.db , and the Data.java should be in that package too ?

Wishes to you,Zhixiong
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Zhixiong,

There is no real reason - it is just a package the Sun assignment creators decided to use. If they hadn't specified this package name, you could have used any package you like (however this would have made it very hard for them to test anything). Since they have specified the package name, you must comply with it.

Of course this does give you a simple package naming convention you can use for all your other packages. For example you might decide to put your GUI classes in the suncertify.gui package.

Regards, Andrew
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Andrew,
When talking about GUI, do you have any suggestion about GUI designment for URLyBird?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Zhixiong,

I wrote nearly 70 pages in my book on GUI development, both in general terms and specific information related to the project. I think your question is broad enough that I wouldnt even know where to begin with answering it (and I am not going to post 70 pages here ). Perhaps you could think about a more specific question (and preferably post it in a different topic, since the GUI has nothing to do with the interface).

Regards, Andrew
Ian Hamilton
Ranch Hand

Joined: May 02, 2006
Posts: 37
Hi Andrew,

Why is it good programming practice to program to the interface?

i.e. ArrayList implements the List interface

so it would be better to write

List aList = new ArrayList();

Why?

Thanks

Ian
Denis Zjukow
Ranch Hand

Joined: Mar 15, 2006
Posts: 39
Originally posted by Ian Hamilton:
Hi Andrew,

Why is it good programming practice to program to the interface?

i.e. ArrayList implements the List interface

so it would be better to write

List aList = new ArrayList();

Why?

Thanks

Ian


Hi, Ian. It's better to write to interfaces like this:

List list = new ArrayList();

because in this case you are not bound to implementation. In future you may change your mind and use LinkedList or maybe some custom class implementing java.util.List interface (which may work faster or need less memory). And you only need to change one line of your code. You can find more in Head First Design Patterns (and there are much more OO principles).
Ian Hamilton
Ranch Hand

Joined: May 02, 2006
Posts: 37
I kinda of figured that without really understanding it.

If we code;

-->List aList = new ArrayList();

(aList then loaded with data)

and then code

-->aList = new Vector();

won't this re-point aList to a new block of memory created for the Vector object (minus the data)?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Yes, you are right, and that is a good example of programming to an interface.

It is generally easiest to show why you should program to an interface by using an example of the potential problem caused by programming to a concrete class. So, take the following example:Lets say that you later realized that you should not have used a Vector in this example, and you wanted to use an ArrayList instead. In order to change this, you will have to make modifications in 3 places - twice on line 5, and once on line 14.

And this was an easy example - consider if you had continued to use the concrete Vector class throughout multiple methods in multiple classes in multiple packages - you could end up with a huge job trying to find and fix them all!

Now lets consider the better way of coding - programming to an interface:Now if I want to change to an ArrayList, I only have one small part of the code to change: one place on line 5. Much simpler.

Regards, Andrew
[ May 16, 2006: Message edited by: Andrew Monkhouse ]
Ian Hamilton
Ranch Hand

Joined: May 02, 2006
Posts: 37
OK I get it. Thank you Andrew great explanation!
Ian Hamilton
Ranch Hand

Joined: May 02, 2006
Posts: 37
Denis the book you sugggested is great - exactly what I was looking for. Thank you
 
GeeCON Prague 2014
 
subject: URLyBird interface?