Saturday, November 13, 2010

Programming: COW Interpreter - A java interface for Bovine Programmers...

In one of my lectures at Swansea University the topic of Esoteric Languages came up, mainly on this topic we discussed the languages of BrainF*** and COW.

BrainF*** is a programming language that only contains the characters:
< > + - , . [ ]

COW only contains 12 commands, each written as MOO's:
moo
mOo
moO
mOO
Moo
MOo
MoO
MOO
OOO
MMM
OOM
oom
*These are of course all case sensitive*

Upon further research into these languages I came across this site: http://www.bigzaphod.org/cow/
And with it, a worrying thing arose to me. There were no stable compilers or interpreters for this great language.

Bare in mind ("rawr"), that at this point I had yet to even write a program in COW, but none the less I was enthused by how such a simple architecture could still have a modern day application, given that COW, and BrainF*** alike are based off of the old Tape Driven computer Systems, where a Command Tape drives the position and data stored in a Data Tape.


[ A 26 line COW program that counts indefinitely in Fibonacci Numbers, or until you reach ]
[ a numerical overflow. ]


[ A 1337 line COW program that runs through 99 Bottles of Beer on the wall. ]

Tuesday, November 2, 2010

Programming: Java: 1st Million Prime Numbers (Why the hell not?)

The program counts upward in prime number, given that the most time the program spends will be updating to the screen I forced it to only print out every thousandth number... Just so I'd know the program was still running, and to see how much it was slowing down as the calculation went on.

c:\Users\Joss\Documents\JCreator LE\MyEclipse\CS141_Assignment_01_Whittle\bin>ja
va Primes 1000000
1000 {7907}
2000 {17387}
3000 {27437}
4000 {37811}
5000 {48593}
6000 {59357}
7000 {70639}
8000 {81773}
9000 {93169}
10000 {104723}
11000 {116443}
12000 {128173}
13000 {139891}
14000 {151693}
15000 {163819}
16000 {176063}
17000 {187951}
18000 {200177}
19000 {212353}
20000 {224729}
21000 {237179}
22000 {249437}
23000 {262133}
24000 {274517}
25000 {287107}
26000 {300017}
27000 {312581}
28000 {324941}
29000 {337537}
30000 {350351}
31000 {363257}
32000 {376099}
33000 {389167}
34000 {401981}
35000 {414971}
36000 {427969}
37000 {440717}
38000 {453877}
39000 {467209}
40000 {479903}
41000 {493123}
42000 {506119}
43000 {519217}
44000 {532331}
45000 {545731}
46000 {559067}
47000 {572303}
48000 {585467}
49000 {598681}
50000 {611951}
51000 {625181}
52000 {638971}
53000 {652417}
54000 {665633}
55000 {679249}
56000 {692539}
57000 {706009}
58000 {719633}
59000 {732911}
60000 {746749}
61000 {760261}
62000 {773599}
63000 {787187}
64000 {800557}
65000 {814243}
66000 {827699}
67000 {841457}
68000 {855331}
69000 {868741}
70000 {882367}
71000 {896003}
72000 {909679}
73000 {923581}
74000 {937373}
75000 {951151}
76000 {965101}
77000 {978931}
78000 {993103}
79000 {1006711}
80000 {1020361}
81000 {1034219}
82000 {1048127}
83000 {1062497}
84000 {1076137}
85000 {1090333}
86000 {1103911}
87000 {1117553}
88000 {1131569}
89000 {1145659}
90000 {1159517}
91000 {1173283}
92000 {1186981}
93000 {1200943}
94000 {1215121}
95000 {1229257}
96000 {1243691}
97000 {1257499}
98000 {1271251}
99000 {1285513}
100000 {1299689}
101000 {1313827}
102000 {1327889}
103000 {1342049}
104000 {1356221}
105000 {1370449}
106000 {1384619}
107000 {1398349}
108000 {1412633}
109000 {1427021}
110000 {1441031}
111000 {1455079}
112000 {1469389}
113000 {1483439}
114000 {1497533}
115000 {1511533}
116000 {1525877}
117000 {1540481}
118000 {1554877}
119000 {1569163}
120000 {1583531}
121000 {1597979}
122000 {1612123}
123000 {1626467}
124000 {1640819}
125000 {1655123}
126000 {1669309}
127000 {1683667}
128000 {1698071}
129000 {1712339}
130000 {1726939}
131000 {1741387}
132000 {1755553}
133000 {1770427}
134000 {1784617}
135000 {1798963}
136000 {1813337}
137000 {1827647}
138000 {1842329}
139000 {1856293}
140000 {1870651}
141000 {1885183}
142000 {1899473}
143000 {1913969}
144000 {1928237}
145000 {1942729}
146000 {1957121}
147000 {1972169}
148000 {1986749}
149000 {2000959}
150000 {2015161}
151000 {2029711}
152000 {2044111}
153000 {2058869}
154000 {2073347}
155000 {2088131}
156000 {2102693}
157000 {2117119}
158000 {2131799}
159000 {2146219}
160000 {2160533}
161000 {2175449}
162000 {2190017}
163000 {2204821}
164000 {2219629}
165000 {2234119}
166000 {2248703}
167000 {2263369}
168000 {2277551}
169000 {2292463}
170000 {2307197}
171000 {2322137}
172000 {2336843}
173000 {2351803}
174000 {2366267}
175000 {2381143}
176000 {2395849}
177000 {2410613}
178000 {2425207}
179000 {2440213}
180000 {2454539}
181000 {2469409}
182000 {2484323}
183000 {2498989}
184000 {2513579}
185000 {2528399}
186000 {2543201}
187000 {2557999}
188000 {2572621}
189000 {2587001}
190000 {2601853}
191000 {2616701}
192000 {2631527}
193000 {2646079}
194000 {2660743}
195000 {2675879}
196000 {2690531}
197000 {2705203}
198000 {2719601}
199000 {2735119}
200000 {2750131}
201000 {2764871}
202000 {2779769}
203000 {2794717}
204000 {2809307}
205000 {2823559}
206000 {2838151}
207000 {2853167}
208000 {2868029}
209000 {2883367}
210000 {2898503}
211000 {2913257}
212000 {2928137}
213000 {2943247}
214000 {2958379}
215000 {2973053}
216000 {2987839}
217000 {3002737}
218000 {3017681}
219000 {3032801}
220000 {3047731}
221000 {3062963}
222000 {3077681}
223000 {3092363}
224000 {3107609}
225000 {3122309}
226000 {3137399}
227000 {3152339}
228000 {3167137}
229000 {3182321}
230000 {3196927}
231000 {3211433}
232000 {3226411}
233000 {3241159}
234000 {3256417}
235000 {3271271}
236000 {3286319}
237000 {3301579}
238000 {3316403}
239000 {3331409}
240000 {3346591}
241000 {3362143}
242000 {3376987}
243000 {3391819}
244000 {3406763}
245000 {3421711}
246000 {3436561}
247000 {3451211}
248000 {3466811}
249000 {3482293}
250000 {3497849}
251000 {3512851}
252000 {3528397}
253000 {3543781}
254000 {3558911}
255000 {3573839}
256000 {3588931}
257000 {3604099}
258000 {3618479}
259000 {3633337}
260000 {3648919}
261000 {3664343}
262000 {3678929}
263000 {3694199}
264000 {3709061}
265000 {3724213}
266000 {3738923}
267000 {3754033}
268000 {3769751}
269000 {3784927}
270000 {3800183}
271000 {3815197}
272000 {3829997}
273000 {3845263}
274000 {3860333}
275000 {3875803}
276000 {3890963}
277000 {3906307}
278000 {3921209}
279000 {3936293}
280000 {3951151}
281000 {3967027}
282000 {3981881}
283000 {3997849}
284000 {4012837}
285000 {4027909}
286000 {4042721}
287000 {4057939}
288000 {4073203}
289000 {4088221}
290000 {4103621}
291000 {4119043}
292000 {4134619}
293000 {4149881}
294000 {4164631}
295000 {4180093}
296000 {4195033}
297000 {4210247}
298000 {4225451}
299000 {4241093}
300000 {4256227}
301000 {4271569}
302000 {4287233}
303000 {4302601}
304000 {4317821}
305000 {4333081}
306000 {4348681}
307000 {4364093}
308000 {4379449}
309000 {4394773}
310000 {4410283}
311000 {4425079}
312000 {4440031}
313000 {4455029}
314000 {4470281}
315000 {4485469}
316000 {4500721}
317000 {4516229}
318000 {4531799}
319000 {4547237}
320000 {4562689}
321000 {4577887}
322000 {4593067}
323000 {4608473}
324000 {4623781}
325000 {4639259}
326000 {4654669}
327000 {4670203}
328000 {4685231}
329000 {4700419}
330000 {4716031}
331000 {4731241}
332000 {4746499}
333000 {4762073}
334000 {4777711}
335000 {4793233}
336000 {4808359}
337000 {4823809}
338000 {4838927}
339000 {4854617}
340000 {4869853}
341000 {4885333}
342000 {4900087}
343000 {4915549}
344000 {4930951}
345000 {4946209}
346000 {4961699}
347000 {4976897}
348000 {4991839}
349000 {5007589}
350000 {5023301}
351000 {5038769}
352000 {5054243}
353000 {5069641}
354000 {5084987}
355000 {5100541}
356000 {5116127}
357000 {5131517}
358000 {5146751}
359000 {5162383}
360000 {5178031}
361000 {5193743}
362000 {5208881}
363000 {5224447}
364000 {5239753}
365000 {5254831}
366000 {5270501}
367000 {5286329}
368000 {5301473}
369000 {5316959}
370000 {5332513}
371000 {5347787}
372000 {5363459}
373000 {5378911}
374000 {5394391}
375000 {5410109}
376000 {5425513}
377000 {5440873}
378000 {5456513}
379000 {5472023}
380000 {5487697}
381000 {5503129}
382000 {5518841}
383000 {5534671}
384000 {5550257}
385000 {5566027}
386000 {5581393}
387000 {5596907}
388000 {5612423}
389000 {5628251}
390000 {5644027}
391000 {5659729}
392000 {5675377}
393000 {5690929}
394000 {5706053}
395000 {5721479}
396000 {5737379}
397000 {5752711}
398000 {5768641}
399000 {5784421}
400000 {5800057}
401000 {5815517}
402000 {5831381}
403000 {5846933}
404000 {5862601}
405000 {5878121}
406000 {5893343}
407000 {5908733}
408000 {5923889}
409000 {5939369}
410000 {5955029}
411000 {5970799}
412000 {5986609}
413000 {6002369}
414000 {6017639}
415000 {6033091}
416000 {6049409}
417000 {6064879}
418000 {6080429}
419000 {6096269}
420000 {6111607}
421000 {6127399}
422000 {6142973}
423000 {6158501}
424000 {6174011}
425000 {6190003}
426000 {6205411}
427000 {6221539}
428000 {6237017}
429000 {6252877}
430000 {6268279}
431000 {6283573}
432000 {6298807}
433000 {6314687}
434000 {6329893}
435000 {6345749}
436000 {6361651}
437000 {6377753}
438000 {6393773}
439000 {6409267}
440000 {6424933}
441000 {6440999}
442000 {6456479}
443000 {6472261}
444000 {6488243}
445000 {6503501}
446000 {6519307}
447000 {6535093}
448000 {6550679}
449000 {6566359}
450000 {6581959}
451000 {6597551}
452000 {6612979}
453000 {6628679}
454000 {6644537}
455000 {6660707}
456000 {6676321}
457000 {6692269}
458000 {6707689}
459000 {6723091}
460000 {6738887}
461000 {6754523}
462000 {6770143}
463000 {6786053}
464000 {6802111}
465000 {6817597}
466000 {6833207}
467000 {6848969}
468000 {6864181}
469000 {6879959}
470000 {6895381}
471000 {6911321}
472000 {6927127}
473000 {6942713}
474000 {6958187}
475000 {6973877}
476000 {6989809}
477000 {7005673}
478000 {7021363}
479000 {7036817}
480000 {7052107}
481000 {7068511}
482000 {7084073}
483000 {7099733}
484000 {7115111}
485000 {7131623}
486000 {7147351}
487000 {7163251}
488000 {7179097}
489000 {7195051}
490000 {7210757}
491000 {7226381}
492000 {7242349}
493000 {7258079}
494000 {7273723}
495000 {7289441}
496000 {7305383}
497000 {7321081}
498000 {7336891}
499000 {7353113}
500000 {7368743}
501000 {7384243}
502000 {7400551}
503000 {7416307}
504000 {7432081}
505000 {7447777}
506000 {7463887}
507000 {7479691}
508000 {7495913}
509000 {7511947}
510000 {7528111}
511000 {7543499}
512000 {7559171}
513000 {7575371}
514000 {7591271}
515000 {7606559}
516000 {7622863}
517000 {7638401}
518000 {7654133}
519000 {7670021}
520000 {7685791}
521000 {7701521}
522000 {7717387}
523000 {7733377}
524000 {7749491}
525000 {7765337}
526000 {7781311}
527000 {7797553}
528000 {7813111}
529000 {7829219}
530000 {7844729}
531000 {7860553}
532000 {7876343}
533000 {7891909}
534000 {7908011}
535000 {7923907}
536000 {7939787}
537000 {7955707}
538000 {7971737}
539000 {7987289}
540000 {8003531}
541000 {8019611}
542000 {8035733}
543000 {8051713}
544000 {8067149}
545000 {8083451}
546000 {8099629}
547000 {8115721}
548000 {8131229}
549000 {8147677}
550000 {8163037}
551000 {8179273}
552000 {8195009}
553000 {8210647}
554000 {8226733}
555000 {8242243}
556000 {8258377}
557000 {8274499}
558000 {8290411}
559000 {8306201}
560000 {8322239}
561000 {8338237}
562000 {8353703}
563000 {8369947}
564000 {8385803}
565000 {8401709}
566000 {8417999}
567000 {8434079}
568000 {8450537}
569000 {8466307}
570000 {8482247}
571000 {8497969}
572000 {8513783}
573000 {8529559}
574000 {8545333}
575000 {8561297}
576000 {8576873}
577000 {8592931}
578000 {8608837}
579000 {8624621}
580000 {8640601}
581000 {8656423}
582000 {8672501}
583000 {8688583}
584000 {8704609}
585000 {8720227}
586000 {8736187}
587000 {8752253}
588000 {8767951}
589000 {8783759}
590000 {8799913}
591000 {8816063}
592000 {8831983}
593000 {8848159}
594000 {8864491}
595000 {8880451}
596000 {8896453}
597000 {8912609}
598000 {8928151}
599000 {8944207}
600000 {8960447}
601000 {8976587}
602000 {8992421}
603000 {9008767}
604000 {9024989}
605000 {9041077}
606000 {9057407}
607000 {9073327}
608000 {9089137}
609000 {9104983}
610000 {9121433}
611000 {9137567}
612000 {9153337}
613000 {9169529}
614000 {9185497}
615000 {9201259}
616000 {9217427}
617000 {9233041}
618000 {9249211}
619000 {9265147}
620000 {9281003}
621000 {9297257}
622000 {9313063}
623000 {9329389}
624000 {9345503}
625000 {9361211}
626000 {9377377}
627000 {9393473}
628000 {9409747}
629000 {9425947}
630000 {9442211}
631000 {9458507}
632000 {9474373}
633000 {9490631}
634000 {9506389}
635000 {9522571}
636000 {9538871}
637000 {9554869}
638000 {9571019}
639000 {9586189}
640000 {9602441}
641000 {9618619}
642000 {9634829}
643000 {9650569}
644000 {9666707}
645000 {9682583}
646000 {9699199}
647000 {9715301}
648000 {9731053}
649000 {9747121}
650000 {9763387}
651000 {9779933}
652000 {9795781}
653000 {9811817}
654000 {9827813}
655000 {9844259}
656000 {9860429}
657000 {9876761}
658000 {9892819}
659000 {9909187}
660000 {9925387}
661000 {9941291}
662000 {9957677}
663000 {9973981}
664000 {9990689}
665000 {10006721}
666000 {10022689}
667000 {10038739}
668000 {10054547}
669000 {10070869}
670000 {10086761}
671000 {10102969}
672000 {10119209}
673000 {10135403}
674000 {10151003}
675000 {10167539}
676000 {10183861}
677000 {10200461}
678000 {10216793}
679000 {10232587}
680000 {10248883}
681000 {10264687}
682000 {10280797}
683000 {10296971}
684000 {10313701}
685000 {10329533}
686000 {10345637}
687000 {10361737}
688000 {10377967}
689000 {10394047}
690000 {10410133}
691000 {10426037}
692000 {10442119}
693000 {10458079}
694000 {10474153}
695000 {10490177}
696000 {10506277}
697000 {10522537}
698000 {10538903}
699000 {10554833}
700000 {10570837}
701000 {10587167}
702000 {10603513}
703000 {10619927}
704000 {10635799}
705000 {10651661}
706000 {10668079}
707000 {10683847}
708000 {10699901}
709000 {10715599}
710000 {10731997}
711000 {10748303}
712000 {10764707}
713000 {10781107}
714000 {10797509}
715000 {10813457}
716000 {10830151}
717000 {10845871}
718000 {10862009}
719000 {10878319}
720000 {10894789}
721000 {10910593}
722000 {10926677}
723000 {10942949}
724000 {10959059}
725000 {10975499}
726000 {10991699}
727000 {11007797}
728000 {11024339}
729000 {11040703}
730000 {11056699}
731000 {11072717}
732000 {11088841}
733000 {11104817}
734000 {11120749}
735000 {11137859}
736000 {11153833}
737000 {11169811}
738000 {11185663}
739000 {11201893}
740000 {11218433}
741000 {11234809}
742000 {11250929}
743000 {11267159}
744000 {11283889}
745000 {11299979}
746000 {11316269}
747000 {11332457}
748000 {11348969}
749000 {11365091}
750000 {11381597}
751000 {11397787}
752000 {11414237}
753000 {11430301}
754000 {11446823}
755000 {11462741}
756000 {11478743}
757000 {11495317}
758000 {11511713}
759000 {11527861}
760000 {11543503}
761000 {11559971}
762000 {11576321}
763000 {11592307}
764000 {11608489}
765000 {11623933}
766000 {11641099}
767000 {11656907}
768000 {11673199}
769000 {11689771}
770000 {11705849}
771000 {11722201}
772000 {11738627}
773000 {11754907}
774000 {11771411}
775000 {11787323}
776000 {11803523}
777000 {11819831}
778000 {11836457}
779000 {11852917}
780000 {11869007}
781000 {11885191}
782000 {11901611}
783000 {11917747}
784000 {11933927}
785000 {11950073}
786000 {11966099}
787000 {11982679}
788000 {11998817}
789000 {12015359}
790000 {12031847}
791000 {12048217}
792000 {12064093}
793000 {12080657}
794000 {12097381}
795000 {12113473}
796000 {12129937}
797000 {12145849}
798000 {12162247}
799000 {12179033}
800000 {12195251}
801000 {12211657}
802000 {12227783}
803000 {12244087}
804000 {12260473}
805000 {12276689}
806000 {12292963}
807000 {12309029}
808000 {12325037}
809000 {12342049}
810000 {12358067}
811000 {12373861}
812000 {12390379}
813000 {12406649}
814000 {12422339}
815000 {12439073}
816000 {12455411}
817000 {12471887}
818000 {12488039}
819000 {12505117}
820000 {12520999}
821000 {12537713}
822000 {12553859}
823000 {12570541}
824000 {12586853}
825000 {12603121}
826000 {12619127}
827000 {12635563}
828000 {12651907}
829000 {12668807}
830000 {12684701}
831000 {12701093}
832000 {12717227}
833000 {12733333}
834000 {12749537}
835000 {12765881}
836000 {12781907}
837000 {12798421}
838000 {12814969}
839000 {12831097}
840000 {12847397}
841000 {12863687}
842000 {12880117}
843000 {12896729}
844000 {12913457}
845000 {12930019}
846000 {12946603}
847000 {12963089}
848000 {12979579}
849000 {12996121}
850000 {13012247}
851000 {13028531}
852000 {13045387}
853000 {13061957}
854000 {13078459}
855000 {13095421}
856000 {13111597}
857000 {13128103}
858000 {13144259}
859000 {13160969}
860000 {13177399}
861000 {13193491}
862000 {13209863}
863000 {13226123}
864000 {13242773}
865000 {13259347}
866000 {13275571}
867000 {13292269}
868000 {13308319}
869000 {13324859}
870000 {13341161}
871000 {13357571}
872000 {13373947}
873000 {13390249}
874000 {13406513}
875000 {13423483}
876000 {13439467}
877000 {13456591}
878000 {13472939}
879000 {13489549}
880000 {13506001}
881000 {13522577}
882000 {13539049}
883000 {13555657}
884000 {13571977}
885000 {13588241}
886000 {13604699}
887000 {13620829}
888000 {13637249}
889000 {13653763}
890000 {13669739}
891000 {13686193}
892000 {13702769}
893000 {13718941}
894000 {13735663}
895000 {13751891}
896000 {13768387}
897000 {13784921}
898000 {13801159}
899000 {13817443}
900000 {13834087}
901000 {13850021}
902000 {13866841}
903000 {13882879}
904000 {13899187}
905000 {13915357}
906000 {13931809}
907000 {13948397}
908000 {13965233}
909000 {13982057}
910000 {13998659}
911000 {14015333}
912000 {14031571}
913000 {14048261}
914000 {14064863}
915000 {14081143}
916000 {14097553}
917000 {14113573}
918000 {14130097}
919000 {14146553}
920000 {14163139}
921000 {14179691}
922000 {14196703}
923000 {14213257}
924000 {14230043}
925000 {14246101}
926000 {14262431}
927000 {14278687}
928000 {14295067}
929000 {14311657}
930000 {14328217}
931000 {14344513}
932000 {14360267}
933000 {14376893}
934000 {14392717}
935000 {14409413}
936000 {14425897}
937000 {14442611}
938000 {14459213}
939000 {14475833}
940000 {14492201}
941000 {14508943}
942000 {14525561}
943000 {14541983}
944000 {14558473}
945000 {14574713}
946000 {14591023}
947000 {14607577}
948000 {14624537}
949000 {14641213}
950000 {14657899}
951000 {14674313}
952000 {14690521}
953000 {14707207}
954000 {14723629}
955000 {14739997}
956000 {14756509}
957000 {14772773}
958000 {14789051}
959000 {14805367}
960000 {14822221}
961000 {14838673}
962000 {14855077}
963000 {14871613}
964000 {14888557}
965000 {14905171}
966000 {14921891}
967000 {14938519}
968000 {14954449}
969000 {14971721}
970000 {14988607}
971000 {15004949}
972000 {15021547}
973000 {15037859}
974000 {15054209}
975000 {15070829}
976000 {15087613}
977000 {15104053}
978000 {15120739}
979000 {15137063}
980000 {15153833}
981000 {15170017}
982000 {15186841}
983000 {15203777}
984000 {15220313}
985000 {15236737}
986000 {15253099}
987000 {15269477}
988000 {15285923}
989000 {15302351}
990000 {15318857}
991000 {15335629}
992000 {15351997}
993000 {15368443}
994000 {15385807}
995000 {15402707}
996000 {15419291}
997000 {15435911}
998000 {15452869}
999000 {15469309}
1000000 {15485857}

1000000 {15485857}

Friday, June 18, 2010

Thursday, June 17, 2010

Electronics: Binary Clock: Concept & Design

 As a way to get back into electronics and integrated circuitry before heading off to University in September I have started a project to design and build a Binary Clock.
A Binary Clock displays time using two groups of LED's; one consisting of six LED's, and the other of seven for a total of 13. 

[ A Binary Display showing 6:33am ]
 [ A Binary Display showing 18:33pm (6:33pm) ]

This is the most efficient Display Model for showing time in Binary, despite the fact it can go up to 315:715, any display smaller than this could not display the vital 23:59 needed to show time in 24 hour format. A display for showing Binary Time in a 12 hour format is only different by the one LED used to represent "20 hours" (left most, row 2), but seeing as an LED would have to be added in to signal AM or PM the 13 LED Display is the most efficient configuration.

[ Schematic v2 ]

This is the 2nd Revision of my Schematic for the Clock. It includes a power circuit (Top Left), a BasicStamp2 Micro-Controller (Top Right), an array of 13 LED's to display the time (Bottom Left), and a In/Out bank consisting of a Button to set the hour, a Button to set the minute, and a Piezo Buzzer to allow the clock to chime or beep on the hour.

The Power Circuit:
The power circuit consists of a standard DC Power Jack (J1), a Diode (D1), a Two Position Switch (S1), two 0.1uf Ceramic Capacitors (C1 & C2), and a 7805 Voltage Regulator (IC3).

By placing a Capacitor on either side of the Voltage Regulator we smooth out any signals or frequencies that may be in the power line. The Diode right after the Power Jack ensures that the flow of electricity does not flow the wrong way, as this could easily damage the Voltage Regulator. 

The Micro-Controller:
The Micro-Controller is the brains of the beast, it has two Ground (GND) pins, one Voltage In (VIN) pin, and 15 Digital I/O pins. By programming the Chip to output either 5volts or 0volts to each of the first 13 pins we control what number configuration is displayed. The last three pins are reserved for the two Buttons to set the time, and a Piezo Buzzer to allow the clock to make audio signals.  

[ BasicStamp2 Micro-Controller ]


Monday, January 11, 2010

Programming: Java: Tutorials: Constructors, Sub-Routines, and Functions

 Certainly, I'd be glad to clarify for you.

When you have a "Sub-Routine" (pocket of code which just runs [fire and forget]) you aren't receiving anything back. You are just saying "go off and do this so I don't have to write out that code again".

A Function is like a Sub-Routine in the sense that it is a section of code that you can keep calling over and over again, accept that it also returns a value.

This can be seen in the code like so.

A sub-routine would be... (For a theoretical class called dog)

public void Bark() {
  System.out.println("Woof! Woof!!!");
}

Notice how seeing as we don't want to retrieve a value from the Bark() method we write Void in its header.

Now, lets say we wanted a Function to tell us how old the dog was... (Note "DogAge" is an integer variable)

public int getAge() {
  return DogAge;
}

Notice how because we want to retrieve an integer we place int instead of Void in the header. This makes the Function return an integer, which we give to it using the "return" command.

The difference is that if we wanted to make the dog bark we would literally type...

myDog.Bark();

and be done with it. But with a function we have to store the value it returns. Seeing as our Function in this case returns an integer, we store it like so...

int Age =  myDog.getAge();

Now we have created a new integer variable called age and given it a value from our Dog class.

But don't be fooled, a Function doesn't just have to return a pre-existing variable. Before we write...

return VAR;

at the end of the function, we can put any calculations we want.

This has been a very drawn out explanation. But don't worry it will become clear in time.

All you need to know about constructors and why I commented on them not "even" having Void in their header was that they are unique, and therefore easy to spot and manipulate.

//Beginning of the Dog Class
public class Dog() {

  //The variables to store information about the dog
  int DogAge;
  String Name;


  //Default Constructor
  public Dog() {
    DogAge = 5;
    Name = "Saxon";
  }


  //2nd Con. for if we want to specify an Age
  public Dog(int Age) {
     DogAge = Age;
     Name = "Saxon";
   }


  //3rd Con. for if we want to specify a Name
  public Dog(String name) {
     DogAge = 5;
     Name = name;
   }

  //4th Con. for if we want to specify a Name and Age
  public Dog(int Age, String name) {
     DogAge = Age;
     Name = name;
   }


  //A Sub-Routine to make the dog Bark
  public void Bark() {
    System.out.println("Woof! Woof!!!");
  }


  //A Function to retrieve the dogs Age
  public int getAge() {
    return DogAge;
  }


}

This sample class shows the fundamental differences between Constructors, Sub-Routines, and Functions.

I hope this helps.

Programming: Java: Tutorials: A Guide to Class Architecture

Java is a type of language known as OO or Object-Oriented. OO Programming differs from other types because it allows the programmer to write less code, which in turn is more efficient code.

As we know, in java a class is written in a .java file. Inside this file we denote the beginning of our class with a line similar to the one bellow.
public class Cat {
This line would create a public class named Cat. That would mean a class that any other class can access. We'll get more into that later.

Inside the class tags, if this is our main class, we have the main() method. This is the method which runs when the program starts.

public void main(String[] args) {
So, what have we done so far? We have made a .java file called Cat.java, which contains a public class name called Cat. Also we have a main method within our class.

So your thinking "So what?", that this is nothing new. Well, that may be but the class we have created has a major flaw. It's static!

Static vs. Non-Static

A Static class means class which just IS. I can't be recreated. It just runs. Values can be applied to it, and drawn from it, but you cannot make multiple instances of it.

What we need is a way to make each version of our class different. What we need is a Constructor!

Constructors

A constructor allows us to create multiple versions of our class in memory, each with different values.

The constructor is a sub-routine which is public, has no return statement (not even void), and has the same name call as the name of the class. In our case a constructor would look like this.

public Cat() {
This would be a "Default Constructor". The reason it gets this title is because it accepts no inputs from the class creating it. All it does is create a new instance of the Cat class using what ever values are already stored within it by default.

Also, like all other sub-routines we can have multiple versions of it, each with different parameters. Like so.

//Default Con.
public Cat() {
  s = 10;
  a = 5;
}

//Secondary Con.
public Cat(int Size) {
  s = size;
  a = 5;
}

//Thir-dary Con.
public Cat(String name) {
  s = 10;
  n = name;
}

//Fourth-dary Con.
public Cat(int Size, String name) {
  s = size;
  n = name;
}

See, by creating a constructor for each possible contingency we allow the Cat class to work under any circumstance.

Now from another class we could create multiple instances of the Cat Class, each with different values like so.

Cat c1 = new Cat();
Cat c2 = new Cat(5,"Dilbert");
Cat c3 = new Cat(7);
Cat c4 = new Cat("Dill");

or we could create an Array of Cat like so.

Cat[] cats = new Cat[4];
for (int i = 0; i < cats.size; i++) {
  cats[i] = new Cat((int)(i,"Cat: #" + i);
}

Sadly...

Sadly the powers that be at VHS aren't exactly "fond" of me posting how to's on my blog here so that's all I'm going to go over. This is the basics of the Class system, there isn't too much else to it accept for practice. Good luck!