Dr. Hacker (January, 2002)
Home Schedule Prime Directives Engineering Log Coding Standards Software Submission Checklist Dr. Hacker

 

1/18/2002

> Dr. Hacker, 
> My question is, how do I change the src directory structure so it doesn't
> mirror the entire package structure?  I'm not sure how you changed that when
> you did the demo in class.
>
> -- Resourceful 
You create the JBuilder folder structure manually. Right click over the project icon (or a folder icon) in the left hand window, and select "New Folder".
> Dr. Hacker,
> 
> I have 2 questions concerning the AccountInfo1 assignment:
> 1. A user could be both, for example Admin AND Faculty?
>     (Yes / No)
Ah, interesting question! Let's say "No" for now. Although it would be amusing to say Yes, no?
> 
> 2. In your assignment you mention:
>     "Clients of the AccountInfo class will want to use it to create, 
>       delete, query, and modify accounts."
>     
> If I wanna change the password of an account, would I do
> a) or b)?
>     
> a) 
> AccountInfo accountInfo = new AccountInfo();
> ...
> accountInfo.setPassword("account313", "newPassword");
> 
> b)
> AccountInfo accountInfo = new AccountInfo();
> ...
> Account account = accountInfo.getAccount("account313");
> account.setPassword("newPassword");
> 
> 
> I think b) is better because a) is kinda redundant. For a) you
> have to program the setPassword function in AccountInfo
> and Account.
> Do I have to take a) or b)?
> 
> -- I like composition
(a) is definitely bogus. (b) is definitely better.
> Dr. Hacker, 
> What do you recommend for the accountinfo1 assignment, interface or
> abstract class?  I'm currently using an abstract class, but you seem to
> hint at using an interface.  So I was just wondering.
>
> -- To abstract, or not to abstract
You are required to implement the most elegant and natural solution. :-) Here's a couple of short articles that will help you in your quest for the most elegant and natural solution:

http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html

http://www.javadigest.net/articlecache/article.php?sid=63

>> Dr. Hacker said the following in a previous email:
>> Have you read the instructions for installing Tomcat?
> 
> The problem is that I cannot find the instructions for installing Tomcat.
>
> -- Where in the world is Tomcat San Diego?
I feel an FAQ entry coming on!!! Repeat after me: "Google is my best friend." "There is no other search engine but google". "Everything I need, google can provide." OK. Now that you're warmed up, go to google, and type in "Tomcat installation". You will be amazed, amused, and bemused at how the first hit in google provides the information that you need. (They don't provide the "I'm Feeling Lucky" button for nothing.)
> Dr. Java,
> 
> I'm a student in 613 class and am confused with the forward e-mail. I'm
> not sure whether the following quoted message is applied to our Account1
> assignment.
> 
> I'm planning to use Applet. Should I change my mind to use Junit?
>
> -- Choosey GUI 
Absolutely. You know the Nike slogan, "Just do it"? In this class, I say "Just junit." Applets would be a waste of time. Remember that you will be living with this code for a long time, changing, enhancing, and improving it. You need a way to easily make sure that when you make a change, nothing has broken. That's what JUnit is good at. Applets aren't.
> Dr. Hacker,
> 
> One question to clarify: This package name is AcountInfo1, but the class
> name is AcountInfo?
No. Package names are always lower case. (See the Elements of Java Style.) Furthermore, the package name has to have the edu.hawaii prefix. So, the package name is probably something like: edu.hawaii.accountinfo The class name should capitalize all interior words (See the Elements of Java Style). So, the class name might be AccountInfo1. By the way, "account" is spelled with two c's.
> 
> 
> And this is how I implement Acounts:
> 
> AcountInfo is an abstract class. Administrator, Faculty and Student are
> another three classes that extends AcountInfo and implement the abstract
> methods. AcountInfo class has all common attributes and behavior of all
> acounts like name and passwd, the abstract method is about the acount type
> -- whether it is a faculty or student or administrator.
> 
> AcountInfo class has a static vector to store each instance of administrator,
> faculty or student object when it is first created. One manage acounts
> through this vector.
> 
> Is it satisfying your requirements? Thank you!
>
> -- Love those vectors
Vectors are bad. Very very bad. Vectors are a mistake from Java 1.1. You should never use Vector voluntarily. I will grade off if I come across Vectors sneaking around in your code, causing mischief and low performance. In Java 1.2 and later, you should be using the Java Collections Framework. (You might be saying to yourself at this moment, "But I don't know what the Java Collections Framework is!" I can tell you only this in response: "Google is your best friend." "There is no other search engine but google". "Everything you need, google can provide." Type in "Java Collections Framework" to google and be amazed at how quickly the solution to your temporary ignorance is provided.)
> Dr. Hacker,
> 
> I have little idea on how to deal with the following statement requirement
> in installing tomcat 3.2.4:
> 
>> Set the environment variable JAVA_HOME to point to the root directory of
>> your JDK hierarchy, then add the Java interpreter to your PATH
>> environment variable.
> 
> 
> I attempted to take care of that by doing the following in the command
> prompt:
> 
>> %set JAVA_HOME=C:\Program Files\JavaSoft\Jre\1.3
>
> -- Environmentally unfriendly
After installing the Java JDK, you usually end up with a directory such as the following (on Windows):

c:\jdk1.3

Set JAVA_HOME to that directory and all will be sunshine.

Have a good weekend.
Dr. Hacker

1/19/2002

> Dr. Hacker,
>
> Can you give us some hint on how to use ant? I installed Tomcat and JUnit,
> both are working ok. I installed ant too but seems there's some problem.
>
> I downloaded two 414 projects. The intallation of them needs build by ant.
> I failed in this step.
>
> -- Ants not marching
All 414 projects are structured the same way. You always need to install Java (and set JAVA_HOME) and you will need to install Tomcat (and set TOMCAT_HOME). On windows, a typical value for JAVA_HOME is 'c:\jdk1.3'. On windows, a typical value for TOMCAT_HOME (for the ICS 414 projects) is 'c:\jakarta-tomcat-3.2.3'. (In our class, we will be using Tomcat 4.0.1.)

You do NOT need to install JUnit or Ant for any of the 414 projects. Those packages come with the download. If you install Java and Tomcat correctly, and set JAVA_HOME and TOMCAT_HOME correctly, then after running setenv.bat, you should be able to type 'ant' (possibly with some target) to build the system.

> Dr. Hacker,
>
> Is windows 2000 required to use/run tomcat?  I have windows 98 and so far,
> tomcat isn't working.  Also, is there any 414 projects that I can look at
> without tomcat so that I can do the assignment by sunday?
>
> -- 20th century dude
I'm pretty sure I made this clear in lecture and in the notes: Windows98 isn't going to work for this class. And as I also indicated in class, some of you will need to learn the hard way. I'm not sure what else I can say or do at this point.

Yes, every single 414 project requires Tomcat. I've had copies of the Win2K disk available continuously since Thursday morning.

Cheers,
Dr. Hacker

1/20/2002

Greetings, hackers:

First, a general comment. Many of you have already submitted your Open Source Experience assignment, which is the good news. The bad news is that several of you have not followed the submission guideline:

"By that time, I (johnson@hawaii.edu) must receive an email from you containing the URLs to the two web pages containing those links."

Note the plural "URLs"---the above sentence means I need in the email TWO links, not a link to a page containing links. Those of you who resubmit by midnight tonight will be OK.

> Dr. Hacker,
>
> I had a hard time to make Win2000 work on my new laptop(the proginal OS is
> Win XP home edition).
>
> Do you think I can use WinXP home edition to replace Win2000 for all
> homework in this class?
>
> -- CDs are bugging me
No. I believe WinXP home edition is this year's version of Win ME, which is last years version of Windows98, which is the prior year's version of Windows95. None of them will bring you anything but pain and sorrow.
> Dr. Hacker,
>
> When is the papers due?  it says:
> Due date:  Sunday, 1/20/2002, midnight, via email.
>
> does this mean I have all of Saturday but once the clock hits midnight, the
> date changes to 1/20 and its due?  Or does it mean that I also have all of
> Sunday to work on it?  Some people may translate you due date differently.
>
> -- Thank goodness Hawaii doesn't observe Daylight Savings Time
Aw, c'mon. "Sunday, midnight" does not mean "Saturday, midnight", at least in the particular space-time continuum that I live in.
> Dr. Hacker
>
> on the section about prime directive 2, becuase it is on the non-developer's
> point of view, all we have to do is be able to use the software.  So for the
> projects that ics414 did, we don't need to download an install their package.
> The use of the software would just be to go to the homepage and
> register/login  or whatever it says to do, and that would be using their
> software satisfing PD#2.  Downloading and installing the package using the
> steps on their README.html would actually be stuff on PD#3.  Am I viewing this
> assignment correctly?
>
> -- Down and out
That is a reasonable interpretation. However, it would also be interesting to evaluate the software from the "user/sysadmin" point of view: someone who is not going to enhance it, but must still download and install and bring it up. But you can take it either way.
> Dr. Hacker,
>
> Should we bring 1 copy of our open source reviews, or sufficient copies for
> everyone in class?  If we need multiple copies, how many?
>
> Or, will you use the links to show the reports on the projection system?
>
> -- Love that xerox machine
No need for hard copy. I am hopeful that I will not need you to submit anything on paper for the entire semester, though that remains to be seen.
> Dr. Hacker,
>   i was wondering what the queryAccount method does.  for now, i made it
> print out the info about the user account.  am i missing anything?  thanks
>
> -- Command shell man
This is a utility class, and should not print out anything. I don't think you need a method called "queryAccount". Instead, to query the account object, you would invoke accessor methods such as "getPassword" or some such thing.

Cheers,
Dr. Hacker

1/23/2002

Aloha, hacker-grasshoppers, 

First, I want to gently remind you about the engineering log. Remember that you should be trying to make at least one entry in your engineering log during each programming session, and that each session/day should start on a new page. The goal of the engineering log is to get you to start "micro-planning"; thinking about things a bit before doing them, and also providing a repository for lateral thoughts that don't fit the task at hand but are still important.

It's possible, even probable, that some of you have started work on AccountInfo but have forgotten about the engineering log. If that's the case for you, then please start immediately. Please *don't* add in retrospective entries to try to "cover your tracks". Just start today. If you haven't yet started the engineering log because you haven't yet started working on the programming assignment, then of course that's good news with respect to the engineering log, but bad news with respect to the assignment. :-)

> Dr. Hacker,
>
> Do you have any hint on ensuring the validity of names and passwords?  I
> wrote two Exception class, InvalidNameException and InvalidPasswordException
> and had methods setName and setPassword throw them.  But I'm only sure on how
> to ensure that the password is at least 6 characters.  How might I check that
> a password have at least one upper case letter, lower case letter, and digit?
> And how to do all the other restrictions of valid names and passwords, I
> currently have no idea except to manual make sure all invalid character are
> not in the String and to check that there is at least one upper case letter
> by checking each Alphabet at a time.  There must be a better way to do this.
> Can you give a hint if there is?
>
> -- Loop de Loop
There are two issues here. The first issue is the design of your exceptions. I'm not sure that you really want to have a unique exception for every single property of a class---that will really explode into a lot of classes over time. There are a couple of things you can do. First, if you keep these two exceptions (which is not unreasonable, just beware of exception explosion), then you really should create a superclass called something like "AccountInfoException" which these two exceptions inherit from. That way, clients of the class can simply catch AccountInfoException and be sure of getting either exception. Another approach is to have a single exception with a string (or other object) that indicates the type of problem that occurred. Which approach is better depends ultimately on client needs. In the real world, as in this class, client needs are not often well knownn and change over time anyway, so you often need to redesign your initial solution anyway. If it were me, I would go for the simple solution right now (a single exception type, with a string indicating the problem), and be aware that it might need to be changed later.

The second issue is your question about validity checking on the password. There are three approaches I can think of.

(1) Do the simple thing, which is to first check the length for validity and reject passwords that are too long. If the password passes that test, then do a loop, getting each character in the password string, finding its integer value, and comparing it to the integer ranges for the upper and lower case characters, etc. (You can combine the length check and the character check in the same loop, of course.) You'll have some flags that you set in order to make sure that at least one of each of the required characters are present.

(2) If that seems like humbug to you, then you could do the fancy thing, which is to use a set of regular expressions to do the check in just a few lines of code. In Java 1.3, that requires downloading a regular expression package. I recommend the Jakarta regexp package (http://jakarta.apache.org/regexp/). Remind me to talk about how you would add such a component to your build package today in class if I forget. (It will probably take most of you longer to figure out how to write the correct few lines of regexp code then it will to write the many more lines of code to correctly check it manually. So, if you're trying to minimize the total elapsed time to finish this assignment, go with (1). However, regular expressions are a really, really great tool, and once you understand them, you'll find they can dramatically improve your productivity in a lot of situations, so I would hope that at least some of you would take this opportunity to invest time in getting comfortable with them. You probably want to write the loop first, just to make sure you know what you're doing, then upgrade to the regexp solution.) Note that password validity checking is a fertile and important playground for test cases. I just generated 11 test cases in 45 seconds to check various legal and illegal passwords.

(3) In Java 1.4, there is a built-in regexp package. The third alternative would be to upgrade to Java 1.4 and use it. Please don't do this, because you'll run into all sorts of incompatibility problems. There are only minor syntactic differences between the Jakarta regexp package and the Java 1.4 package, so if you pursue approach (2), you'll be able to migrate to the 1.4 solution later with little problem.

Cheers, 
Dr. Hacker

1/24/2002

Greetings, codeheads,

First, I want to give you a heads up about one aspect of adapting the stack template to your accountinfo program. Yesterday, I gave the impression that you could simply copy your accountinfo.jpx file into the stack template's lib/JBuilder directory and all would be copacetic. That's potentially not the case: if you do that, it is possible that (a) the jpx file will no longer find the java files, or (b) the jpx file will point to the "old" version of your java files, not the "new" version inside the stack directory.

There are two solutions to this:

(1) The simple solution. Create a *new* project from scratch. Start by allowing the system to create the jpx file in the top-level directory of the template. After setting up the project and adding the files in your template directory to the project, you right click on the project icon in the left window, select "Rename project", and then rename it into the correct directory (lib/JBuilder).

(2) The less simple solution. Copy your old jpx file in the lib/JBuilder directory. Then bring up the jpx file in a non-JBuilder editor (such as Emacs, or even the dreaded NotePad.) Manually edit the links inside this file so that they move out of the lib/JBuilder directory and back down into the other directory (such as ../../src/edu/hawaii/....). These must be relative references, not absolute references (such as c:\jnk\accountinfo..), because if they're absolute, then the jpx file won't work when someone else (such as me) opens it.

Yes, this is a little bogus, but it's just the overhead of transferring from one project structure to another. Once you understand what's going on, it's only a couple of minutes more work.

A meta-level comment: Don't be misled into thinking that the "real work" in this class is writing the accountinfo class, and that creating the stack template and editing the jpx file is just "overhead". The process of creating project structures and integrating tool files into it is just as much "real work" in software engineering as the development of the software itself. So, you are learning something worthwhile while you stumble through this.

>>> A previous question was:
>>>
>>> How might I
>>> check that a password have at least one upper case letter, lower case
>>> letter, and digit?
>
> Dr. Hacker,
>
> It sounds to me like this student is asking about the existence of methods
> like the character tests for isDigit, isLetter, isLetterOrDigit, etc., to
> make the testing easier.
>
> -- Alternative interpretation
Perhaps. See the JavaDocs for the Character class in the JDK 1.3 doc directory.
> Dr. Hacker,
>
> The assignment write up says, "The test cases should be implemented as a
> set of methods belonging to a class named TestAccountInfo."
>
> But... I think I remember you saying in class today that we shouldn't name
> our main class starting with "Test".  So, should we just call it
> AccountInfoTest or something like that?
>
> --- Name de Plume
Shoots. This is a potential issue. I'm not sure what the test task will do when there is no JUnit test in the class. The worst case is that a failure is reported (the failure being that there is no JUnit test defined in the class being run by JUnit). Or nothing bad might happen.

This is only an issue with the first assignment, fortunately. Let's decide that you can do it either way, just remember to document in the README and how you're naming the class (or classes) doing testing and in your JavaDocs that the class (or classes) you chose for testing is a test class.

Note that if you're using JRefactory, then it's quite simple to rename classes so you could try it either way.

> Dr. Hacker,
> I have a question. Ever since the very first day of class, your
> suggestion to change operating systems to either Windows 2000 or XP scared
> me out of my wits. Since then I've been deliberating for a solution and I've
> finally obtained one. I just recently obtained a refurbished computer,
> however this nice tool came equipped with Windows XP Home Edition. My
> question for you is, is it possible to complete this course successfully
> if I used the Home Edition or would it be better to use either Windows
> 2000 or XP Professional.
> 
> -- Freaked and frustrated
First, I want to point you back to the the Q&A email for 1/20/2002, where this exact same question was asked and answered. I am happy to answer any question once. I am less happy when asked the same question twice in three days.

As I said in my prior email, Home Edition won't work. Windows 2000 will. XP Professional is an unknown quantity. However, since XP Pro is this year's version of Windows 2000, it will probably work.

As for being scared out of your wits, I sympathize. At the end of the day, though, getting comfortable with OS installation is a very useful skill. Take it as yet another learning opportunity from this class.

Cheers,
Dr. Hacker

1/25/2002

> Dr. Hacker,
>
> I rushed out of class yesterday & forgot to pin you down on the
> .jpx.local and .jpx~ question. What's the deal with files with this extension?
>
> -Jay Builder
This is an excellent opportunity to motivate one of the "meta-level" educational aspects of this class. Most of you are accustomed to the "sage on a stage" format for education: you have a question, you ask the instructor. It's certainly the easiest thing to do, and after all, you're paying me to answer your questions, right?

Well, no. Actually, you're paying me to teach you how to be successful professional software engineers (actually, my real goal is to help you become happy and fulfilled human beings, but that comes later on in the semester :-). And there are two problems with the "ask the instructor whenever a question pops into my head" mode. (1) It takes time to get an answer. At least a day, maybe more. Sometimes you don't want to wait that long. (2) I won't always be there to answer them for you. Like as of May 15th. Thus, it is quite important to me that I try to use this semester to "teach you how to fish", rather than simply giving you a fish whenever you're hungry.

While I certainly don't want to discourage questions, I am going to sometimes "deflect" them and have you look someplace else (typically the net) for the answer. One of my best students from 413/414 last year recently told me that this approach of mine was one of the most important benefits that he got from the class--he now realizes that when he started the class, he had no idea how to *effectively* use the net to get answers to his programming questions. On the other hand, another student dropped the class after 413 and complained in his course evaluation about how I made people use google to find out answers and that wasn't what he was paying his tuition money for. Sigh.

Let me bring up another issue: my time, your time, and this class is a limited and precious resource. Over time, I want us all to learn how to use this resource as effectively and productively as possible. The better you folks get at using the net to answer "simple" questions, the more time we will have in class and in emails to focus on "hard" questions. There are plenty of questions we can discuss in this class that aren't answered by simple google searches or by looking in your reference books, and those are the questions that, IMHO, you are paying your tuition money to have my insights about.

OK, at long last, back to the questions: what is *.jpx.local, and what is ~.jpx~? These are clearly JBuilder files. There are two strategies for answering this question yourself. First, read the JBuilder documentation, which is available from within JBuilder. Second, do a google search. For example, I went to google, clicked on the google groups button, typed in "jpx.local", and the very first hit explained exactly what the jpx.local file is. (Note that while I know what jpx~ files are, I didn't know the purpose of jpx.local files until I did the google group search myself. I'd say it took me roughly 15 seconds to find out the answer. And as it's taken me about 15 minutes to type out this response, you can see that I view this issue as quite important since it would have taken far less time to simply answer the silly question directly. :-)

> Dr. Hacker,
>
> Can I adapt my accountinfo1 to stack template by copying those files that my
> accountinfo doesn't have to my current accountinfo1 directory?
>
> - Vice Versa
Good question. My opinion is as follows: once you _understand_ what's going on, then you can successfully go either way: either use a copy of the template as the basis and add stuff in, or else do what you're suggesting, which is to use your current directory structure as the basis and add the template stuff in. So there's no a priori reason why you can't. The reason why I suggested that you use the template as a basis is that it more-or-less guarantees that your project will have all the required components in some form. I still think it's somewhat safer to do it the way I suggested, but if you are careful you should be able to go the other direction without a problem. Just test everything out and double check to make sure everything that should be there is there.
> Dr. Hacker,
>
> JRefactory's pretty printer tabs 4 spaces while the class standards say we
> should use 2 spaces.  Is it okay to leave pretty printer's formatting of 4
> spaces?
>
> - Indentationally challenged
Absolutely not! It is your responsibility to make sure JRefactory is configured to obey the Elements of Java Style standards (and our own JOSSE extensions). Fortunately, it's not hard. Bring up the JRefactory configuration settings window and read through the entire thing. That setting (and many others) can be easily changed.
> Also
>
> What is the different between the two uses of Exception handling:
>     myMethod() throws Exception {
>         try{}catch{}
>     }
>
> and
>
>     myMethod() {
>         try{}catch{}
>     }
>
> I've done both, but seems to be no difference.  Which do you prefer and why?
I believe all experienced Java programmers have a very definite preference for one of these code fragments. BTW, neither code fragment is syntactically correct, which makes it way more difficult to interpret your question. However, assuming I understand what you're asking, what you need to do right away is to read the section in Java in a Nutshell regarding exceptions and exception handling. Next, I would go to google and type in "java exception handling tutorial", and click on the very first link that comes up. I am confident that you will then know not only the difference between these two fragments but also a lot of other important information regarding exceptions that you didn't know before.

More specifically, although the compiler may not complain, one of these is definitely incorrect from a software engineering point of view and I will mark off if I catch it. (Note that this is a hallmark of software engineering as opposed to programming: the fact that a program compiles doesn't make it "right".)

Incidentally, now that I've addressed this question, it is fair game for a midterm exam question (i.e. given two syntactically correct code fragments similar to those above, discuss which one is incorrect and why.) Thus, unless you know the answer to this question already, I advise you to read the Nutshell section and the online tutorial at your earliest convenience. Most of you will learn some important additional information about exception handling in the process (checked vs. unchecked exceptions is a particularly interesting issue) which will help you in your programming activities. You might even want to print out portions of the tutorial so that it would be available to you during the midterms and final.

Have a most excellent weekend. 
Dr. Hacker

1/26/2002

> Dear Dr. Hacker,
>
> I'm wandering around which collections to use for holding objects. I
> assume you will ask us to add "sorting" and "search" mechanism in. Is this
> assumption correct?
>
> - Informationally hidden
Great question! This, in fact, is one of the most fundamental software questions out there---how do you "design for change?" A very famous software engineer, David Parnas, wrote some seminal papers concerning this issue. A nice one page summary with pointers for further reading is at:

http://www.construx.com/cxone/Design/StrategiesAndMethods/CxBest_DesignForChange.pdf

A google search on <"design for change" parnas> will yield other pointers. (I found it amusing that I uncovered a couple of software engineering midterm exams with this search, indicating the importance of this topic. You might want to check some of these out.)

Here's the key insight: if you design your system correctly, then *you don't have to choose the "perfect" container data structure initially*. The reason is because if you need to change it later, you can do it *without affecting any clients of the class*. And that's one of the key contributions of the collections framework: it provides you with abstractions that clients of a module can manipulate without forcing the module to reveal the internal data structure chosen.

Here's a simple example. Let's say the client of the container needs to be able to get at all of the objects in the container. And let's say the container is using, say, ArrayList to hold the objects. One way to satisfy the client's need is to provide a public method called getArrayList() that returns the ArrayList to the client. That would satisfy the client's need, but it would be a bogus solution. Why? Because now you can't change the internal implementation from an ArrayList to, say, a TreeMap without changing the interface exposed to the client! In other words, you haven't *hidden* the underlying data structure from the clients.

On the other hand, let's say that you satisfy the client's need by providing a method that returns an "iterator" over the elements of the container. (BTW, for consistency with other collection classes, you should name this method iterator(), not getIterator().) Now, if you decide to change from ArrayList to TreeList (or whatever), then you can do it and the client interface is unchanged.

Food for thought, and potential midterm question: under what circumstances would you *have* to actually deploy a solution in order to find out what the optimal choice of internal data structure would be? (In other words, under what circumstances would no amount of pre-implementation design be sufficient to determine the "right" choice of data structure?)

> Dear Dr. Hacker,
>
> are we supposed to write down stuff like converting our accountinfo1 class
> into an Ant-based build in our engineering log?  Or do we just write down
> coding ideas?  Those are the only questions I have for now.  Everything
> seems fine for my project except that when I run "ant test" I get 66
> errors from checkstyle.  I think it's because I didn't use javadoc style
> comments.  It said something about that in the errors.  I've never done
> javadocs, so I'll have to read about that over the weekend.  I hope after I
> do that everything is okay.  By the way, I just wanted to let you know I'm
> really enjoying this class.  Pretty cool stuff.
>
> - Amazed and confused
Regarding the engineering log, yes, you should write down something about every major task, because one of the things the engineering log should provide is the ongoing "to do" list for your work. You should *also* be writing down coding ideas.

Glad to hear you're enjoying the class. We aim to please.

> Dear Dr. Hacker,
>
> You mentioned the midterm in a previous email. What should we be doing to
> prepare?
>
> - Looking ahead
At this point, I suggest that your main preparation focus be on creating and organizing your personal library of information. For example, some midterm questions are going to be based upon questions you've asked via email. It would be a good strategy to print out every Q&A email and make a collection of them, and also skim through the back issues from time-to-time. Any time I provide a link in an email, or suggest you search google on a particular topic, it becomes fair game for the midterm. A good strategy is to print out these links (or read and make notes on the contents). The lecture notes are an obvious source of midterm material (maybe *too* obvious :). One of the impacts of an "open source" policy on midterms is that I can't simply ask you simple questions that could be answered by just copying down the contents of a slide.

Cheers, 
Dr. Hacker

1/27/2002

> Dear Dr. Hacker,
>
> In reference to the following question from a previous column:
>
>>> What is the different between the two uses of Exception handling:
>>>     myMethod() throws Exception {
>>>         try{}catch{}
>>>     }
>>>
>>> and
>>>
>>>     myMethod() {
>>>         try{}catch{}
>>>     }
>>>
>>> I've done both, but seems to be no difference.  Which do you prefer and why?
>
>
> I've read the Java in a Nutshell and the Sun info you mentioned.  I
> understand that the general objective is to either catch the exception and
> handle it (second situation), or to let it rise to the top and be handled at
> the next level (using throws clause but not catching.)
>
> The other book I use for reference (Deitel, Java How to Program) also
> discusses exception handling and allows for rethrowing an exception that has
> been caught --- and may have been partially handled at the current level, or
> a decision is made to not handle the particular exception and to pass it back
> up the chain.  In that case, the first approach (of course specifying the
> specific exception being thrown, not just Exception as a catchall) would
> apply.
>
> Are you implying that rethrowing an exception is not good form  --- i.e.,
> that exceptions should be exclusively either caught or passed up the line,
> but not both?
>
> -Exceptionally yours,
This is interesting, and points up the problem when "example code" is underspecified. There are actually many different interpretations of what this question was actually about, and the only way to decide among them would be to see a real code example that actually compiles and runs. My interpretation was actually different than yours, but both of them fit the partial data provided. In future, when supplying example code (which is a good idea), one should always err on the side of completeness if you're not sure how much code to provide. (On the other hand, don't simply provide the complete listing of your entire program. You should always pare down your code to the minimal program that illustrates your question or problem.) To help you understand my interpretation of the original question, note that the following is a syntactically correct method definition:

public void foo () throws Exception {}

In other words, you can legally declare that an exception is being thrown by a method even when it obviously won't throw it. (It is interesting to think about why this needs to be allowed in the language specification.) Although this is syntactically legal in this case, it is clearly a style error: you should never mislead the clients of your class by declaring that an exception is being thrown when it won't be.

Let's move on to today's question, though. Rethrowing an exception is often good form. For example, in AccountInfo, someone might pass a null pointer in as a value of an object, which could potentially throw some sort of string-related exception inside your code. Rather than letting that string-related exception get thrown back to the client of the class, it would be better style to catch that "internal" exception, then rethrow it as an AccountInfoException. That makes the interface to the AccountInfo class much cleaner for clients; they aren't having to deal with 14 different kinds of exceptions.

Now, there is a nice design pattern for rethrowing exceptions. It's called the "Chained Exception" pattern, and the idea is that when you catch internal exceptions then rethrow them as a new "higher level" exception, you include the internal exception as a component of the new exception. Exception chaining is very easy to implement, and an overview of the technique is available here:

http://www.javaworld.com/javaworld/jw-09-2001/jw-0914-exceptions.html

In fact, exception chaining is such a useful technique that it will be provided as a built-in feature in Java 1.4:

http://java.sun.com/j2se/1.4/docs/guide/lang/chained-exceptions.html

So, catching and rethrowing is often good, and if you do that, exception chaining is a good pattern to implement. On the other hand, sometimes it is good to catch and handle exceptions internally. For example, in some situations, you might want to provide "default" values for certain attibutes, and indicate in your JavaDocs that if an illegal value is passed, the default value will be substituted.

Finally, one thing that you definitely shouldn't do is simply "swallow" exceptions. If an exception occurs, you have to deal with it somehow. You can't simply catch it and forget about it and hope that the client will somehow rescue you.

> Dear Dr. Hacker,
>
> I can't fit my zip file on to a floppy!  My zip file is 3MB.  Should I
> put it on a CDRW or should I post it on the web?  I don't have a zip
> drive, but if that's what it comes down to, I'm sure I can find one to
> use somewhere.
>
> - Stack overflow
Geez, who's the instructor for your class? What a bonehead! At any rate, the assignment specifications must be changed so that you will submit your assignment by posting the .zip file to your uhunix web site and sending the URL to the instructor. Then, in class, I'll use the web to download and install your software.

Once we move to CVS, the submission mechanism will be much easier.

> Dear Dr. Hacker,
>
>> (1) Do the simple thing, which is to first check the length for validity and
>> reject passwords that are too long.  If the password passes that test, then
>> do
>
> I didn't find any information about the max length of account name and
> password. Will you specify it?
>
> - Long John
Did I already mention that someone was a bonehead? I should have said to reject passwords that are too *short*. The specification does not indicate that there is any limit on the maximum length of passwords.
> Dear Dr. Hacker,
>
> The project seems to be working ok, including Ant build/dist.
>
> Next question...should my original (default) constructor using interactive IO
> be deleted, since it's not really tested in the program?
>
> I originally wrote a default constructor using interactive IO to get user
> information for creating the class.  My assumption is/was that new accounts
> should not be created unless/until it is possible to verify all of the
> account information, since I don't want to give someone an account unless
> they give me valid information to start (but I'm not really validating the
> email addresses, just checking for an entry).
>
> I subsequently wrote a parameterized constructor that can be used for testing
> and provides all the user information.  That is the constructor being tested
> exclusively by the test class.
>
> Since the default constructor is not tested by the test class, should it be
> removed from the project (for cleanliness)?  Or should it be left for
> completeness?
>
> - Love that command line input loop
Good for you for knowing an important general rule of good programming practice: constructors should always return a valid object from the perspective of the application domain. For example, if you have a class called "Account", then if you make a new Account instance, you should be required to pass the account name and password, and an exception should be thrown from the constructor if the account name already exists, if the password is illegal, etc.

Another implication of this rule is that you need to declare a private default constructor:

private Account() {}

This is to ensure that clients of your system cannot write the following code:

Account bogusAccount = new Account();

Moving on, it appears that as part of the implementation process, you implemented a default constructor that reads from the command line in order to fill in fields. This is one way to go, although once you know how to use JUnit, you will probably realize that it is faster and more efficient to write JUnit tests from the start since you have to implement them eventually anyway. In Extreme Programming, this approach has been coined "Test First Design":

http://www.extremeprogramming.org/rules/testfirst.html

So, the answer is: Yes, you should remove the default constructor for a number of reasons:
the default constructor should be private
in general, ADTs should not do any I/O themselves. I/O should be the responsibility of the client classes.
even viewing your I/O as a "developer level" capability, it is more appropriate to use test cases to exercise the interface. Now that you have them, there is no need to have the interactive output inside the class itself.

Cheers, 
Dr. Hacker

1/28/2002

> Dear Dr. Hacker,
> 
> I am busily revising all the stack files to be accountinfo1 files. What
> should I do about the following that appears in several of the .html files?
> 
>        $Id: JBuilder.html,v 1.1 2002/01/23 02:39:47 johnson
>
> - Confused in configuration management land
That is revision data that is automatically maintained in the files by CVS. For this first assignment, let's just ignore it. Once you start importing your files into CVS, these strings will be automatically updated. I prefer that you ignore them for now rather than delete them, because you'll just want to put them back in later. Thanks for asking, though.
> Dear Dr. Hacker,
> 
> regarding the sorting stuff, is it part of description that we need to
> include? To my opt, we still can write another sorting function to sort out
> the whole object list as option method. But if we need to create object, add
> into collection list then sort it or pass in the object, sorting the list and
> search it. I think this is unnecessary plus this should not really go faster
> when you do the search with sorting. Even if you have sorted list whenever it
> has create the object, that will even slow down overall speed. For example, I
> just did the linear seach for the modify function or query function for the
> proj. The worst case is still going to be O(n) because just linear for loop,
> but if you add sorting method whenever you create/search, the best case will
> still be like O(nlogn). But if you create bunch of objects without any
> sorting and sort before you close of collection list and next time you create
> object with sort collection list, it indeed will help to search object in
> this way intially. Other than that, I could not find any reason why we need
> to include sorting in search/create function.
>
> - Out of sorts with this assignment
OK, if I understand you right, I can suggest a few things. First, you should look at the TreeMap collection implementation, which provides a really nice data structure that is fast for both insertion/deletion and also maintains the collection in sorted order. You definitely don't want to implement your own sort algorithm when Java provides much better ones for free. Second, the ultimate performance of the class is going to depend upon the actual way in which it is used. None of us has any data yet to indicate how clients are actually going to use this class, and under what circumstances they'll sort vs. insert vs. delete. Until you can get good usage profiling data, it is not possible to come to conclusions about whether or not it makes sense to keep it in sorted order or not. Third, (and I'm not sure you're implying this, but I'll say it anyway) you definitely don't want to just return the collection to the client and make them sort it.
> p.s.
> One thing about your link on my email is broken..maybe just mine but I think I
> still can link up if I type all of linked words. Correct my bad thinking!
No, your thinking is good. It's my mailer that's bad. When URLs are long, the mailer inserts newlines so that some components of the URL end up on the next line and thus aren't recognized as part of the URL. You'll just have to type it in correctly yourself. Sorry for the inconvenience.
> Dear Dr. Hacker,
> 
> If our uhunix account is about to be full and cannot accomadate the 3M
> project, can we put the link to an outside website?
> 
> - Out of space, out of time
Yes, Virginia, there is a Hacker Claus. I just got word back from ITS that everyone has had 10MB added to their account. So everyone should be able to post their assignment to uhunix now.
> Dear Dr. Hacker,
> 
> I have one question.
> 
> Everything is going on great with my package. However when I use
> "ant test", there is one line indicating a failure:
> 
>     [junit] TEST edu.hawaii.chenx.accountinfo1.TestAccountInfo FAILED
> 
> And when I check the build/test_output result, there is one error:
> 
>     Class edu.hawaii.chenx.accountinfo1.TestAccountInfo has no public
>     constructor TestCase(String name)
> 
> Since we are writing our own TestAccountInfo class, should we include
> a TestCase constructor in it? I guess this is caused by not using JUnit
> this time, so maybe we don't have to pay attention to this little bug
> as you indicated before?
Correct. If you're not using JUnit but running the 'ant test' task, you're going to have problems. This is OK for this assignment. Remember to document in your README file the way in which someone actually runs the tests on your code (and what the expected output should be.)
> Dear Dr. Hacker,
> 
> I am a little unsure of how to code the throws clause in a method declaration
> that has more than 80 characters in it.  In Java style, it says to break
> apart long lines and to align the parameter on the next with that of the
> first on the previous line like so:
> 
> public void createAccount(int accountType, String username, String password,
>                           String firstName, String lastName, String email) {}
> 
> My question is when you use a throws clause, how should you align it.
> 
> Like this:
> public void createAccount(int accountType, String username, String password,
>                           String firstName, String lastName, String email)
>   throws AccountInfoException {}
> 
> Or like this:
> public void createAccount(int accountType, String username, String password,
>                           String firstName, String lastName, String email)
>                           throws AccountInfoException {}
> 
> I have tried to look for an example that shows this, but I was unable to find
> one.  I feel that the first one is a little harder to read than the second,
> but Pretty Printer will set it to that first one whenever I run it.  Another
> question...should we always start our throws clause on another line, even if
> the clause can safely fit on the first line?
> 
> Like this:
> public void deleteAccount(String username)
>   throws AccountInfoException {
>   accounts.remove(indexOf(username));
> }
> 
> Or like this:
> public void deleteAccount(String username) throws AccountInfoException {
>   accounts.remove(indexOf(username));
> }
> 
> I just ask this because otherwise, half of my methods will look like the
> createAccount method and the other half will look like the deleteAccount
> method.  I'm pretty sure I should only keep it on one line though since Java
> style says to only break apart long lines.  Just asking for
> confirmation...man, correct coding style and documentation is almost harder
> than the coding itself!! :)
> 
> Thanks,
> Exceptionally Anal :)
I've looked through my books of wisdom, and my current reading of the holy grails is as follows:

* Whenever you have to break up a line and introduce a line break, align the next line with the line immediately above it. So this is the correct one:

> public void createAccount(int accountType, String username, String password,
>                           String firstName, String lastName, String email)
>                           throws AccountInfoException {}
* Do not uniformly start your throws clause on the next line. In a sense, that is "lowest common denominator" consistency. If the throws can fit, keep it on the line. If it can't, then break it.
> Dear Dr. Hacker,
>
> Now that I am happily using the prettyprinter, I am wondering what to do with
> this javadoc line the prettyprinter puts in there with "Title" on it.
> Obviously, the Title of the class is the name of the class which is also the
> name of the file, so putting that in there would be utterly redundant. So
> title of what? The whole project?
>
> - Entitled to an explanation
I have never seen this behavior from JRefactory. However, JBuilder has a default documentation template that it inserts when creating classes from scratch. I would guess that this is what you're referring to. Personally, I find JBuilder to be pretty pathetic in this case, and I just delete that stuff immediately.

Cheers,
Dr. Hacker

1/29/2002

Dear Readers,

I received the following as part of an email from a former student who is now a developer on the mainland (and who is working with me from there on the open source Hackystat project.) I thought you would appreciate an independent, industrial perspective on the class:

> BTW, I also spent some time at http://csdl.ics.hawaii.edu/~johnson/413s02/. 
> The content of 613 is getting better than ever. I took 613 twice, but if i 
> were in UH now, I would have taken this class again. Ant and junit are the de
> facto tools for java developers. Both my previous job and current job use
> these tools at a daily basis. My current company USUALLY has them in the
> interview questions. The only thing that I found missing is design pattern.
> Almost any job interview for a Java developer position would involve
> questions about design patterns. Thanks for making the slides PDF available
> to public, I went through all of them so far, and learned something from each.
I agree with him that design patterns are a crucial topic and, in fact, we have already touched on some (for example, ensuring that constructors always return valid instances.) My belief is that the best way to teach design patterns is through everyday practice rather than as a "unit". However, maybe near the end of the semester I'll have a class specifically devoted to design patterns that familiarizes you with the terminology and summarizes the ones we've seen during the semester.
> Dear Dr. Hacker,
>
> I have a question about the constructor of account, student,
> administrator, and faculty.  i have a constructor that takes in all the
> parameters like firstName, lastName, email etc.  Can a constructor throw
> exceptions?  i'm trying to verify a valid password in it but i cant seem
> to get it working.  should i seperate this and make another method to
> create an account w/ the variables?
>
> - Needing constructive criticism
Yes, constructors can throw exceptions, and no, you shouldn't create an "empty" constructor that returns an invalid object that is made valid through subsequent method invocations.
> Dear Dr. Hacker,
> 
> Do we have to write our own README.htm, history.htm, etc from scratch? Or
> can we use what was provided in the Stack system and just change relevant
> areas?  eg. your name to our name, stack to accountinfo, etc..
>
> - Hoping that plagiarism is the sincerest form of flattery
I heartily recommend that you start with what I gave you and change only those parts that differ in your implementation. My goal was to give you template text that you could use as a basis.
> Dear Dr. Hacker,
> 
> ok, i'm having some problems w/ replacing the stack files w/ accountinfo
> files.  First, where are we suppose to store the ".class" generated?  Also
> if you get the properties of the project window up, what folder is the
> "working directory" suppose to be pointing too?
> 
> - Classically confused
OK, the first thing you want to do is review the notes from the IDE and Build lectures, because this is where we covered these issues. To reiterate in brief:

(1) .class files will be placed into the build/classes directory. With Ant, the build.xml is already set up to do that. In JBuilder, you need to set the "Output path" to the build/classes directory (creating it first if necessary).

(2) The working directory should be the top-level directory of the distribution. For example, on my system, it's set to 'c:/cvs/stack'.

> p.s.
> the last question is that i was just wondering if we zipped our files and
> whatever up, will our project run on other computers?  because we had to
> set up the file path and what not (class path, source path), and i think
> we all would have put the project in different folders like:
> 
> c:\Documents and Settings\Admin\Destop\accountinfo1\...
> 
> others might just store the project in c drive
> 
> c:\accountinfo1\...
> 
> will this make a difference when taken to another computer?
Well, it better not, otherwise this class is totally worthless! The whole point of the approach I'm teaching is to enable distributed development, not only among people running windows using different base directories, but also people on completely different operating systems such as Unix or Mac OS/X.

Recall that there are two ways to compile your system: using JBuilder, and using Ant. I am hopeful that you don't see a problem with compilation in other environments using Ant. What you're probably worried about is JBuilder, since the Project Properties window specifies what appears to be absolute pathnames. For example, as I indicated above, my working directory is 'c:\cvs\stack'. Clearly, that's not a valid Unix pathname, so how, for example, could someone possibly load my JBuilder.jpx file on a unix box? Much less load it on another windows box with a different base directory, as you question above?

The answer is that although the paths are displayed as absolute paths, internally they are stored as relative paths in the stack.jpx file. This might be a good time for everyone to actually open up that file (use Notepad or something other than JBuilder for this.) What you'll find is an XML file containing data like the following:

  <node name="stack" type="Folder">
    <file path="../../src/edu/hawaii/stack/Assert.java"/>
    <file path="../../src/edu/hawaii/stack/Debug.java"/>
    <file path="../../src/edu/hawaii/stack/EmptyStackException.java"/>
    <file path="../../src/edu/hawaii/stack/overview.html"/>
    <file path="../../src/edu/hawaii/stack/package.html"/>
    <file path="../../src/edu/hawaii/stack/Stack.java"/>
    <file path="../../src/edu/hawaii/stack/TestStack.java"/>
  </node>
 
What you see is that JBuilder stores paths internally as relative to the location of the .jpx file. In the stack template, you have to cd out of the lib/JBuilder directory and then down into the src directory. Thus, no matter where you unzip the package, once you open up the .jpx file, JBuilder will be able to find the other files. As a side note, it is amusing to see that JBuilder internally stores file names using Unix-style pathnames!

Cheers, Dr. Hacker