example: add clock.nit and clock_mode.nit
authorJean Privat <jean@pryen.org>
Wed, 16 Feb 2011 18:22:25 +0000 (13:22 -0500)
committerJean Privat <jean@pryen.org>
Thu, 17 Feb 2011 16:11:18 +0000 (11:11 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

examples/clock.nit [new file with mode: 0644]
examples/clock_more.nit [new file with mode: 0644]
tests/sav/clock.sav [new file with mode: 0644]
tests/sav/clock_more.sav [new file with mode: 0644]

diff --git a/examples/clock.nit b/examples/clock.nit
new file mode 100644 (file)
index 0000000..8fdb9ab
--- /dev/null
@@ -0,0 +1,78 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This module provide a simple wall clock.
+# It is an example of getters and setters.
+# A beefed-up module is available in clock_more
+module clock
+
+# A simple wall clock with 60 minutes and 12 hours.
+class Clock
+       # total number of minutes from 0 to 719
+       var total_minutes: Int
+       # Note: only the read acces is public, the write access is private.
+
+       # number of minutes in the current hour (from 0 to 59)
+       fun minutes: Int do return self.total_minutes % 60
+       
+       # set the number of minutes in the current hour.
+       # if m < 0 or m >= 60, the hour will be changed accordinlgy
+       fun minutes=(m: Int) do self.total_minutes = self.hours * 60 + m
+
+       # number of hours (from 0 to 11)
+       fun hours: Int do return self.total_minutes / 60
+
+       # set the number of hours
+       # the minutes will not be updated
+       fun hours=(h: Int) do self.total_minutes = h * 60 + minutes
+
+       # the position of the hour arrow in the [0..60[ interval
+       fun hour_pos: Int do return total_minutes / 12
+
+       # replace the arrow of hours (from 0 to 59).
+       # the hours and the minutes will be updated.
+       fun hour_pos=(h: Int) do self.total_minutes = h * 12
+
+       redef fun to_s do return "{hours}:{minutes}"
+
+       fun reset(hours, minutes: Int) do self.total_minutes = hours*60 + minutes
+
+       init(hours, minutes: Int) do self.reset(hours, minutes)
+
+       redef fun ==(o)
+       do
+               # Note: o is a nullable Object, a type test is required
+               # Thanks to adaptive typing, there is no downcast
+               # i.e. the code is safe!
+               return o isa Clock and self.total_minutes == o.total_minutes
+       end
+end
+
+var c = new Clock(10,50)
+print "It's {c} o'clock."
+
+c.minutes += 22
+print "Now it's {c} o'clock."
+
+print "The short arrow in on the {c.hour_pos/5} and the long arrow in on the {c.minutes/5}."
+
+c.hours -= 2
+print "Now it's {c} o'clock."
+
+var c2 = new Clock(9, 11)
+print "It's {c2} on the second clock."
+print "The two clocks are synchronized: {c == c2}."
+c2.minutes += 1
+print "It's now {c2} on the second clock."
+print "The two clocks are synchronized: {c == c2}."
diff --git a/examples/clock_more.nit b/examples/clock_more.nit
new file mode 100644 (file)
index 0000000..d2ef89e
--- /dev/null
@@ -0,0 +1,60 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This module beef up the clock module by allowing a clock to be comparable.
+# It show the usage of class refinement
+module clock_more
+
+import clock
+
+redef class Clock
+       # Clock are now comparable
+       super Comparable
+
+       # Comparaison of a clock make only sense with an other clock
+       redef type OTHER: Clock
+
+       redef fun <(o)
+       do
+               # Note: < is the only abstract method of Comparable.
+               #       All other operators and methods rely on < and ==.
+               return self.total_minutes < o.total_minutes
+       end
+end
+
+var c1 = new Clock(8, 12)
+var c2 = new Clock(8, 13)
+var c3 = new Clock(9, 13)
+
+print "{c1}<{c2}? {c1<c2}"
+print "{c1}<={c2}? {c1<=c2}"
+print "{c1}>{c2}? {c1>c2}"
+print "{c1}>={c2}? {c1>=c2}"
+print "{c1}<=>{c2}? {c1<=>c2}"
+print "{c1},{c2}? max={c1.max(c2)} min={c1.min(c2)}"
+print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
+print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"
+
+print "-"
+
+c1.minutes += 1
+
+print "{c1}<{c2}? {c1<c2}"
+print "{c1}<={c2}? {c1<=c2}"
+print "{c1}>{c2}? {c1>c2}"
+print "{c1}>={c2}? {c1>=c2}"
+print "{c1}<=>{c2}? {c1<=>c2}"
+print "{c1},{c2}? max={c1.max(c2)} min={c1.min(c2)}"
+print "{c1}.is_between({c2}, {c3})? {c1.is_between(c2, c3)}"
+print "{c2}.is_between({c1}, {c3})? {c2.is_between(c1, c3)}"
diff --git a/tests/sav/clock.sav b/tests/sav/clock.sav
new file mode 100644 (file)
index 0000000..8988504
--- /dev/null
@@ -0,0 +1,8 @@
+It's 10:50 o'clock.
+Now it's 11:12 o'clock.
+The short arrow in on the 11 and the long arrow in on the 2.
+Now it's 9:12 o'clock.
+It's 9:11 on the second clock.
+The two clocks are synchronized: false.
+It's now 9:12 on the second clock.
+The two clocks are synchronized: true.
diff --git a/tests/sav/clock_more.sav b/tests/sav/clock_more.sav
new file mode 100644 (file)
index 0000000..36a33a4
--- /dev/null
@@ -0,0 +1,17 @@
+8:12<8:13? true
+8:12<=8:13? true
+8:12>8:13? false
+8:12>=8:13? false
+8:12<=>8:13? -1
+8:12,8:13? max=8:13 min=8:12
+8:12.is_between(8:13, 9:13)? false
+8:13.is_between(8:12, 9:13)? true
+-
+8:13<8:13? false
+8:13<=8:13? true
+8:13>8:13? false
+8:13>=8:13? true
+8:13<=>8:13? 0
+8:13,8:13? max=8:13 min=8:13
+8:13.is_between(8:13, 9:13)? true
+8:13.is_between(8:13, 9:13)? true