theclapp

Search theclapp.blog-city.com

 

Chapter 3, exercise 3

posted Wednesday, 8-Jan-2003
09:25 -- 3.3, starting Java version
09:34 -- done
---
// ch03_ex03.java
// The exercise says "From the sections labeled "if-else" and "return", put
// the methods test() and test2() into a working program." Except the
// "return" section only mentions a method named test(). So I've renamed it,
// since it obviously can't keep its original name.

public class ch03_ex03 {
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = +1;
else if(testval < target)
result = -1;
else
result = 0; // Match
return result;
}

static int test2(int testval, int target) {
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}

public static void main( String[] args ) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));

System.out.println(test2(10, 5));
System.out.println(test2(5, 10));
System.out.println(test2(5, 5));
}
}
---

10:05 -- starting 3.3, Lisp version
10:08 -- finished

---
;;;; chapter 3, ex 3
(defun test (testval target)
(let (result)
(cond ((> testval target) (setf result 1))
((< testval target) (setf result -1))
(t (setf result 0)))
result))
(defun test2 (testval target)
(cond ((> testval target) 1)
((< testval target) -1)
(t 0)))
(test 10 5)
(test 5 10)
(test 5 5)
(test2 10 5)
(test2 5 10)
(test2 5 5)
---

links: digg this    del.icio.us    technorati    reddit