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

0 comments: