June 8, 2008

Chapter: Operators

Exercise 5: (2) Create a class called Dog containing two Strings: name and says. In main( ), create two dog objects with names “spot” (who says, “Ruff!”) and “scruffy” (who says, “Wurf!”). Then display their names and what they say.


In Java:
public class Dog {

String name, says;

public Dog(String name, String says) {
this.name = name;
this.says = says;
}

public static void main(String[] args) {
Dog spot = new Dog("spot", "Ruff!");
Dog scruffy = new Dog("scruffy", "Wurf!");
System.out.println(spot.name + ": " + spot.says);
System.out.println(scruffy.name + ": " + scruffy.says);
}

}


In Python:
class Dog:
def __init__(self, name, says):
self.name = name
self.says = says

spot = Dog("spot", "Ruff!")
scruffy = Dog("scruffy", "Wurf!")
print(spot.name, spot.says)
print(scruffy.name, scruffy.says)



In Lisp:
(defclass Dog ()
((name :accessor name :initarg :name)
(says :accessor says :initarg :says)))

(setf spot (make-instance 'Dog :name "spot" :says "Ruff!"))
(setf scruffy (make-instance 'Dog :name "scruffy" :says "Wurf!"))
(print (list (name spot)
(says spot)
(name scruffy)
(says scruffy)))

I have a website that uses the Tomcat application server. During development, one of the best practices is to setup an Ant build script to increase productivity. I've searched online for example scripts, and one of the best is the script from the Spring Framework's tutorial, located about a quarter down the page.

May 31, 2008

Structure and Interpretation of Computer Programs is a classic, must read computer science book. It focuses on the core ideas of computer science that can be applied to all languages. Like my Thinking in Java posts, I am going to use this blog to keep track of my progress as I read through the book again. SICP uses Scheme to illustrate the ideas, but I will mostly use Common Lisp, and maybe even a little Python just to show the differences.

May 30, 2008

Chapter: Everything is an Object

Exercise 4: (1) Turn the DataOnly code fragments into a program that compiles and runs.


Again, not completely sure what they're after, but here is one that works:
public class Excercise4 {
public static void main(String[] args) {
DataOnly data = new DataOnly();

data.i = 20;
data.d = 1.23;
data.b = false;

System.out.println(data.i);
System.out.println(data.d);
System.out.println(data.b);
}

}

class DataOnly {
int i;
double d;
boolean b;
}


In Lisp:
(defclass data-only ()
((i :accessor the-int :initform 20)
(d :accessor the-double :initform 1.23)
(b :accessor the-bool :initform nil)))

(setf data (make-instance 'data-only))
(print (list (the-int data)
(the-double data)
(the-bool data)))


In Python:
class DataOnly:
i = 20
d = 1.23
b = False

data = DataOnly()

print data.i
print data.d
print data.b

Chapter: Everything is an Object

Exercise 3: (1) Find the code fragments involving ATypeName and turn them into a program that compiles and runs.


Not exactly sure what they're looking for here, but here's a simple program in Java:
public class Exercise3 {
public static void main(String[] args) {
ATypeName x = new ATypeName();
System.out.println(x.i);
}
}

class ATypeName {
int i = 5;
}

Chapter: Everything is an Object

Exercise 2: (1) Following the HelloDate.java example in this chapter, create a “hello, world” program that simply displays that statement. You need only a single method in your class (the “main” one that gets executed when the program starts). Remember to make it static and to include the argument list, even though you don’t use the argument list. Compile the program with javac and run it using java. If you are using a different development environment than the JDK, learn how to compile and run programs in that environment.


The classic Hello World program in Java:
public class Exercise2 {
public static void main(String[] args) {
System.out.println("Hello Blog Readers!");
}
}


In Lisp:
(print "Hello Blog Readers!")


In Python:
print "Hello Blog Readers!"

May 29, 2008

Chapter: Everything is an Object

Exercise 1: (2) Create a class containing an int and a char that are not initialized, and print their values to verify that Java performs default initialization.


Java code:
public class Exercise1 {
static int i;
static char c;

public static void main(String[] args) {

System.out.println("Int: " + i);
System.out.println("Char:" + c);
}
}


Result:
Int: 0
Char:


Ints default to 0 and chars default to null. Note that you will get a complier error if you declare the variables within a method and try to use them without initializing them.