theclapp

Search theclapp.blog-city.com

 

Chapter 2, exercises 1-4

posted Monday, 6-Jan-2003
13:31

Got Eckel's HelloDate.java program to compile and run. WooHoo. The usual initial annoyances getting CLASSPATH set correctly for my jdk.

13:43

Chapter 2, exercise 1: create hello world:

---
// HelloWorld.java

import java.util.*;

public class HelloWorld {
public static void main( String[] args ) {
System.out.println( "Hello, world" );
}
}
---

13:47

Lisp

---
;;;; chapter 2, ex 1
(defun hello-world ()
(format t "Hello, world~%"))
---

So far, so good (though kinda silly ... :)

14:24

Spent a few minutes getting zsh to tab-expand only *.java for invocations of javac, and only *.class for invocations of java, but don't include the ".class" part.


compctl -g '*.java' javac
compctl -g '*.class(:r)' java

14:32

Chapter 2, Exercise 2:

Java:

---
// ATypeName.java
// chapter 2, ex 2

class ATypeName {
public static void main( String[] args ) {
ATypeName a = new ATypeName();
}
}
---

14:38

Lisp:


---
;;;; chapter 2, ex 2
(defclass a-type-name ())
(defmethod a-type-name.main ()
(let ((a (make-instance 'a-type-name)))))
---

Sort of a weird piece of code, but only about as contrived as the Java version, I think. Perhaps later, when I learn more of both languages, I can clean it up.

CMUCL warns "Note: Variable A defined but never used."

14:50

Chapter 2, ex 3

Java

---
// DataOnly.java
// chapter 2, exercise 3

class DataOnly {
int i;
float f;
boolean b;

public static void main( String[] args ) {
DataOnly d = new DataOnly();
d.i = 47;
d.f = 1.1f;
d.b = false;
}
}
---

Interestingly, at first I mispelled the "new DataOnly" line as "new dataOnly", and my javac hung (I aborted after 4 minutes). Oops.

14:59

Lisp

---
;;;; chapter 2, ex 3
(defclass data-only ()
((i :type integer :accessor data-only-i)
(f :type float :accessor data-only-f)
(b :type boolean :accessor data-only-b)))
(defmethod data-only.main ()
(let ((d (make-instance 'data-only)))
(setf (data-only-i d) 47)
(setf (data-only-f d) 1.1)
(setf (data-only-b d) nil)))
---

Arguably I should use "fixnum" instead of "integer" here, but I'll ignore that issue for now.

The Lisp version appears a bit more verbose than the Java version; I suppose one pays that price for the flexability of typed objects instead of typed variable names. I've read (in comp.lang.lisp) that one could create reader macros to transform the text "d.i" into "(data-only-i d)", but I don't know how to do that, yet. :) For now, with Vim's keyword completion, it doesn't take that much longer to type than the Java.

15:29

Fiddled some more with zsh tab-completion so vi doesn't expand any *.class files:


compctl -g '^*.class' vi gvim

15:38

Chapter 2, Exercise 4

Java

---
// DataOnly_2.java
// chapter 2, exercise 4

import java.util.*;

class DataOnly_2 {
int i;
float f;
boolean b;

public static void main( String[] args ) {
DataOnly d = new DataOnly();
d.i = 47;
d.f = 1.1f;
d.b = false;
System.out.println("d.i is " + d.i);
System.out.println("d.f is " + d.f);
System.out.println("d.b is " + d.b);
}
}
---

15:48

Lisp:

---
;;;; chapter 2, ex 4
(defclass data-only-2 ()
((i :type integer :accessor data-only-2-i)
(f :type float :accessor data-only-2-f)
(b :type boolean :accessor data-only-2-b)))
(defmethod data-only-2.main ()
(let ((d (make-instance 'data-only-2)))
(setf (data-only-2-i d) 47)
(setf (data-only-2-f d) 1.1)
(setf (data-only-2-b d) nil)
(format t "d.i is ~d~%" (data-only-2-i d))
(format t "d.f is ~g~%" (data-only-2-f d))
(format t "d.b is ~a~%" (data-only-2-b d))))
---

I had to poke around in the CLHS for the proper FORMAT directives. I guess I'll remember 'em eventually. :)

15:51

Reading c.l.l for a while.

links: digg this    del.icio.us    technorati    reddit