Saturday, April 26, 2008

Modula-2

I tried a little Modula-2 programming today. I used XDS 2.5. I built the following little program successfully in XDS IDE.

MODULE hello;
FROM InOut IMPORT WriteString, WriteLn;
BEGIN
WriteString("Hello, world!");
WriteLn;
END hello.

And I could use xc to compile it. The following error showed up when I used xlink to link it.

XDS Link Version 2.6 Copyright (c) 1995-2001 Excelsior
Fatal error (13): No program entry point


I have googled answers for it and read XDS documentation. Unfornately, I can't find an answer. Again, it shows a quick start guide with some simple examples is extremely important for a beginner to use some new tools.

After wresting with xlink for some time , I worked out the following solution.

xlink hello.obj C:\free\XDS\LIB\x86\libxds.lib C:\free\XDS\LIB\x86\import32.lib C:\free\XDS\LIB\x86\xstart.lib

Tuesday, April 15, 2008

Reading of Programming Language Pragmatics

Programming Language Syntax


The following syntax shows dangling else problem of Pascal.

  • stmt -> if condition then_clause else_clause | other_stmt

  • then_clause -> then stmt

  • else_clause -> else stmt | ε


Sometimes, we have a hard time to use some language. Sometimes it is not because we are not smart enough. It is because of the flaws of the programming language.

Saturday, April 12, 2008

Hiding and overriding in Java

For some technical terms in this post, please refer to JLS 3.0. And the discussion is not very precise. I have looked for some explicit rules governing this topic in JLS. But I failed. So I am just trying to summarize my personal understanding. Any feedbacks are welcomed.

In the following discussion, A and B are used. Type A extends type B (type means class or interface).

Field


Only hiding applies tojava fields. Hiding happens in the following situation.Both A and B have a variable named as var. And var is of the same kind of variables ( static fields or instance fields).
Java allows a variable belonging to different variable kinds appears in both A and B. But hiding does not happen in such situations.

Method


Method m1 in A . Method m2 in B.

  1. m1 is subsignature of m2

  2. m1's visibility is no less than m2

  3. Both m1 and m2 are instance methods. Or both m1 and m2 are static methods. Otherwise, there will some compile errors.


Overriding


Overriding applies to instance methods.

Hiding


Overriding applies to static methods.

Wednesday, April 9, 2008

blank in shell script

Try to avoid the use of blank in pathnames. It will drive you crazy under some circumstances. Take the following script as an example.

#!/bin/sh
opt="-path \"*/a bc\" -prune"
find $opt -o -print0
bash -c "find $opt -o -print0"

find $opt -o -print0 does not work. But bash -c "find $opt -o -print0" works. I have tried very hard to find a way to make the former work. But I failed. I will be happy to hear if anybody has a solution.

Little hacking with bash shell

I have recently tried locate from findutils in cygwin. I wanted to use updatedb to create a file name database. And I wanted to exclude all the files and directories under .svn directories . So I tried updatedb --localpaths='D:/gnu/ws/ctre/space' --findoptions='-path "*/.svn" -prune -o'. But I could't exclude the files in .svn directories with this command. I am not very experienced with bash. But I wanted to solve this problem. So I checked update script and did some debugging to it. The problem is that bash shell will do pathname expansion to "*/.svn". My first fix is to add set -f in updatedb script. It works.

Then I wanted to open a bug for this script. For opening a bug, I need to do further investigation. So I learned more about bash and try more scripts. Finally, I found that I can use updatedb --localpaths='D:/gnu/ws/ctre/space' --findoptions='-path */.svn -prune -o' or updatedb --localpaths='D:/gnu/ws/ctre/space' --findoptions='-path '*/.svn' -prune -o'. Bash shell will preserve the literal value of every chacter within the qoutes. But bash shell will do expansion to characters enclosed by double quotes.

As a result, I did not open an invalid bug. I have learned more. It tells me that contributing to community will let me learn more.

Sometimes I was frustrated during this hacking. But the overall experience is good.

Saturday, April 5, 2008

Solution to one Design Puzzle in Head First Design Pattern

I am reading Head First Design Pattern. It is a great book.

Generally, I don't like exercises in a book which don't have answers provided. I have run into the design puzzle on page 468. At first sight, I knew that I should use state pattern. I have never applied state pattern in real software work before. So I took this exercise as an opportunity to practice state pattern. Here is my solution.

State.java

package headfirst.proxy.virtualproxy;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public interface State {
public int getIconWidth();
public int getIconHeight();
public void paintIcon( Component c, Graphics g, int x, int y );
}

NullState.java

package headfirst.proxy.virtualproxy;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NullState implements State {

private ImageProxy proxy;
private Thread retrievalThread;
private URL imageURL;
boolean retrieving = false;

public NullState( ImageProxy p, URL url) {
proxy = p;
imageURL = url;
}

public int getIconWidth() {
return 800;
}

public int getIconHeight() {
return 600;
}

public void paintIcon( final Component c, Graphics g, int x, int y ) {
g.drawString("Loading CD cover, please wait...", x+300, y+190);
if (!retrieving) {
retrieving = true;
retrievalThread = new Thread(new Runnable() {
public void run() {
try {
ImageIcon imageIcon = new ImageIcon(imageURL, "CD Cover");
proxy.setState( new ImageState( imageIcon ) );
c.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
});
retrievalThread.start();
}
}
}

ImageState.java

package headfirst.proxy.virtualproxy;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageState implements State {

private ImageIcon imageIcon;

public ImageState( ImageIcon i ) {
imageIcon = i;
}

public int getIconWidth() {
return imageIcon.getIconWidth();
}

public int getIconHeight() {
return imageIcon.getIconHeight();
}

public void paintIcon( final Component c, Graphics g, int x, int y ) {
imageIcon.paintIcon(c, g, x, y);
}
}

ImageProxy.java

package headfirst.proxy.virtualproxy;

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageProxy implements Icon {

private NullState ns;
private State state;

public ImageProxy(URL url) {
ns = new NullState( this, url );
state = ns;
}

public int getIconWidth() {
return state.getIconWidth();
}

public int getIconHeight() {
return state.getIconWidth();
}

public void paintIcon(final Component c, Graphics g, int x, int y) {
state.paintIcon( c, g, x, y);
}

public void setState( State s )
{
state = s;
}
}

ClassNotFoundException problem with rmiregistry

It is very possible that you will get ClassNotFoundException when you try Getting Started tutorial for RMI in JDK documentation. There is a easy way to fix it. Start rmiregistry and the server implementation in the same command line console on Windows (assuming that the current directory is the root directory of the class file tree for the example).
  • start rmiregistry

  • java example.hello.Server

The CLASSPATHs for both rmiregistry and java example.hello.Server must contains the path for the class file tree for the example if you want to run them from different command line console.

I have run into this problem twice. And JDK documentation does not explicitly specified. I searched the web. Remote Method Invocation (RMI) - RMI server ClassNotFoundExceptionsolves my problem.

So documentation is vital to software quality. Good documentation can save developers a lot of precious time.