Pascal's Weblog
The Grid...



Archives
« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
Click me to subscribe
Search

Links
 

Today's Page Hits: 9

Monday May 19, 2008
Command line parameters
Here is the shortest example I could come up with to enter parameters on the command line (reader classes will be treated as just "it works this way" for now):

import java.io.*;

public class ReadString {

   public static void main (String[] args) throws Exception  {

      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String name = br.readLine();

      System.out.println("Thanks: " + name);

   }

}
Posted at 11:46AM May 19, 2008 by Pascal Ledru in Java  |  Comments[1]

Layout management, how simple can it be?
Trying to teach Java to a 14 year old, and after working on some problems such as: Solving a quadratric equation, checking if a number is prime, we have so far changed the data inside the program, recompiled it and ran again....

We are reaching the point where we want to be able to enter the data in a fashionable way! Meaning time to address the "GUI" problem. While it is straightforward to create a frame and add components to it, it is tricky to layout the components inside the frame.

Here are some examples using the layout manager as little as possible (I am keeping this subject for way later...).

In the first example, we simply add some components to the panel used by the frame (FlowLayout is used by default)

In the second example, we use the GridLayout class to order the components.

In the third example, the components in a row are first added to a panel which is then added to the overall panel.
import javax.swing.*;

public class Test {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();

    JLabel l = new JLabel("label:");
    JTextField f = new JTextField(10);
    JButton b = new JButton("Calc");

    panel.add(l);
    panel.add(f);
    panel.add(b);

    frame.getContentPane().add(panel);

    frame.setSize(300, 300);
    frame.setVisible(true);

  }

}



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

public class Test2 {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4,2));

    JLabel l1 = new JLabel("label:");
    JTextField f1 = new JTextField(10);
    panel.add(l1);
    panel.add(f1);

    JLabel l2 = new JLabel("label:");
    JTextField f2 = new JTextField(10);
    panel.add(l2);
    panel.add(f2);

    JLabel l3 = new JLabel("label:");
    JTextField f3 = new JTextField(10);
    panel.add(l3);
    panel.add(f3);

    JButton b = new JButton("Calc");
    panel.add(b);

    frame.getContentPane().add(panel);

    frame.setSize(300, 300);
    frame.setVisible(true);

  }

}




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

public class Test3 {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4,1));

    JLabel l1 = new JLabel("label:");
    JTextField f1 = new JTextField(10);
    JPanel panel1 = new JPanel();
    panel1.add(l1);
    panel1.add(f1);
    panel.add(panel1);

    JLabel l2 = new JLabel("label:");
    JTextField f2 = new JTextField(10);
    JPanel panel2 = new JPanel();
    panel2.add(l2);
    panel2.add(f2);
    panel.add(panel2);

    JLabel l3 = new JLabel("label:");
    JTextField f3 = new JTextField(10);
    JPanel panel3 = new JPanel();
    panel3.add(l3);
    panel3.add(f3);
    panel.add(panel3);

    JButton b = new JButton("Calc");
    JPanel panel4 = new JPanel();
    panel4.add(b);
    panel.add(panel4);

    frame.getContentPane().add(panel);

    frame.setSize(300, 300);
    frame.setVisible(true);

  }

}


Posted at 11:17AM May 19, 2008 by Pascal Ledru in Java  |  Comments[2]

Wednesday Jun 20, 2007
CS 101
I have been looking recently for resources to start teaching my son who is in middle school some introduction to computer programming.

I am not too sure where to start? Should I look into some intro to computer architecture first, some Operating Systems concept...

My current thinking is to focus on programs, what is a computer program and how to write one while introducing the above concepts in parallel. I will probably use Java as I think it is much easier to learn than C for example (Yes, I know of all these programmers who said how hard it is to switch to Java and OO concepts!).

Here are some web sites I found and plan to use:
Posted at 09:47AM Jun 20, 2007 by Pascal Ledru in Java  |  Comments[2]

Friday Dec 15, 2006
And the solution...
A quick analysis of the code is:
Should be straightforward? but the program actually blocks.
Taking a closer look, here is what is going on:

The moral of this story is that as everyone knows it is way too easy to make this sort of mistakes. To make concurrent programming little bit easier, Java 1.5 introduced the java.util.concurrent package and several classes to ease the development of multi-threads applications. Looking back at our problem, we want to ensure the run method is executed first, then the get method. A possible solution (I just started learning about these new classes!) is to use the new CountDownLatch class:

Also, since we are not using the wait and notifyAll calls directly any longer it is not necessary to synchronized the run and get methods. The implementation of the class becomes:
class CRun1 implements Runnable {

  String s;
  CountDownLatch c = new CountDownLatch(1);

  CRun1(String s) {
    this.s = s;
  }

  public void run() {
    System.out.println("Lrun " + s);
    try {
      Thread.sleep(4000);
    } catch (InterruptedException ex) {
    } finally {
    }
    System.out.println("count down - notify for run " + s);
    c.countDown();
  }

  public void get() {
    while (true) {
      try {
        c.await();
        break;
      } catch (InterruptedException ex) {
      }
    }
    System.out.println("recv notify...");
  }
}
Posted at 09:52AM Dec 15, 2006 by Pascal Ledru in Java  |  Comments[0]

A Thread issue...
What's wrong with this program? It did not run as expected, JConsole did not detect a deadlock, and FindBugs did not detect a problem (it did report not to use sleep with a lock held)

class Run1 implements Runnable {
  String s;
  boolean wait = true;
  Run1(String s) {
    this.s = s;
  }
  public synchronized void run() {
    System.out.println("run " + s);
    try {
      Thread.sleep(4000);
    } catch (InterruptedException ex) {
    }
    wait = false;
    System.out.println("notify for run " + s);
    notifyAll();
  }
  public synchronized void get() {
    while (true) {
      try {
        System.out.println("wait...");
        wait();
        if (!(wait)) break;
        System.out.println("recv notify...");
      } catch (InterruptedException ex) {
      }
    }
  }
}


public class TestThread {

  public synchronized void testIt() throws Exception {
    Run1 run1 = new Run1("1");
    Run1 run2 = new Run1("2");
    Thread t1 = new Thread(run1);
    t1.start();
    Thread t2 = new Thread(run2);
    t2.start();

    run1.get();
    run2.get();

  }

  public static void main(String[] args) throws Exception {
    TestThread t = new TestThread();
    t.testIt();
  }

}
Posted at 09:01AM Dec 15, 2006 by Pascal Ledru in Java  |  Comments[1]

Wednesday Dec 06, 2006
Remote and Serializable interfaces
Two interfaces are of utter importance to distributed programming in Java:

The Remote interface specifies that an object implementing this interface is an object in a different process (but of course it is technically possible to be in the same process), most likely on a different machine.
For example:
public interface ComputeServer extends Remote {
  public double integrate(String function, double min, double max) throws RemoteException;
}

Remote objects must implements this interface and extends the UnicastRemoteObject class:
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
  public double integrate(String function, double min, double max) throws RemoteException {
    ...
  }
}

All methods in a class implementing the Remote interface must throw the RemoteException (This exception will occurs if the network is down).
Notice the signature of the above method:
  public double integrate(String function, double min, double max) throws RemoteException {

Suppose now that you want to group the parameters to the function to integrate in an object:
class FunctionToIntegrate {
  String function;
  int min;
  int max;
}

and pass an object of this class such as:
  public double integrate(FunctionToIntegrate function);

to identicate and marshall the object over the wire (e.g, IP) it is necessary to have the object implement the Serializable interface:
class FunctionToIntegrate extends Serializable {
  ...
}

So, while you will typically pass a Serializable object in a remote call, is this possible to pass a Remote object? Yes! This is typically used when the server needs to call back the client such as:
public class ComputeServerImpl extends UnicastRemoteObject implements ComputeServer {
  public void integrate(String function, Remote callback) throws RemoteException {
    ...
  }
}

Suppose the computation takes a while. The above method returns right away, but calls back the client when the result is ready.

Posted at 12:01PM Dec 06, 2006 by Pascal Ledru in Java  |  Comments[0]