tag:blogger.com,1999:blog-92100489379448490602024-03-12T18:46:12.086-07:00Larry Watanabe's Bloglarrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.comBlogger160125tag:blogger.com,1999:blog-9210048937944849060.post-89760752997351327012012-03-05T06:48:00.001-08:002012-03-05T06:48:58.011-08:00Principles of ProgrammingPrinciples of Programming<br /><br />1. Model everything in the user domain and the programming domain as an object. It provides a useful paradigm for program organization, remembering what each part does, and conceptual chunking.<br />2. Always use constants and enumerations.<br />3. Create unit tests to guide development.<br />4. Create print method for each class.<br />5. Create standard exception handling method.<br />a. Easy at highest level to capture all exceptions.<br />b. Exceptions themselves should contain information on how to handle themselves.<br />c. Handlers can decide how to handle exception, i.e. output to stdout, logfile, abort, or html error message.<br />6. Document (almost) every class and method – except for functions whose description can be determined by convention, i.e. getter/setter.<br />7. Document even functions that seem redundant with their name, just to show that they actually do what their name implies.<br />8. Learn how to use the source level debugger and the IDE and other programming tools.<br />9. Keep a log of what you are doing – hardcopy, in a notebook.<br />10. Make system with minimal features for each stage of development.<br />11. Make system extendible for future stages of development.<br />12. Write install document as you do development, so that you remember to write down what is needed to get going in the environment. Later when you are expert you may make too many assumptions.<br />13. No global variables for the program. Some may be used for testing or runtime environment, to save typing.<br />14. If you need global variables, you really need an “application” class. Imagine that you have 2 instances of the application running in another module – will it work?<br />15. Let the app class know about everything (i.e. root of a tree of information that encompasses the program) and let everything know about the app tree (directly or indirectly, through it’s parents/ancestors).<br />16. (to be continued)larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-51159429665866912192011-07-22T08:52:00.000-07:002011-07-22T08:53:55.680-07:00Why Process?I'm beginning to understand why software companies have process. It isn't because you have bad programmers on your team. Usually you have a few great ones, a lot of good ones, some average ones, and the occassional bad one. But any one of them can mess up the system on any given day, and it isn't necessarily the bad programmers who are the worst offenders -- sometimes it's the great ones.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-12487800544380544132011-04-22T10:16:00.000-07:002011-04-22T11:19:04.250-07:00Netbeans APINetbeans API<br />- <a href="http://platform.netbeans.org/tutorials/nbm-runtime-container.html">tutorial </a>- learning trail<br />- tried first example, didn't run until added more libraries<br />- do not see the System.out.println resultlarrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-82347955015522264962011-04-19T19:01:00.000-07:002011-04-19T19:02:41.016-07:00SVN Importhttp://svnbook.red-bean.com/en/1.4/svn.tour.importing.html<br /><br /><br />svnadmin create /usr/local/svn/newrepos<br />$ svn import mytree file:///usr/local/svn/newrepos/some/project \<br /> -m "Initial import"<br />Adding mytree/foo.c<br />Adding mytree/bar.c<br />Adding mytree/subdir<br />Adding mytree/subdir/quux.h<br /><br />Committed revision 1.<br /><br />---------------<br /><br />So much better than the headache of figuring out CVS Root syntaxlarrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-40035719357707537152011-04-18T15:34:00.001-07:002011-04-18T15:34:59.486-07:00Hibernate NotesHibernate Notes<br />1. Develop a system that displays, allows add, delete, and modifications<br />New Project<br />2. Web Application, <br />3. select Hibernate and Java Faces frameworks, <br />4. choose correct database<br />hibernate configuration<br />1. open hibernate.cfg.xml<br />2. add, <br />a. driver_class, <br />b. url, jdbc:mysql://localhost:3306/databaseName<br />c. username, <br />d. password if not there<br />3. Optional Properties<br />a. Configuration properties<br />i. Show_sql – true<br />4. Miscellaneous properties<br />a. Current_session_context_class – thread<br />Package<br />Create filviewer package in Soruce Packages<br />HibernateUtil<br />i. Use File->Hibernate->New HIbernateUtil (or somewhere in there)<br />ii. Put in Source Packages in package filmviewer<br />Reverse Engineering<br />i. Go to default package<br />ii. Open up New file->Hibernate->Hibernate reverse engineering wizard<br />iii. Use src/java<br />iv. Add tables from database <br />Mapping POJO’s<br />i. Use File->Hibernate->Hibernate Mapping Files and POJO’s from Database<br />ii. Use <br />a. JDK5.1 language, <br />b. hibernate.cfg.xml , <br />c. hibernate.reveng.xml, <br />d. domain Code, <br />e. Hibernate XML Mappings<br />Session Bean<br />i. Use File->JavaServer Faces->JSF Managed Bean<br />ii. Use either Request Scoped or Session Scoped as appropriate.<br />iii. Edit:<br />a. Import Org.hibernate.Session, Query, Transaction, java.util.*<br />b. Cache HibernateUtil.getSessionFactory().getCurrentSession() in an instance variable<br />c. Helper Methods<br />private Session getSession() {<br /> return HibernateUtil.getSessionFactory().getCurrentSession();<br /> }<br /><br /> private Transaction beginTransaction() {<br /> return HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();<br /> }<br /><br /> private Transaction getTransaction() {<br /> return HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();<br /> }<br />View<br />public List<POJO> view() {<br /> try {<br /> beginTransaction();<br /> Query query getSession().createQuery("from Film");<br /> List< POJO > result = (List< POJO >) query.list();<br /> getTransaction().commit();<br /> return result;<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return null;<br />}<br /> Add<br />public void add(String title, String description) {<br /> try {<br /> beginTransaction();<br /> POJO film = new POJO ();<br /> film.setTitle(title);<br /> film.setDescription(description);<br /> getSession().save(film);<br /> getTransaction().commit();<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br />Delete<br />public void delete(int id) {<br /> try {<br /> beginTransaction();<br /> Film film = (Film)getSession().get(Film.class, id);<br /> getSession().delete(film);<br /> getTransaction().commit();<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br />Update<br /><br /> public void update(int id, String title, String description) {<br /> try {<br /> beginTransaction();<br /> Film film = (Film)m_session.get(Film.class, id);<br /> film.setTitle(title);<br /> film.setDescription(description);<br /> getSession().update(film);<br /> getTransaction().commit();<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /><br /> <br />Running Methods/Accessing View<br />a. Edit Web Pages/index.xhtml<br />b. Add xmlns:f=http://java.sun.com/jsf/core<br />c. <br /><h:body><br /><h:form><br /><h:dataTable id=" " value="#{session.property}" var="myVar"><br /> <h:column> <br /> <f:facet name=" "><br /> <h:outputText value=" "/><br /> </f:facet><br /> <h:outputText value="#{myVar.property}"/><br /></h:column><br /> </h:dataTable><br /><br /> <h:commandButton id="delete" value="Delete" action="#{session.delete(myVar.id)}"><br /> </h:form><br /></h:body><br />Debugging<br />1. Helpful to run server from console using glassfish\bin\startserv so you can see stacktraces in terminal window.<br />Database<br />Example of how to create a table with autoincremented primary key<br />CREATE TABLE film ( <br /> filmId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, <br /> title VARCHAR(255) , <br />description VARCHAR(1000) <br />);larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-77189000341810961162011-04-18T07:09:00.001-07:002011-04-18T07:10:00.916-07:00What to plan before starting development on a project?[someone asked this on Programmers.stackexchange.com - my response]<br /><br />Most important thing to do: review the specs, interact with customer to get more refined specs.<br /><br />The requirements are undoubtedly incomplete, vague, or incorrect. The biggest waste of time is doing the wrong thing. Customers are not professional software engineeers, and cannot be expected to be good at developing a good set of requirements.<br /><br />So, you should review the specs, interview the customer and find out if this is what he/she really needs and wants, and can afford, etc.<br /><br />Develop test/use cases and review with customer. If a requirement isn't testable, throw it out.<br /><br />Develop the design and make sure if all the pieces function correctly that it would in theory do what you need.<br /><br />Develop an architecture prototype that tests all of the technology to be used in every layer but ignore functionality. You are testing the architecture, not the functional specification. Having the wrong architecture will mean you have to rewrite everything, so getting the right architecture is important. Make sure it can meet your requiremenets for speed, efficiency, security, etc.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-22766173674613017912011-04-12T09:11:00.001-07:002011-04-12T09:11:48.240-07:00How to delete a windows service1. Run Regedit or Regedt32<br /><br />2. Find the registry entry "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services"<br /><br />3. Look for the service there and delete it. You can look at the keys to know what files the service was using and delete them as well (if necessary).larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-77747287536934044382011-04-09T20:15:00.001-07:002011-04-09T20:17:00.106-07:00Glassfish no free port available port value out of rangeYou may get this error when trying to start the Glassfish server. I believe this occurs because it's trying to use a port already in use.<br /><br />My glassfish was set up with default ports 8080, and I had a Default Web Site and Team Foundation server running using port 8080. When I shut these off, this error disppeared.<br /><br />When I have time I'll just change the ports.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com1tag:blogger.com,1999:blog-9210048937944849060.post-26139186669544392092011-04-07T21:19:00.001-07:002011-04-07T21:19:56.248-07:00Kamikaze JavaSun's Java initiative reminds me of the Kamikaze in WWII. A gutsy move against an overwhelmingly powerful enemy, but they had to know its close to the end when they're that desperate.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-63166754133672072432011-04-04T01:48:00.001-07:002011-04-04T01:48:50.283-07:00Zune - No installation MediaThis fix worked for me<br /><br />http://support.microsoft.com/kb/910336larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-61207448526446925412011-03-29T02:46:00.000-07:002011-03-29T02:47:00.262-07:00find Missing int - amazon questionstatic private void findMissing(int[] intArray, int n)<br /> {<br /> bool[] bitArray = new bool[n];<br /><br /> if (intArray == null)<br /> throw new Exception("findMissing - intArray is null");<br /><br /> for (int i = 0; i < n; i++)<br /> bitArray[i] = false;<br /><br /> foreach (int x in intArray)<br /> {<br /> if (x < 0 || x >= n)<br /> throw new Exception("findMissing - x is out of range - x=" + x);<br /> bitArray[x] = true;<br /> }<br /> for (int i = 0; i < n; i++)<br /> {<br /> //Console.WriteLine("i=" + i + " bit=" + bitArray[i].ToString());<br /> if (!bitArray[i])<br /> Console.WriteLine("Missing element at i=" + i);<br /> }<br /> }<br /><br /> static private void testFindMissing()<br /> {<br /> int[] intArray = new int[]{ 1, 2, 4, 7, 3, 5, 6, 0, 9, 10 };<br /> findMissing(intArray, 11);<br /> Console.ReadLine();<br /> }larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-2623908024566637342011-03-29T02:28:00.001-07:002011-03-29T02:28:28.414-07:00amazon question find min diff in a list of intstatic private void minDiff(int[] intArray)<br /> {<br /> if (intArray == null)<br /> throw new Exception("minDiff - intArray is null");<br /> <br /> var query = intArray.OrderBy(x1 => x1);<br /> List<int> intList = query.ToList<int>();<br /> if (intList.Count <= 1)<br /> throw new Exception("minDiff - must have at least 2 elements in list");<br /><br /> int minDiff = int.MaxValue;<br /> int x = -1;<br /> int y = -1;<br /> for (int i = 1; i < intList.Count - 1; i++)<br /> {<br /> if (intList[i] - intList[i - 1] < minDiff)<br /> {<br /> x = intList[i];<br /> y = intList[i - 1];<br /> minDiff = x - y;<br /> }<br /> }<br /> Console.WriteLine("x=" + x + " y=" + y + " minDiff=" + minDiff);<br /> Console.ReadLine();<br /> }<br /><br /> static private void testMinDiff()<br /> {<br /> int[] intArray = new int[] { 3, 9, 6, 17, 33, 1, 16 };<br /> minDiff(intArray);<br /> }larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-22910397163997415482011-03-23T20:42:00.000-07:002011-03-23T20:44:11.788-07:00Nesting Functions vs Intermediate VariablesUnless you really love looking at the call stack, create intermediate variables and un-nest your function. The compiler will compile away these variables anyways, so no loss in efficiency. They will be handy for debugging and make the code clearer.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-82765045093243630802011-03-22T07:21:00.000-07:002011-03-22T07:41:08.238-07:00Delete a Project from Team Foundation ServerAnswer: go to the Visual Studio 2010 command prompt and use “TfsDeleteProject.exe“. <br /><br />Example:<br /> <br /><br />TfsDeleteProject /collection:http://larry-pc:8080/tfs “Fret Project“<br /><br />You can get the url, if you've forgotten it, by going to <br />Team -> Connect to Team Foundation Servers -> Servers ..larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-8033739708940283632011-03-22T01:11:00.000-07:002011-03-22T07:40:59.141-07:00Forcing DataGridView to Refresh when changedThis works - save DataSource, save scroll position, restore datasource, restore scroll position, e.g.<br /><br /> private void forceRefresh()<br /> {<br /> int x = dgvMusicalScaleList.FirstDisplayedScrollingRowIndex;<br /> dgvMusicalScaleList.DataSource = null;<br /> dgvMusicalScaleList.DataSource = musicalScaleList;<br /> dgvMusicalScaleList.FirstDisplayedScrollingRowIndex = x;<br /> }larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-59352688597936051082011-03-21T23:29:00.000-07:002011-03-22T07:39:27.620-07:00You forgot to make it public, dummy!Microsoft has a convoluted way of saying this;<br /><br />Error 1 Inconsistent accessibility: property type 'System.Collections.Generic.List<Frets.Classes.GuitarStringNotes>' is less accessible than property 'Frets.MusicalScale.notes' J:\Users\Larry\Documents\Visual Studio 2008\Projects\Frets\Frets\Classes\MusicalScale.cs 44 41 Fretslarrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-28331592603469871082011-03-19T10:27:00.001-07:002011-03-19T10:39:29.565-07:002-valued boolean variableWhat do you name a variable that is a boolean but can have 2 meaningful values out of N?<br /><br />Example: x can have values "red" or "blue".<br /><br />Calling it bRed, or isRed, is meaningful if he value is true , but doesn't convey any info about the "false" case.<br /><br />YOu could implement it as an int, or if you are picky, as an enum. But this means more lines of code to declare the enum, etc.<br /><br />I thought of "redOrBlue", but logically speaking, the value is always true :) <br /><br />What do you think of "redNotBlue"? Any better ideas?larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-36690623081324486422011-03-19T03:58:00.001-07:002011-03-19T04:00:31.281-07:00DataGridView not Updating when DataSource modifiedI had this problem. I managed to solve it as follows:<br /><br /><blockquote>int x = dgvMetronomeSpeeds.FirstDisplayedScrollingRowIndex;<br />dgvMetronomeSpeeds.DataSource = null;<br />m_metronomeSpeeds.Add(new Integer(0));<br />dgvMetronomeSpeeds.DataSource = m_metronomeSpeeds;<br />dgvMetronomeSpeeds.FirstDisplayedScrollingRowIndex = x;</blockquote><br /><br />If you don't save and restore the FirstDisplayedScrollingRowIndex, then the user loses his/her scroll position.<br /><br />other keywords - c#, msdn, .NET, scrollinglarrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-40246750713671272792011-03-18T10:38:00.000-07:002011-03-18T10:43:50.985-07:00Never take notice of an error that you don't know how to handleThis is in general a good principle, but in specific cases it is a good idea to intercept it. The special case is when the error occurs at a very low level (i.e. a null pointer exception) and thus there is not enough context information to make sense of the error.<br /><br />When debugging, it isn't an issue because you have access to the call stack and can figure out the context from there. If, however, the error is generated for a user, and the user provides the information to support, then there won't be enough information to figure out the error, at least not easily.<br /><br />In this case (or if you are a developer and are anticipating this case happening in the future, where the user might be QA) it is often helpful to catch the error, add some context information, and rethrow the exception. Or, you could check for the error that might cause the exception, and throw an exceptioin yourself instead of calling the routine that might generate the exception.<br /><br />For example, usinng a variable <br /><br />Class1 c = complicatedCreationMethod(); <br /><br />....<br /><br />c.method1();<br /><br />Now, method1 will fail if c is null. If there is any possibility that complicatedCreationMethod() might return null, you may want to check for this and throw an exception rather than simply letting the null pointer exception percolate to the top of the error handlers.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-28735521826909568162011-03-16T09:23:00.001-07:002011-03-16T09:27:51.684-07:00Tracking down bugsThis is basic, but anyways this is how I do it.<br /><br />Put a breakpoint before the error occurs (your best guess). Step OVER code (not going into any code) until you run into the exception. Then, set the breakpoint at that procedure call, remove the previous breakpoint, step into that part, and repeat above until you find the bug.<br /><br />Eventually you end up with a single line of code where the error occurs, that has no function calls. Simplify the call by removing any nested procedure calls and serializing them. E.g. instead of f1(f2(f3)) do x = f3; y = f2(x); z = f1(y). This makes it easier to step and watch the variables.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-31389360526722335722011-03-14T09:11:00.000-07:002011-03-14T09:15:33.721-07:00Data Design or Procedure First?It's often a difficult decision whether to design the data representation or the algorithm first.<br /><br />In a big team, designing the data representation first means that all team members can work on their individual functional components that operate on the same data representation.<br /><br />However, the data representation may not be the right one for the algorithms, or not as simple or optimal as it could be. To determine the best data representation, you need to design the algorithm first.<br /><br />When programming individually, I find it is best to figure out the simplest data representation (even if hard-coded, no IO) required to develop the algorithm, get that working, then modify the data representation to fit additional requirements (i.e. persistence, modifiability, etc). There's no point designing a lot of functionality (i.e. communication, database, etc.) if you don't have a good representation.<br /><br />In a team, you can do this by prototyping critical algorithms first, then designing the data representation, then passing out functional component work.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-59110175832328392862011-03-14T00:24:00.000-07:002011-03-15T22:44:13.091-07:00Xml Serialization and Deserialization of Complex Objects/Listshttp://msdn.microsoft.com/en-us/library/ms950721.aspx<br /><br />what isn't really made clear here is that you have to pass into the XMLSerializer constructor all the types that will come in the nested types of the objects. you can do this e.g.<br /><br />public class Class1<br />{<br /> public static Type[] extraTypes = new Type[] { typeof(Class2), typeof(Class3) }<br /> <br /> private list1 List<Class2> = new List<Class2>();<br /> public List<Class2><br /> { <br /> get .. <br /> set ..<br /> }<br /> ... same for Class3 ..<br />}<br /><br />Then you need to create the xml serializer as XmlSerializer(typeof(Class1), Class1.extraTypes)larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-88418556319702792642011-03-11T11:06:00.001-08:002011-03-11T11:06:39.232-08:00Changing SoundPlayer volumehttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/42b46e40-4d4a-48f8-8681-9b0167cfe781larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-78967832905599402732011-02-23T12:25:00.000-08:002011-02-23T12:27:37.661-08:00CheckedListBox Mutual Exclusive SelectionI thought that this would be a simple property setting in the CheckedListBox, but no I guess not. I assume that the individual only wants to select one item, and that item should be both selected and checked.<br /><br />Here's how I did it:<br /><br /> private void checkOnlySelection(CheckedListBox cbox)<br /> {<br /> Object selectedItem = cbox.SelectedItem;<br /> if (selectedItem == null)<br /> return;<br /> for (int i = 0; i < cbox.Items.Count; i++)<br /> {<br /> Object o = cbox.Items[i];<br /> bool match = o == selectedItem;<br /> cbox.SetItemChecked(i, match);<br /> }<br /> }<br /><br />Just call this method in the SelectedIndexChange event handler and it will do the trick.larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0tag:blogger.com,1999:blog-9210048937944849060.post-74067213450409178062011-02-17T09:25:00.002-08:002011-02-17T09:31:44.580-08:00Career Aptitude Tests - Nature vs. NurtureIf they ever develop a really good test for career aptitudes, what correlations they would find? Gardeners <-> biologists, mechanics <-> computer programmers, waiters/tresses <-> actors/actresses? But every waiter is an aspiring actor so it would be hard to separate nurture vs. nature. Are they called waiters/resses because they're all waiting to become actors/tresses?larrywatanabehttp://www.blogger.com/profile/14381601653784413243noreply@blogger.com0