1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486 |
- <?xml version="1.0" encoding="utf-8"?>
- <!--
-
- h t t :: / / t /
- h t t :: // // t //
- h ttttt ttttt ppppp sssss // // y y sssss ttttt //
- hhhh t t p p s // // y y s t //
- h hh t t ppppp sssss // // yyyyy sssss t //
- h h t t p s :: / / y .. s t .. /
- h h t t p sssss :: / / yyyyy .. sssss t .. /
-
- <https://y.st./>
- Copyright © 2017 Alex Yst <mailto:copyright@y.st>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org./licenses/>.
- -->
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <base href="https://y.st./en/coursework/CS1102/" />
- <title>Learning Journal <https://y.st./en/coursework/CS1102/></title>
- <link rel="icon" type="image/png" href="/link/CC_BY-SA_4.0/y.st./icon.png" />
- <link rel="stylesheet" type="text/css" href="/link/basic.css" />
- <link rel="stylesheet" type="text/css" href="/link/site-specific.css" />
- <script type="text/javascript" src="/script/javascript.js" />
- <meta name="viewport" content="width=device-width" />
- </head>
- <body>
- <nav>
- <p>
- <a href="/en/">Home</a> |
- <a href="/en/a/about.xhtml">About</a> |
- <a href="/en/a/contact.xhtml">Contact</a> |
- <a href="/a/canary.txt">Canary</a> |
- <a href="/en/URI_research/"><abbr title="Uniform Resource Identifier">URI</abbr> research</a> |
- <a href="/en/opinion/">Opinions</a> |
- <a href="/en/coursework/">Coursework</a> |
- <a href="/en/law/">Law</a> |
- <a href="/en/a/links.xhtml">Links</a> |
- <a href="/en/coursework/CS1102/index.xhtml.asc">{this page}.asc</a>
- </p>
- <hr/>
- <p>
- <a href="/en/coursework/BUS1101/" title="Principles of Business Management">BUS 1101</a> |
- <span class="hyperlink_unavailable" title="Basic Accounting">BUS 1102</span> |
- <span class="hyperlink_unavailable" title="Principles of Marketing">BUS 2201</span> |
- <span class="hyperlink_unavailable" title="Multinational Management">BUS 2207</span> |
- <span class="hyperlink_unavailable" title="Business and Society">BUS 3306</span> |
- <a href="/en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a> |
- <a href="/en/coursework/CS1102/" title="Programming 1">CS 1102</a> |
- <a href="/en/coursework/CS1103/" title="Programming 2">CS 1103</a> |
- <span class="hyperlink_unavailable" title="Computer Systems">CS 1104</span> |
- <a href="/en/coursework/CS2203/" title="Databases 1">CS 2203</a> |
- <span class="hyperlink_unavailable" title="Communications and Networking">CS 2204</span> |
- <a href="/en/coursework/CS2205/" title="Web Programming 1">CS 2205</a> |
- <a href="/en/coursework/CS2301/" title="Operating Systems 1">CS 2301</a> |
- <span class="hyperlink_unavailable" title="Software Engineering 1">CS 2401</span> |
- <span class="hyperlink_unavailable" title="Data Structures">CS 3303</span> |
- <span class="hyperlink_unavailable" title="Analysis of Algorithms">CS 3304</span> |
- <span class="hyperlink_unavailable" title="Web Programming 2">CS 3305</span> |
- <span class="hyperlink_unavailable" title="Databases 2">CS 3306</span> |
- <span class="hyperlink_unavailable" title="Operating Systems 2">CS 3307</span> |
- <span class="hyperlink_unavailable" title="Information Retrieval">CS 3308</span> |
- <span class="hyperlink_unavailable" title="Comparative Programming Languages">CS 4402</span> |
- <span class="hyperlink_unavailable" title="Software Engineering 2">CS 4403</span> |
- <span class="hyperlink_unavailable" title="Advanced Networking and Data Security">CS 4404</span> |
- <span class="hyperlink_unavailable" title="Mobile Applications">CS 4405</span> |
- <span class="hyperlink_unavailable" title="Computer Graphics">CS 4406</span> |
- <span class="hyperlink_unavailable" title="Data Mining and Machine Learning">CS 4407</span> |
- <span class="hyperlink_unavailable" title="Artificial Intelligence">CS 4408</span> |
- <span class="hyperlink_unavailable" title="English Composition 2">ENGL 1102</span> |
- <span class="hyperlink_unavailable" title="World Literature">ENGL 1405</span> |
- <span class="hyperlink_unavailable" title="Introduction to Environmental Science">ENVS 1301</span> |
- <a href="/en/coursework/HIST1421/" title="Greek and Roman Civilization">HIST 1421</a> |
- <span class="hyperlink_unavailable" title="College Algebra">MATH 1201</span> |
- <span class="hyperlink_unavailable" title="Calculus">MATH 1211</span> |
- <span class="hyperlink_unavailable" title="Introduction to Statistics">MATH 1280</span> |
- <span class="hyperlink_unavailable" title="Discrete Mathematics">MATH 1302</span> |
- <span class="hyperlink_unavailable" title="Introduction to Philosophy">PHIL 1402</span> |
- <a href="/en/coursework/PHIL1404/" title="Ethics and Social Responsibility">PHIL 1404</a> |
- <a href="/en/coursework/POLS1503/" title="Globalization">POLS 1503</a> |
- <span class="hyperlink_unavailable" title="Introduction to Psychology">PSYC 1504</span> |
- <a href="/en/coursework/UNIV1001/" title="Online Education Strategies">UNIV 1001</a>
- </p>
- <hr/>
- <p>
- <a href="/en/coursework/CS1102/GravityCalculator.java.xhtml" title="GravityCalculator.java">Unit 1</a> |
- <a href="/en/coursework/CS1102/SnakeEyesCount.java.xhtml" title="SnakeEyesCount.java">Unit 2</a> |
- <a href="/en/coursework/CS1102/Unit3.java.xhtml" title="Unit3.java">Unit 3</a> |
- <a href="/en/coursework/CS1102/firstsubroutines.java.xhtml" title="firstsubroutines.java">Unit 4</a> |
- <a href="/en/coursework/CS1102/Unit5.java.xhtml" title="Unit5.java">Unit 5</a> |
- <a href="/en/coursework/CS1102/payrollsystem.xhtml" title="package payrollsystem;">Unit 7</a> |
- <a href="/en/coursework/CS1102/matchbox.xhtml" title="package matchbox;">Unit 6</a> |
- <a href="/en/coursework/CS1102/Unit7.java.xhtml" title="Unit7.java">Unit 7</a>
- </p>
- <hr/>
- </nav>
- <header>
- <h1>Learning Journal</h1>
- <p>CS 1102: Programming 1</p>
- </header>
- <h2 id="Unit1">Unit 1</h2>
- <p>
- I've anxiously awaited the availability of the coursework.
- At 22:05 (in my time zone), I was finally able to begin.
- I started by taking a look at the learning journal, programming assignment, and discussion for the week.
- It looks like the learning journal assignments in this course are going to be the same as they were in <a href="https://y.st./en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a>: a detailed overview of my activities for the week and a reflection.
- Like last time, this'll be really easy; I already keep a <a href="https://y.st./en/weblog/">daily journal</a> of everything I do, so completing this kind of learning journal assignment simply means writing it all down here instead of there.
- </p>
- <p>
- The programming assignment had me worried.
- It said to first install NetBeans.
- I'm always weary when specific software is needed.
- I only run free software, and this NetBeans might be something proprietary.
- Thankfully, it's not; <a href="https://netbeans.org/cddl-gplv2.html">it's covered by the <abbr title="GNU's Not Unix">GNU</abbr> <abbr title="General Public License version Two">GPLv2</abbr></a>, a common free software license.
- Strangely, NetBeans <a href="https://packages.debian.org/search?keywords=netbeans">isn't in the latest stable version of Debian</a>, Debian 8, but <strong>*was*</strong> present in Debian 7 and <strong>*is*</strong> present in the testing version of Debian that will become Debian 9 one day.
- Not having NetBeans in my repository made it more difficult to install.
- I downloaded and ran the installer, but it couldn't find my OpenJDK installation for some reason.
- I wasn't sure what to do about that, and I thought I'd need to use my own development environment (Geany plus the command line).
- </p>
- <p>
- The discussion assignment was about four operators, ones that are very familiar from <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>.
- This is a course on Java though, not <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, so I waited until reading the course material for the week to write up my discussion assignment submission, just in case a couple of them do something different in Java.
- </p>
- <p>
- The introduction to the reading material had this to say:
- </p>
- <blockquote>
- <p>
- This course has no prerequisites, although a general familiarity with computers would certainly be helpful.
- </p>
- </blockquote>
- <p>
- Lies!
- <span title="Programming Fundamentals">CS 1101</span> was a prerequisite to this course.
- On that same page, the first page that contains study material, it also says this:
- </p>
- <blockquote>
- <p>
- In unit 1 we learned about compilers and interpreters and their role to check syntax of a program, however, some errors can actually be identified by the IDE editor before the program is subjected to either the compiler or interpreter.
- </p>
- </blockquote>
- <p>
- Um.
- This <strong>*is*</strong> <a href="https://y.st./en/coursework/CS1102/#Unit1">Unit 1</a>, and we haven't even mentioned compilers or interpreters yet.
- I know what both are, but what is this page even talking about?
- I get the feeling this course will be a bit disorganised, not unlike the assignment instructions from <span title="Programming Fundamentals">CC 1101</span>.
- Later in the unit, we did discuss interpreters and compilers, but that was only after passing through this introduction that said we'd already covered them.
- </p>
- <p>
- The instructions in the introduction to the reading assignment direct us to the Windows download page for NetBeans.
- You can't assume everyone's running Windows, because we're not.
- Only most of us are.
- I located the download I need, though I didn't seem to be able to get the software working for the time being.
- Later in the week, I decided to try again to install NetBeans.
- This time, I had a breakthrough.
- I did some research online to find where my <abbr title="Java Development Kit">JDK</abbr> installation was, so I could manually tell NetBeans where to find it.
- After pointing the NetBeans installer int the right direction, it finally gave me a more helpful error message.
- Previously, it told me it couldn't find <abbr title="Java Development Kit">JDK</abbr>.
- Now, it told me that <abbr title="Java Runtime Environment">JRE</abbr> was present, but a full <abbr title="Java Development Kit">JDK</abbr> was needed.
- I use OpenJDK as my Java environment.
- You'd think that something with "JDK" in the name would contain a <abbr title="Java Development Kit">JDK</abbr> implementation.
- However, that's not exactly the case, at least in the Debian package of it.
- Either the OpenJDK team or the Debian Project team broke OpenJDK up into different components, and I intuitively had the OpenJDK <abbr title="Java Runtime Environment">JRE</abbr> installed, but not the OpenJDK <abbr title="Java Development Kit">JDK</abbr>.
- Running <code>sudo aptitude install <a href="apt:openjdk-7-jdk">openjdk-7-jdk</a></code> on the command line fixed the main problem with the NetBeans installer, though the installer kind of stumbled a bit.
- It no longer complained that no <abbr title="Java Development Kit">JDK</abbr> was installed, but after the installation was running, it complained that no <abbr title="Java Development Kit">JDK</abbr> was found in the location that the installer had decided it was.
- I aborted the installation, deleted the work it'd completed, and ran it again.
- This time, I tried a few different directories until I found the one the installer wanted the <abbr title="Java Development Kit">JDK</abbr> path set to.
- Everything seemed to work this time.
- </p>
- <p>
- The reading assignment was as follows:
- </p>
- <ul>
- <li>
- <a href="https://docs.oracle.com./javase/tutorial/getStarted/">Trail: Getting Started (The Java™ Tutorials)</a>
- </li>
- <li>
- <a href="https://docs.oracle.com./javase/tutorial/getStarted/cupojava/netbeans.html">"Hello World!" for the NetBeans IDE (The Java™ Tutorials > Getting Started > The "Hello World!" Application)</a>
- </li>
- <li>
- <a href="https://docs.oracle.com./javase/tutorial/java/concepts/">Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)</a>
- </li>
- <li>
- <a href="https://docs.oracle.com./javase/tutorial/java/nutsandbolts/">Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)</a>
- </li>
- <li>
- <a href="https://en.wikipedia.org./wiki/ASCII">ASCII - Wikipedia</a>
- </li>
- <li>
- <a href="https://netbeans.org./kb/docs/java/javase-intro.html">Developing General Java Applications - NetBeans IDE Tutorial</a>
- </li>
- <li>
- <a href="https://netbeans.org./kb/docs/java/quickstart.html">NetBeans IDE Java Quick Start Tutorial</a>
- </li>
- <li>
- <a href="https://netbeans.org./kb/trails/java-se.html">General Java Development Learning Trail - NetBeans Tutorials, Guides and Articles</a>
- </li>
- <li>
- <a href="https://ocw.mit.edu./courses/electrical-engineering-and-computer-science/6-092-introduction-to-programming-in-java-january-iap-2010/lecture-notes/MIT6_092IAP10_lec01.pdf">6.092 Lecture 1: Types, Variables, Operators - MIT6_092IAP10_lec01.pdf</a>
- </li>
- <li>
- <a href="https://www.youtube.com./watch?v=0iXc1Me2xiA">CS1102 Programming 1 Unit 1 Lecture 2 - YouTube</a>
- </li>
- <li>
- <a href="https://www.youtube.com./watch?v=T6Sa3U7OT_o">CS1102 Programming 1 Unit 1 Lecture 3 - YouTube</a>
- </li>
- <li>
- <a href="https://www.youtube.com./watch?v=_e65BDDjY9Y">CS1102 Programming 1 Unit 1 Lecture 1 - YouTube</a>
- </li>
- <li>
- <a href="http://gwydir.demon.co.uk./jo/numbers/binary/intro.htm">Introduction to Binary</a>
- </li>
- <li>
- <a href="http://linfo.org/ascii.html">ASCII definition by The Linux Information Project (LINFO)</a>
- </li>
- <li>
- <a href="http://math.hws.edu./javanotes/c1/index.html">Javanotes 7.0, Chapter 1 -- Overview: The Mental Landscape</a>
- </li>
- <li>
- <a href="http://math.hws.edu./javanotes/c2/index.html">Javanotes 7.0, Chapter 2 -- Programming in the Small I: Names and Things</a>
- </li>
- </ul>
- <p>
- I found it interesting that the video lecture called <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> a shell script language.
- I myself use <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> as a shell script language on a daily basis.
- However, most people don't look at <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> as a shell script language.
- Instead, they think <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> is Web-specific and only runs in the context of a Web server, sending pages to a Web browser upon request.
- <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> actually does have full shell script capabilities though, and can even be used to develop graphical applications (though I haven't use it for that myself yet).
- It was a pleasant change to hear my native language not written off as "just a website thing".
- </p>
- <p>
- The lecturer also addressed that if you're watching the video on a mobile telephone, that device is nothing more than a mobile computer.
- Yes!
- I've been trying to tell people for years that smartphones aren't telephones at all, but computers.
- Many people write me off though.
- Arguably, some mobile telephones actually are telephones, but those devices aren't the ones that are Web-enabled or in any way able to engage in video playback.
- People sometimes think I'm silly for calling my mobile a "mobile" or a "device", but it's a simple matter of accuracy.
- It's not a telephone, so I don't call it a "phone".
- A real telephone has no need for a <abbr title="central processing unit">CPU</abbr> or operating system.
- </p>
- <p>
- I found the explanation of the Java Virtual Machine to be very enlightening.
- I already knew how it worked, but the lecturer showed me a new way to think about it.
- I've always considered "compiled" java code to be be a middle state between actual compiled code and source code.
- The Java language is both compiled and interpreted, as it must be compiled first, then the compiled code must be interpreted.
- It's a strange hybrid of the two systems.
- It's useful, but still very strange.
- The lecturer talked about how the <abbr title="Java virtual machine">JVM</abbr> is an emulator though.
- It emulates a system that has never really existed.
- It's an abstraction.
- The Java compiled compiles code to run on this nonexistent system, then the code is run on the emulator that emulates this system.
- I still think my original notion is more accurate, as emulation is an expensive process.
- Java is known to be inefficient, but if it were doing full emulation, it'd have a lot more overhead.
- Instead, Java probably functions more like <abbr title="Wine Is Not an Emulator">Wine</abbr>, but instead of acting as a compatibility <abbr title="application programming interface">API</abbr> for Windows-based instructions, acts as a compatibility <abbr title="application programming interface">API</abbr> for this idealised Java computer.
- </p>
- <p>
- The lecturer says NetBeans is a lot easier to install, get set up, and update than Eclipse.
- That was a laugh!
- That totally depends on your local setup.
- I could install Eclipse as easily as running <code>sudo aptitude install <a href="apt:eclipse">eclipse</a></code>.
- I've done that before, and Eclipse is still in the Debian 8 repositories.
- To update it, all I'd have to do is run <code>sudo aptitude upgrade</code>, and Eclipse would be updated along with the rest of my software.
- It wouldn't need to be updated individually.
- With NetBeans, if I want to update it, I need to delete the current installation from my file manager, find and download the new version, then run the installer and hope it works.
- I'll likely need to locate the <abbr title="Java Development Kit">JDK</abbr> path again too to make the installation successful.
- As for the initial install, it took me four days to figure out how to get it to install properly.
- NetBeans was <strong>*not*</strong> easier to install!
- Which <abbr title="integrated development environment">IDE</abbr> is easier to install, set up, and keep updated is platform-dependant.
- The lecturer also said that installing NetBeans automatically installs the <abbr title="Java Development Kit">JDK</abbr> as well, but again, that's platform dependant.
- It doesn't do that on Debian unless installed through <abbr title="Advanced Package Tool">APT</abbr> or Aptitude, which isn't an option on Debian 8.
- </p>
- <p>
- I got a lot of error messages when running the initial code provided to us.
- One was this:
- </p>
- <blockquote>
- WARNING torsocks[10829]: [syscall] Unsupported syscall number 229. Denying the call (in tsocks_syscall() at syscall.c:165)
- </blockquote>
- <p>
- The other twelve error messages were this:
- </p>
- <blockquote>
- WARNING torsocks[10829]: [syscall] Unsupported syscall number 186. Denying the call (in tsocks_syscall() at syscall.c:165)
- </blockquote>
- <p>
- Is ... Is NetBeans trying to access the Internet in some way while executing my code?
- It really shouldn't be doing that, there's nothing network-related about the initial code.
- I located NetBeans' proxy settings so I could set those directly and quit using <code>torsocks</code>, and that cleared up the issue.
- </p>
- <p>
- When explaining data types, the lecturer said that using a byte, we can store one of 2^8 different numbers, then calculates that as two-hundred fifty-five different numbers.
- As usual, people are forgetting to count zero.
- There are two-hundred fifty-<strong>*six*</strong> possibilities, and 2^8 is equal to 256.
- Additionally, the lecturer said the values go from -127 to +128.
- If Java is like other programming languages, that's backwards.
- The <strong>*negative*</strong> limit is further from zero than the positive limit.
- They should've said the range is from -128 to +127.
- </p>
- <p>
- The introduction to object-oriented concepts was pretty much just review.
- I use object-oriented programming concepts in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> all the time.
- <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>'s sort of a hybrid language, able to be used in a functional way or an object-oriented way.
- I tend to prefer the object-oriented way of expressing code when there's even the slightest bit of complexity involved.
- As soon as data has to fit some domain or there's two or more functions that are interrelated, I set up a class with a validating constructor and/or turn the functions into class methods.
- The parts on variables and control flow were also just review, though the part on operators was helpful for making sure my understanding of <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> operators also applied to Java.
- Different languages use different operators.
- For example, <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, Python, and Java all use <code>!=</code> to perform an inequality test.
- However, Lua instead uses the <code>~=</code> operator for that purpose.
- </p>
- <p>
- The material on <abbr title="American Standard Code for Information Interchange">ASCII</abbr> was review as well, and these days, <abbr title="American Standard Code for Information Interchange">ASCII</abbr> itself shouldn't really used much.
- Unicode has superseded it.
- <abbr title="American Standard Code for Information Interchange">ASCII</abbr> is unfortunately embedded in many networking protocols, but I have hope this'll be corrected in the future.
- That said, maybe it won't.
- Take the use of <abbr title="Domain Name System">DNS</abbr> in other protocols, for example.
- <abbr title="Domain Name System">DNS</abbr> can store names of just about any value, but in practice, it only holds names with characters from a subset of <abbr title="American Standard Code for Information Interchange">ASCII</abbr>.
- The reason for this is that other characters aren't permitted by other protocols, such as <abbr title="Hypertext Transfer Protocol">HTTP</abbr>.
- Did the standards bodies decide to fix this limitation in <abbr title="Hypertext Transfer Protocol">HTTP</abbr>?
- No.
- Instead, they built a hacky workaround and called it <a href="https://en.wikipedia.org./wiki/Internationalized_domain_name"><abbr title="internationalised domain name">IDN</abbr>s</a>.
- Instead of actually fixing the problem, they made a bit of a mess of it.
- It's highly probable that other such hacky workarounds will be put in place to make use of <abbr title="American Standard Code for Information Interchange">ASCII</abbr> in various networking protocols work globally instead of actually repairing the outdated protocols themselves.
- The <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <strong>*table*</strong> still remains useful though even outside <abbr title="American Standard Code for Information Interchange">ASCII</abbr>'s archaic use, as the beginning of the greater Unicode table and the section of Unicode most used by certain groups of people.
- It's be an incredible waste of time and brain space to try to memorise the <abbr title="American Standard Code for Information Interchange">ASCII</abbr> table, but there's some things I think everyone working in computer science should remember about the character order.
- Control characters come first, at the beginning of the <abbr title="American Standard Code for Information Interchange">ASCII</abbr> table.
- After that, come digits, upper-case letters, and lower-case letters, in that order. Between these four groups, and after the lower-case letters, other characters are stored.
- When in doubt as to the order of non-alphanumeric characters, just consult an <abbr title="American Standard Code for Information Interchange">ASCII</abbr> table; they're not hard to find.
- I use an application on my computer frequently to find characters I can't type from the Unicode table, and it has an option to display just the <abbr title="American Standard Code for Information Interchange">ASCII</abbr> section, which also comes in handy for sorting.
- For example, I have my <a href="https://bandcamp.com/y_st">Bandcamp collection</a> organised in Unicode order.
- Albums beginning with digits are near the beginning, while albums from artists that can't even be bothered to capitalize the proper nouns that are their album titles are at the end.
- Bandcamp doesn't offer automated sorting options, so I have to do all that manually using their drag-and-drop album-sorting interface.
- When album titles start with strange characters, the <abbr title="American Standard Code for Information Interchange">ASCII</abbr> table's nice to have around to help get everything set up right.
- </p>
- <p>
- The part on binary was review as well.
- Binary's just like the number system we commonly use, except that binary's in base two and the one we usually use in in base base ten.
- All the regular concepts apply though.
- I wouldn't say all number systems are this similar, though.
- For example, there's also Roman numerals and such, that don't really have the same concept of a base that binary and our common number system do.
- ally marks are another great example.
- Binary has a lot in common with our common number system that people take for granted, things that really aren't just a given.
- </p>
- <p>
- Before getting to work on my programming assignment, I looked through NetBean's settings a bit.
- First, I made the whitespace visible.
- As a programmer, there's no excuse for having any character be invisible.
- Ever.
- Last term, I was in a class in which we used <abbr title="Integrated Development and Learning Environment">IDLE</abbr>.
- One of <abbr title="Integrated Development and Learning Environment">IDLE</abbr>s main flaws was that there wasn't a way to get it to display whitespace, but NetBeans doesn't suffer from this problem.
- Next, I noticed a comments section in the template NetBeans provided me to start with:
- </p>
- <blockquote>
- <pre><code>/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */</code></pre>
- </blockquote>
- <p>
- NetBeans has license headers?
- Neat.
- I tried changing the license header though, and it had no effect.
- I tried deleting the project and starting fresh.
- That didn't do it either.
- Eventually, I figured out I needed to change the license header, delete the initial class file, then recreate the initial class file.
- The license header is <strong>*only*</strong> added when the file is first created.
- Changes made later are <strong>*not*</strong> applied.
- I'm not yet sure how to set a default license header though.
- It'd save me a lot of time if I could just have the license header always start as specifying all my projects as being covered by the <abbr title="GNU's Not Unix">GNU</abbr> <abbr title="General Public License version Three or later">GPLv3+</abbr>.
- As I started work on the assignment, I noticed NetBeans was automatically indenting code I added, but it was doing so using spaces instead of tabs.
- That had to be corrected before I could get anywhere.
- Otherwise, I'd have to go back and reformat my code manually before submitting it.
- Additionally, NetBeans had hijacked my tab key, so instead of inserting tab characters, it inserted strings of spaces.
- I had that same problem with <abbr title="Integrated Development and Learning Environment">IDLE</abbr> last term.
- Why is it that all the <abbr title="integrated development environment">IDE</abbr>s I end up assigned do this by default?
- If I wanted to insert spaces, I'd hit the space bar repeatedly.
- Instead, I hit the tab key because I want <strong>*tabs*</strong>.
- I disabled the "Expand Tabs to Spaces" option, but that gave the tab key an incredibly bizarre behaviour.
- It inserted four spaces the first time you hit it, then turned the spaces into a tab the next time, then added four more spaces the next time, and so on.
- Next, I set the "Number of Spaces to Indent" option to zero.
- That ... kind of did what I wanted.
- The tab key no longer added spaces anywhere, but it would always indent to exactly the indention level the <abbr title="integrated development environment">IDE</abbr> assumed I'd want.
- Actually, it was pretty neat, aside from the fact I couldn't override it.
- Hitting the tab key again wouldn't do anything at all.
- I could only indent to exactly where the <abbr title="integrated development environment">IDE</abbr> thought I should.
- In most cases, the <abbr title="integrated development environment">IDE</abbr> will probably get it right, but there should always be a way for the programmer to type what they actually want as well, even when the <abbr title="integrated development environment">IDE</abbr> doesn't agree.
- My next try got it right.
- I set the space indention level to the same number as the tab width, and the tab key indented using tabs first to the level the <abbr title="integrated development environment">IDE</abbr> thought I wanted, then added further tabs when hit again.
- </p>
- <p>
- The first thing I noticed when working on the actual code is that I needed to square a number.
- I did some research, and found Java lacks an operator for this.
- My native language, <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, also lacks such an operator, but I just came from a course on Python, which does have an exponentiation operator.
- In any case, exponentiation can be performed with a function or simply emulated with basic multiplication.
- The multiplication option seems cleaner when working with small exponents, so I did that.
- The <a href="https://y.st./en/coursework/CS1102/GravityCalculator.java.xhtml">assignment</a> was easy enough to complete in a single sitting without testing until complete, but I'm not sure how to verify my answer.
- The code compiles and runs without error, but is the <strong>*semantics*</strong> of the code correct?
- It should be.
- It looks correct.
- The template NetBeans started me with had a couple mentions of the author of the code, which it set to my log in name, instead of the name I have set in my computer.
- I ended up correcting them to my preferred name, but since my preferred name isn't my birth name and University of the People knows my by my birth name for now (until I get it legally changed), other students won't recognise it.
- The coursework submission remains anonymous.
- </p>
- <p>
- I didn't do as well on the ungraded quiz as I'd like to have, but this week's not really turned out as planned, so it's not too surprising.
- Apparently, my terminology's a bit rusty, so I should've paid more attention to the reading assignment.
- </p>
- <p>
- The discussion board assignment this week didn't really present a complex enough subject facilitate meaningful interaction.
- Instead, we just covered the use of four Java operators that given the operator, the meaning of it was intuitive (if you've worked with other programming languages, anyway).
- There wasn't really anything for me to say about what other students posted, as we all basically said the same thing.
- Instead, I took the opportunity to introduce terminology to students that didn't seem to have heard the word "modulo" before.
- Thankfully, most students seemed to know about modulo division, but there were a few that talked about the goofy concept of a "remainder" from regular division.
- In division though, we don't have a remainder.
- We're not bound to just numbers that are integers, so there's no need for a "remainder".
- </p>
- <p>
- I used to know Java.
- I took a course on it at another university, but Java has certain problems that I really don't like.
- The most obvious is a feature called "overloading".
- Multiple methods can all share a name, as long as they don't share a signature.
- That is, if the name is identical, but parameters the method takes must be different.
- Personally, I think giving the same name to multiple methods is a very bad idea.
- However, it gets worse.
- When you accidentally pass the wrong variables into a method call or your variables don't have the types you thought they did, it causes <strong>*misleading*</strong> error messages to be thrown by the compiler.
- Instead of telling you you used bad values as arguments, the compiler complains that you called an undeclared method.
- For me, this makes me focus my bug search in the wrong places.
- I look for reasons why the method might not be defined by that point in the code yet.
- I look for typos in the method name, both in the method declaration and the method call.
- I check everything related to the method, and nothing related to the arguments, as the arguments weren't said to be the problem.
- After the course ended, I promptly dropped the Java language and never looked at it again.
- I don't remember how to write even basic Java applications.
- Now though, I'd like to get into mobile application development.
- I'm not sure I want to do it as a job or anything, but there's applications I lack on my Replicant device, and I'd like to be able to program what I need.
- That means figuring out how to use Java, as Replicant is an Android distribution and Android applications are developed in Java.
- In this course, I'm hoping to relearn Java, and this time, actually use it.
- As for the overloading issues ...
- If I can remember that a "missing method" error usually has nothing to do with the method name and everything to do with bad arguments, I might actually be able to get somewhere.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- The increment operator, <code>++</code>, simply increments the value stored in the variable it's used with by one.
- If the variable once held the value <code>2</code>, it'll now hold the value <code>3</code>.
- The variable can be placed to the left or to the right of the increment operator (<code>variable++</code> or <code>++variable</code>), but if the expression is a subexpression of a greater expression, order matters.
- This subexpression will return the value of the variable before or after the incrimination, respectively.
- </p>
- <p>
- The <code>&&</code> operator checks to see if two subexpressions are both true.
- If both ate true, the whole expression will be true, but if one or both are false, the expression will be false.
- </p>
- <p>
- The <code>!=</code> operator checks to see if two subexpressions are not equal to one another.
- If they're equal, the whole expression will be false, but the expression will be true otherwise.
- </p>
- <p>
- The <code>%=</code> operator performs modulo division using the value of the variable on the left as the first operand, and the subexpression on the right as the second.
- The result of this modulo division is then assigns the resulting value back to the variable on the left.
- </p>
- </blockquote>
- <blockquote>
- <p>
- The word "remainder" is kind of misleading.
- In real division, there is no remainder; you divide even that which doesn't divide evenly.
- For example, five divided by two isn't "two remainder one"; it's two and a half or two point five.
- What the <code>%</code> and <code>%=</code> operators do is perform modulo division, an operation that removes the evenly-divisible part and leaves the rest.
- It's like moving around something; it's like an analogue clock.
- If it's 3:00 and you move forward twenty-five hours, it's not 28:00.
- Instead, you perform modulo division on your result to get the real result.
- twenty-eight modulo twenty-four (or twelve, if you're on a twelve-hour clock) is four, so it's now 4:00.
- </p>
- </blockquote>
- <blockquote>
- <p>
- If there's a remainder, it implies you haven't actually used mathematical division.
- Mathematically, we have fractions and decimals.
- We don't need to neglect to divide the whole number.
- Five divided by two isn't "two remainder one", it's two and a half or two point five.
- It's very rare that we'd need to know what "can't" be divided.
- Instead, the <code>%</code> and <code>%=</code> operators are often used when we need to "wrap around" back to the beginning.
- For example <abbr title="rotate by 13 places">ROT13</abbr> is a weak encryption scheme that moves each of the 52 English letters forward by one space, but moves uppercase letters and lowercase letters separately.
- It's a variation of the Caesar Cipher.
- Each letter is part of a set of twenty-six, and is basically assigned a number. "A" is zero, "Z" is twenty-five, and the other uppercase letters fall between.
- <abbr title="rotate by 13 places">ROT13</abbr> moves each one forward by thirteen places.
- So what happens when you need to encode the letter "S"?
- The number for "S" is eighteen, so adding thirteen gives us thirty-one.
- There is no letter for number thirty-one, so what do we do?
- We have to wrap around, and that's what modulo division does.
- thirty-one modulo twenty-six is five, so <abbr title="rotate by 13 places">ROT13</abbr> encodes "S" as "F".
- </p>
- </blockquote>
- <blockquote>
- <p>
- Integers aren't the only numbers we have, so when we divide, there is no remainder.
- Five divided by two isn't "two remainder one", it's just two and a half or two point five.
- If we want to know what part doesn't divide evenly, it's not a "remainder" we're after.
- Instead, we're just looking for the solution to a modulo division problem; and that's what the <code>%</code> and <code>%=</code> operator are for.
- While five divided by two is two point five, five <strong>*modulo*</strong> two is one, which is the answer we were looking for.
- This also makes things easier to express.
- Mathematically, two point five is expressed as <code>2.5</code>.
- But how is "two remainder one" expressed?
- Even if we had a way to express it, when we actually needed that "remainder" alone (when programming), we'd need to find a way to parse it out of the whole answer.
- Additionally, the solution to a mathematical expression can always be dropped into another equation.
- For example, in <code>4 * (5/2)</code>, we divide five by two first.
- If five divided by two is two point five, the whole expression evaluates to ten.
- But what if five divided by two is "two remainder one"?
- How do we multiply "two remainder one" by four?
- </p>
- </blockquote>
- <h2 id="Unit2">Unit 2</h2>
- <p>
- The <a href="https://y.st./en/coursework/CS1102/SnakeEyesCount.java.xhtml">assignment</a> for the week said to use an <code>if</code> statement from within a loop, but a <code>do</code> <code>while</code> statement was a cleaner way to accomplish the goal.
- The test was built right into the loop, so no <code>break</code> statement was needed.
- That could go well for me, leaving me with cleaner code, or it could backfire on me, causing me to lose points on the assignment.
- I can't see the grading criteria until it's too late.
- I didn't get started on the assignment as soon as I should have, but thankfully, my code all came together with little hassle.
- I did hit a snag when I nested my initial code within the 1000-time run loop, but I just needed to move the division calculation to the end of the code, outside both loops.
- On the first run of the thousand-time version, the code output a perfect <code>36</code>, the exact average you'd expect.
- It didn't do that every time though, and once I added the code to convert the number to a float before dividing, the number was no longer perfect, but it still gets pretty close to the statistical average.
- </p>
- <p>
- I didn't have the level of focus I normally have for my coursework.
- One interaction in the forum showed me I don't understand enums like I thought I did.
- This shows me an area I need to study again.
- I also read feedback from our professor on my learning journal submission from last week.
- I've tried to keep this entry more brief than last week, as requested.
- </p>
- <p>
- My main attitude right now is one of lethargy.
- I'm not sure what my problem is this term, but both this week and last week, I've had issues with focus.
- I need to figure out where my enthusiasm went and get it back.
- I should've had this week's work done pretty quickly, but instead, I put it off until the last minute.
- That needs to stop.
- </p>
- <p>
- I suppose the main thing I relearned this week is how to properly terminate a statement.
- I keep leaving off the semicolons.
- In <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, my native language, we also use semicolons to terminate our statements, but I haven't had time to build anything in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> lately.
- Last term, I worked in Python for my coursework though, and statements there are terminated by ... line feeds.
- I've also been programming Minetest mods lately, and those are written in Lua.
- Lua also just uses line feeds to terminate statements.
- Python and Lua have made me soft.
- I've also learned I need to study better.
- I need to go back and review enums.
- </p>
- <p>
- My final thought for the week is that I'm confused by this <code>TextIO.java</code> file the assignment mentioned.
- It didn't seem to be necessary for the assignment, and was only mentioned as a hint, so I was able to move on without it.
- The <a href="https://y.st./en/coursework/CS1102/#Unit3">Unit 3</a> assignment mentions it as well, though this time, it includes the link to get it.
- I'm hoping it wasn't actually necessary for the Unit 2 assignment though, as we didn't have access to that file yet.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- A for loop loops over its code block until a counter-based conditional tells it to stop.
- This results is the code executing a predetermined number of times.
- A for loop might look something like this:
- </p>
- <blockquote>
- <pre><code>for(int i=0; i<32; i++) {
- int square = i*i;
- System.out.println("Squaring " + i + " gives us " + square + ".");
- }</code></pre>
- </blockquote>
- <p>
- It's worth noting though that all three statements in the for loop header are optional.
- <code>for(;;)</code> is perfectly valid.
- The counter could be initialised outside the header before the loop begins and the counter could be incremented within the loop.
- However, if you leave out the test condition, you've got no counter at all and the loop will continue until broken with a <code>break</code> statement or something similar.
- </p>
- <p>
- A for-each loop instead uses iteration.
- It executes its code block once for each item in the given enum, usually doing something with each item in the enum supplied.
- An enum is basically a data type for storing multiple values.
- In <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, this is comparable to an array, and in Python, this is comparable to a dictionary, tuple, or list.
- "Enum" is short for "enumerable", and enums can be enumerated with a for-each loop.
- Like with a for loop, the for-each loop will execute an expected number of times based on the number of items in the enum; once for each item.
- If we know how many elements are in the enum, we know how many times the block will be executed.
- A for-each loop might look like this:
- </p>
- <blockquote>
- <pre><code>String[] fruitlist = {"apple", "banana", "cherry", "date", "elderberry"}
- for(String fruit : fruitlist) {
- System.out.println(fruit);
- }</code></pre>
- </blockquote>
- <p>
- Some languages, such as <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, use a different key word for for loops and for-each loops.
- In <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, a for-each loop uses the <code>foreach</code> and <code>endforeach</code> key words, while a for loop uses the <code>for</code> and <code>endfor</code> key words for for loops.
- Java is not like this.
- In Java, the <code>for</code> key word is used for both types of loops, and the loop type is determined not exclusively by the key word chosen, but also by the structure of the loop's header.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You make a great point.
- Java enums don't use keys/value pairs.
- I suppose my comparison to constructs in other languages was confusing.
- My point had been that like these constructs from other languages, enums contain multiple values.
- </p>
- <p>
- The planetary example on <a href="https://docs.oracle.com./javase/tutorial/java/javaOO/enum.html">this page</a> makes it look like there's a way to store key/value pairs in an enum, though I'm a bit confused as to how it works.
- It's highly likely I'm misinterpreting the example.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You mention that a for loop can only be used when the index is required or the array needs to be modified.
- How is that?
- In your example, there was no array, so there were no indeces and no array elements to modify.
- </p>
- <p>
- Great explanation of enums!
- I didn't quite understand them as well as I should have from the readings.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You make a good point that the incrimination expression can either increment or decrement.
- It can also do neither, and the loop can rely on an expression within the loop to do that instead.
- </p>
- <p>
- However, a for each loop isn't always executed at least once.
- That would be a do while loop.
- A for each loop will be executed zero times if given an array of zero items.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You're right, the for each loop is more restrictive.
- It serves a very specialised purpose, looping through all elements of an array, so that's all it can do.
- A for loop is more generic, more generalised.
- Because of that, it has more power, but also more complexity.
- </p>
- </blockquote>
- <h2 id="Unit3">Unit 3</h2>
- <p>
- The <a href="https://y.st./en/coursework/CS1102/Unit3.java.xhtml">programming assignment</a> this week was easy enough, but we were required to use that strange <code>TextIO.java</code> file to read the text file we were given.
- As a result, we didn't actually learn how to read and parse a file ourselves in Java.
- For that reason, I'm not sure what we actually accomplished, and that disappoints me a bit.
- The comments of the <code>TextIO.java</code> file say that the class doesn't do things the correct or efficient way, so it's not even like we were supposed to read that file to learn how file-reading should be achieved.
- </p>
- <p>
- That said, I'm not sure about the other students, but I did learn a couple smaller things.
- First, I learned about the <code>equals()</code> method of the <code>String</code> class.
- It seems like such a small detail, but it'll be hugely invaluable in any program that needs to check for certain strings or compare strings, both of which are common use cases.
- Second, I skipped a typecast I felt was redundant and found something interesting. I tried to perform some maths involving <code>100.0</code>, then stick it in a <code>float</code>-type variable.
- That didn't work out so well.
- I figured that <code>100.0</code> was a <code>float</code>, so a mathematical expression involving it would also be a <code>float</code>.
- I started with a faulty premise though, so I ended up with a faulty conclusion.
- By default, decimal numbers aren't actually stored as <code>float</code>s, they're stored as more-precise <code>double</code>s!
- I considered adding in a typecast, but instead just switched the variable's type to match the expression's.
- More precision doesn't hurt, right?
- </p>
- <p>
- One of the most helpful interactions this week for me helped me understand why a <code>try</code> block requires a <code>catch</code> or <code>finally</code> block to be valid.
- I would think that sometimes you just want to catch exceptions to avoid program-crashing, but logically, you still need a <code>catch</code> block for that.
- Any exception of a class not specifically caught by a <code>catch</code> block causes a program halt.
- A <code>try</code> block without a <code>catch</code> block wouldn't surpress all exceptions; it'd fail to have anything to catch!
- Instead, an empty <code>catch</code> block would need to be added to the end of the <code>try</code> block, one specified to catch exceptions of the <code>Exception</code> class, the class all other exception classes descend from.
- Of course, in most cases, you don't want the <code>catch</code> block to be empty, you want it to actually do domething.
- However, the <strong>*theory*</strong> of an empty <code>catch</code> block can be used to understand why a <code>catch</code> block would even be necessary if empty.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- A try ... catch statement is a way to tell the program to brace itself for potential errors, as well as tell it what to do if those errors occur.
- Normally, an exception (what most object-oriented languages such as Java throw instead of raising "errors") will halt the execution of the program.
- An exception leave the program in a place it doesn't understand, unable to continue.
- By bracing itself to expect these errors, it's ready to handle them with the instructions you've provided for doing so.
- </p>
- <p>
- A try ... catch statement might look something like this:
- </p>
- <blockquote>
- <pre><code>double dub;
- try {
- dub = 3.0 / previously_defined_int;
- } catch(Exception e) {
- System.out.println("An error occurred. previously_defined_int was probably set to zero, and we can't divide by that.");
- dub = 0;
- }</code></pre>
- </blockquote>
- <p>
- In this case, we try to divide by an integer defined elsewhere in the program.
- The integer might be zero though, which it doesn't make sense to divide by because of some multiplication strangeness surrounding the number.
- We don't know for sure that the number is or is not zero when we write the code though, so we hope for the best, but prepare for the worst in case it doesn't work out.
- After catching the exception, we can output an error message (or not) and set our variables that would've been set had we succeeded.
- By setting our variables, we can limit the damage of failure, as later code that needed those variables will still have them.
- </p>
- </blockquote>
- <blockquote>
- <p>
- Yeah, you're right, I should've covered the fact that exceptions can be caught based on which class they belong to.
- In my example, I used the superclass that all exception classes descend from, which would catch all exceptions.
- That's not the only way to do it though.
- Multiple <code>catch</code> blocks could be specified, and each one could catch a different class of exception.
- Like you said, if done that way, any exception belonging to a class without a corresponding <code>catch</code> block will crash the program.
- One way to get around that if you wanted multiple <code>catch</code> blocks but didn't want the chance of unexpected exceptions crashing the program would be to write out your normal <code>catch</code> blocks, but then add one more <code>catch</code> block at the end that catches anything in the <code>Exception</code> class.
- </p>
- </blockquote>
- <blockquote>
- <p>
- When something exceptional and bad has happened is a great way to explain when exceptions should be thrown.
- Exceptions can be caught to prevent the execution of your program from terminating, but you don't even always want to do that.
- In many of my projects, an unexpected situation means I've done something wrong as the programmer, as these projects don't take in any outside data.
- In these types of programs, I throw exceptions when my program's in a state I assume it'll never be in.
- That way, when I run it, it'll crash and give me a stack trace.
- If it's in an unexpected place, I don't <strong>*want*</strong> it to finish what it's doing; I want it to exit immediately and tell me what went wrong.
- In other cases, I write a program that <strong>*does*</strong> take in outside data.
- In those cases, I like to throw exceptions when my code runs into a situation it doesn't understand.
- That way, I can add the necessary logic for handling that type of data, as I'll know what to add.
- For example, I built a Web spider, and it would find hyperlinks on a page and try loading the pages from those hyperlinks to try to find more hyperlinks.
- In two different occasions, I ran into problems with the <abbr title="Uniform Resource Identifier">URI</abbr> handler, and both times, purposely crashing the program helped me debug.
- In the first instance, it turned out the <abbr title="Uniform Resource Identifier">URI</abbr> parser I was using was buggy.
- It couldn't handle <abbr title="Uniform Resource Identifier">URI</abbr> references that contained only a fragment component if that fragment component contained a colon.
- It wasn't mine and I couldn't find a way to make it work properly, so I built my own to replace it.
- Without thrown exceptions though, I'd've just ended up with bad data in my database instead of even knowing there was an issue.
- In the second case, it turned out some Web author didn't know <abbr title="Extensible Hypertext Markup Language">XHTML</abbr>/<abbr title="Hypertext Markup Language">HTML</abbr>.
- They used a phrase instead of a <abbr title="Uniform Resource Identifier">URI</abbr> as the target of their hyperlink.
- My spider was ending up with nonsense data because of that, so I reworked the spider to log any invalid "<abbr title="Uniform Resource Identifier">URI</abbr>s" for manual review later (so I could check for false negatives) and had it avoid passing that bad data to the part of the code that would load the next page.
- Again, exceptions and stack traces were very helpful to the process, and in both cases, something exceptional and bad <strong>*had*</strong> occurred.
- </p>
- </blockquote>
- <blockquote>
- <p>
- It's worth noting that a <code>catch</code> block doesn't <strong>*have*</strong> to print out anything in regards to the exception.
- Often times, I don't use <code>try</code>/<code>catch</code> blocks because my goal in throwing exceptions is to <strong>*cause*</strong> the program to halt.
- When I use <code>try</code>/<code>catch</code> blocks, it's because I'm expecting known problems that I can't solve.
- For example, I would use <code>try</code>/<code>catch</code> blocks when processing user data.
- Users can never be trusted, so I expect them to input erroneous data sometimes.
- If processing a large amount of input files, I don't want all the errors popping up, so I silently catch the exceptions that might get thrown, and maybe log them in some way.
- I don't output any information about the exception to anything visible to the user though.
- In some cases, you <strong>*do*</strong> want to output error messages and move on, but the outputting of error messages isn't a requirement.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I hadn't thought about it before, but it makes sense that a <code>try</code> block requires a <code>catch</code> block or <code>finally</code> block.
- A <code>catch</code> block has the possibility of getting the program back on track after an exception has been thrown.
- Even without a <code>catch</code> block though, a <code>try</code> block can have purpose when combined with a <code>finally</code> block.
- The <code>finally</code> block can perform necessary cleanup tasks before the program finishes aborting.
- But what would a <code>try</code> block do without either of these two other block types?
- It wouldn't be any different semantically than just having that same code without the block, so it's not allowed.
- </p>
- </blockquote>
- <h2 id="Unit4">Unit 4</h2>
- <p>
- While I was writing up my initial discussion post for this course, I ran into another idiocy in Java.
- This time, it's not in Java itself, but in the terminology that surrounds it.
- We all know what parameters and arguments are in regards to functions and methods, right?
- Well, Java mixes it up a bit just to be confusing.
- What everyone else calls a "parameter", Java calls a "formal parameter" (or "dummy parameter").
- Meanwhile, what everyone else calls an "argument", Java calls an "actual parameter" (or "argument", like everyone else).
- In other words, Java needlessly makes the word "parameter" ambiguous.
- In order to be clear about what you mean, the word "parameter" can never be used alone.
- It must always be qualified with the word "formal", "dummy", or "actual".
- Additionally, Java redefines the word "function".
- As far as I can tell, the definition of the word "method" in Java is the same as in other languages.
- However, other languages define a "function" as being a method that's not bound to an object class.
- Java instead redefines it as a method with a return value.
- Why isn't Java using the same, standard terminology as everyone else?
- I can't see a single good reason for Java having redefined standard programming terminology like this.
- </p>
- <p>
- One thing I learned this week is how single quotes and double quotes in Java are used.
- In some programming languages, they're used interchangeably.
- This is the case in Python.
- In other languages, they behave differently, but both delimit strings.
- This is the case in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>.
- However, Java has more data types.
- To delimit strings, double quotes are used, and to delimit characters, single quotes are used.
- I also relearned what static method calls are.
- I should know that already, as I deal with the same construct when I use <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, which I do on a daily basis.
- However, when I was drafting my programming assignment this week, I ran into an error involving the static calling of methods, and instead of jumping right to the obvious fix, I tried a bunch of things that made zero sense to even try.
- While Java does redefine several key pieces of programming terminology, static method calls aren't one of them.
- I should've known exactly what the problem was based on the error message without any guessing.
- </p>
- <p>
- My main interaction this week was probably the one in which a student said I probably should've provided example code to go with my explanation.
- I should probably do this in general with all my posts in programming courses.
- Many programming concepts are very familiar to me because I've been using them for years, so I forget that other people might need an example to clear up what is meant when I explain these simple concepts, such as those of (formal) parameters and arguments.
- I also got something out of the grading this week.
- Two out of three of the assignments I graded didn't just assume the required database file they required existed.
- They accounted for the possibility that it wouldn't, providing an error message and aborting if that was the case.
- Honestly, why <strong>*didn't*</strong> I do this?
- That's one of those bare minimum things you do when dealing with user input, such as this file.
- You don't assume the data exists, you don't assume the data is valid, and you don't assume the data is safe.
- In the case of last week's assignment, invalid/"unsafe" data simply results in a bad count.
- No arbitrary executions should happen.
- However, the threat of the file being missing is still very real and needs to be dealt with.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- Parameters are basically local variable names within a method that don't have any value associated with them until the method is called.
- When a method is called with arguments, the values of the arguments are assigned to the names given by the parameters.
- In this way, a method call can influence some of the internal aspects of the method.
- The readings suggest that parameters don't just apply to methods though.
- They may be present in any type of subroutine definition.
- I'm not yet sure what subroutines besides methods exist in Java yet.
- Arguments are values without names that will be passed into a subroutine and assigned to the names given by the parameters to form full local variables with name and value that can be used within the subroutine.
- That allows us to define the subroutine to make use of those local variables by name without knowing when we define the method what the values passed in will be yet.
- It's worth elaborating too what I mean when I say the value passed in has no name.
- You can use an expression in the method call, and that expression might simply be a named variable.
- The value just doesn't have a name within the method until passed as an argument, but in may or may not have a name outside the method.
- </p>
- <p>
- The concept of "formal parameters" and "actual parameters" is a strange one.
- In most languages, the word "parameter" refers to the name declared in the function/method definition, and that's the way I used the word in the preceding paragraph.
- However, in Java, these are referred to as either "formal parameters" or "dummy parameters".
- In Java, the arguments I mentioned in the above paragraph are called either "arguments" (as I called them) or "actual parameters".
- That is to say, when speaking about method definitions and calls in Java, the word "parameters" is ambiguous.
- If you're coming from having worked with other programming languages, this is a bit frustrating and counter-intuitive.
- </p>
- <p>
- Eck, D. J. (2016, December). Javanotes 7.0, Chapter 4 -- Programming in the Large I: Subroutines. Retrieved from <a href="http://math.hws.edu./javanotes/c4/"><code>http://math.hws.edu./javanotes/c4/</code></a>
- </p>
- </blockquote>
- <blockquote>
- <p>
- Yeah, I probably should've included an example.
- I also should've mentioned that (formal) parameters include a type declaration.
- If an argument of the wrong type is passed into the method, Java will look for a different method.
- The readings this week said that two methods can't share a name, but in Java, that's annoyingly false.
- My least favourite thing about the Java language is that two methods <strong>*can*</strong> have the same name, provided the number of arguments or the types of the arguments they take are different.
- The reason I hate this feature so much is that it makes debugging a huge pain in the neck.
- The Java compiler will complain that you are attempting to call undefined methods, so you try to find typos in the method name in the method definition and the method call, but there's nothing wrong there.
- Instead, the real problem is that you passed bad <strong>*arguments*</strong>; arguments of the wrong type.
- This could be because you passed the wrong variable as an argument or it could be that you you thought the variable you passed was of a different type than it really was.
- An <abbr title="integrated development environment">IDE</abbr> makes finding the real source of the problem much easier, but honestly, if you need an <abbr title="integrated development environment">IDE</abbr> to get the job done instead of just basic syntax highlighting, your language is being difficult.
- To be clear, I'm not saying use of <abbr title="integrated development environment">IDE</abbr>s is bad, just that use of <abbr title="integrated development environment">IDE</abbr>s should provide an extra layer of ease, not the basic level needed to get anything done efficiently at all; <abbr title="integrated development environment">IDE</abbr>s should be optional, not for all practical purposes necessary.
- </p>
- <p>
- As an example of (formal) parameters, arguments, and methods with the same name, see this class:
- </p>
- <blockquote>
- <pre><code>/*
- * Copyright (C) 2017 Alex Yst
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package cs1102;
- /**
- *
- * @author Alex Yst
- */
- public class Unit4 {
- public static void test(int parameter) {
- System.out.println("The integer version of this method was called.");
- }
- public static void test(String parameter) {
- System.out.println("The string version of this method was called.");
- }
- public static void main(String[] arguments) {
- System.out.println("This will call the integer version of the method.");
- test(-7);
- System.out.println("This will call the string version of the method.");
- test("This is a string.");
- }
- }</code></pre>
- </blockquote>
- <p>
- In this example, we see that each method defines the types of the arguments it takes.
- The names assigned to these arguments, as well as the declared types, are the (formal) parameters.
- The values used in the method calls are the parameters.
- Additionally, we see two methods both named <code>test()</code>.
- Java allows this, but only if both (or all, if more than two share a name) take different argument types and/or a different number of arguments.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I like how you explained that.
- I completely agree.
- (Formal) Parameters are like variable names and type declarations.
- Parameters are just the values assigned to those variable names.
- That's why within the method, we can use them just like any other local variable: they <strong>*are*</strong> just local variables within the method.
- Anything you can do with a variable, you can do within the function with the (formal) parameter.
- You can even assign it a new value!
- </p>
- </blockquote>
- <blockquote>
- <p>
- Code reuse through defined methods is a very powerful tool.
- For starters, it allows us to be a bit less verbose in our code.
- We don't have to copy and paste the same thing in multiple places.
- Even better, when we update the code, we can update it only in the method, instead of in several places, as we didn't just paste the code everywhere we needed it.
- Code reuse is made easier in other projects, too.
- Our class can be included in another program, and that program can pass the values it needs to into our methods.
- It allows us to build more general software to fill a larger set of needs.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I guess (formal) parameters can be thought of as placeholders.
- Any variable can, really.
- Instead of directly using the value we intend to, we assign it to a variable, then use the variable in our computations.
- The same applies to (formal) parameters and arguments.
- The (formal) parameter declares the type and name of the variable to be used locally within the method.
- When the method's called, but argument assigns a value to the (formal) parameter; that is to say, it assigns a value to the local variable defined by the (formal) parameter in the method definition.
- </p>
- </blockquote>
- <h2 id="Unit5">Unit 5</h2>
- <p>
- I read over the two programming assignments this week before getting started on the reading assignment.
- I was very confused at first.
- One programming assignment asks us to write a <code>main()</code> method that defines a particular class, then instantiate that class.
- A method ... that defines the class?
- I thought the assignment was misworded, as I seem to recall few programming assignments in another course, <a href="https://y.st./en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a>, being worded in ways that made no sense at all.
- I had make guesses as to what was actually required sometimes.
- Obviously, this assignment was one like those.
- I needed to do one of two things and I wasn't sure which.
- One option was to define the class as specified, then add a <code>main()</code> to it that causes it to instantiate itself and call its own methods.
- This option seemed the most convoluted in a way, but also seemed to better fir the directions.
- The second option was to not define the class in the same file even; just include and use it the way we included and used the <code>TextIO</code> class in a <a href="https://y.st./en/coursework/CS1102/Unit3.java.xhtml">previous assignment</a>.
- I was wrong though.
- The assignment made perfect sense if you know how Java actually works.
- In <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, you can define classes from within the methods of other classes.
- However, Java's a bit strange in that you can't even have code that runs outside a method.
- Everything you program in Java is a class definitions, essentially.
- As such, I expected class definitions to be required to be more ... I don't know, grounded.
- As in, they had to be defined in the outermost scope.
- Additionally, I though Java had a one-class-per-file rule, though perhaps that's just convention.
- In any case, I learned that classes in Java can in fact be defined from within methods of other classes.
- The assignment makes perfect sense.
- </p>
- <p>
- Another thing I learned was how to set up a constructor method.
- The constructor method is simply a method sharing its name with its class.
- This method should return no value.
- Maybe <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> got this convention from Java!
- In the past, <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> also used constructor methods named after the class they instantiate, though this was later replaced with the <code>__construct()</code> method.
- Java's option to give multiple methods the same exact name as long as they take different arguments is a major pain in my neck when debugging.
- In most use cases, I see this feature as providing zero useful value and only causing problems.
- However, constructor methods are the only exception to this.
- When passing different argument types to a method, you might as well use a different method name for clarity, especially when using variables to pass the values.
- However, when constructing an object, you may want to be able to do so in a variety of ways.
- Having multiple constructor methods makes this possible.
- </p>
- <p>
- I guess my main interaction this week was when someone said that I was right that understanding the difference between a class and an object is difficult.
- I never said that though!
- Understanding the difference between a class and an object is as easy as understanding the difference between the integer <code>-3</code> and the data type <code>int</code>.
- Honestly, you shouldn't be mixing those two up.
- Would you try to solve an equation that looked like <code>int + 5 * int</code>?
- No, because the data type isn't a valid value.
- The same is true of objects and classes.
- You don't perform operations on a class, as a class is only a group, a data type.
- Objects are your actual data.
- </p>
- <p>
- My final thought for the week is about the second programming assignment for the week, the multi-week project.
- The project submission form only allowed the upload of four files, but there were five classes to upload:
- </p>
- <ul>
- <li>
- <code>Employee</code>
- </li>
- <li>
- <code>FullTime</code>
- </li>
- <li>
- <code>PartTime</code>
- </li>
- <li>
- <code>PayrollSystem</code>
- </li>
- <li>
- <code>Vehicle</code>
- </li>
- </ul>
- <p>
- I left out the <code>PayrollSystem</code> because of the upload limit, which hopefully won't be a problem.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- A class is a blueprint with which objects can be built.
- The class defines the methods and properties that will be available to the objects created with it.
- In this way, a class is almost like a type definition, allowing you to define new data types in Java.
- </p>
- <p>
- An object is a value created from a class.
- All objects belong to a particular class, and the class determines what the object can and cannot do.
- </p>
- <p>
- A method is a bit harder to explain in Java terminology because Java redefines the standard programming terms.
- Normally, I'd say it's a function bound to an object class, but Java redefines the word "function", making that definition incorrect.
- I think the best way to describe it would be that it's a named subroutine that is bound to a particular object class.
- If the method is static, it can be called directly without an object.
- However, if the method isn't static, an object of that class must be created and the method called using that object.
- </p>
- <p>
- Attributes are sort of like subvalues within an object.
- The attribute name acts as a key, forming a key/value pair with the attribute value.
- The class defines what properties (both by name and by type) an object will have, but the values of the properties can differ between objects of the same class.
- </p>
- <p>
- Finally, we come to instantiation.
- Instantiation is the process of creating an instance of a class; this instance is what we call an object.
- In Java (and many other languages),
- </p>
- <p>
- The discussion assignment says to discuss a practical example of these Java concepts from experience, but I don't have practical experience in Java.
- The only things I've written in Java were for assignments in this course and for another course at another school.
- All of these concepts apply to <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> though, and I do have practical experience in that, so I'll discuss how I used them there.
- </p>
- <p>
- I was writing a Web crawler and ran into an issue with the built-in <abbr title="Uniform Resource Identifier">URI</abbr> parser of the language.
- Under certain circumstances, the built-in subroutine'd fail, and as my spider expected reasonable data, it'd throw an exception and crash when it instead got absolutely no data at all returned from the subroutine.
- At first, I build a suite of subroutines for processing <abbr title="Uniform Resource Identifier">URI</abbr>s as strings and as arrays of strings, as well as subroutines for converting between the two forms.
- I soon realised though that because <abbr title="Uniform Resource Identifier">URI</abbr>s have their own grammar and syntax, they're basically a specialised data type ans should be treated as such.
- I lumped the subroutines into a single class and reworked them to be object methods.
- </p>
- <p>
- This class is called <code>\st\y\uri</code>, though had it been written in Java, It've been called <code>st.y.URI</code>.
- Whenever I need to process a <abbr title="Uniform Resource Identifier">URI</abbr>, I instantiate a new object from this class.
- The constructor method takes a <abbr title="Uniform Resource Identifier">URI</abbr> string as an argument, parses it, and sets the appropriate private attributes.
- In the process, it normalises the <abbr title="Uniform Resource Identifier">URI</abbr> components and insures they're valid.
- If invalid data isused in the constructor, an exception is thrown.
- Getters are used to read any properties needed, while setters normalise their input before setting the properties.
- Additionally, again, invalid input into the setters will cause exceptions to be thrown.
- For storage of <abbr title="Uniform Resource Identifier">URI</abbr>s, a method is provided to convert these <abbr title="Uniform Resource Identifier">URI</abbr> objects back into a (now-normalised) string, and child classes can define other processing methods as needed, depending on the subtype of <abbr title="Uniform Resource Identifier">URI</abbr> being processed.
- I now treat <abbr title="Uniform Resource Identifier">URI</abbr>s as their own data type and this data type behaves as you'd think it should.
- </p>
- <p>
- I've also been know to extend built-in classes to provide more intuitive constructors and/or more methods for more-specific use cases.
- One of the great things about object-oriented programming is that you can reuse the functionality of an existing class as a base for adding functionality to when defining your own class.
- You don't need to rewrite existing functionality to add it to a new class.
- Additionally, objects of the child class are considered to be both of the child class (mainly) and of the parent class.
- That makes objects of your new extended classes drop-in replacements for objects of the parent class!
- In Java, like in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, multiple inheritance isn't an option though.
- Because classes cant inherit from two or more parent classes, a child class can't be used to create objects that'll act as drop-in replacements for objects of multiple other classes (unless all those classes are part of the same inheritance chain) and if you need to combine the functionality of multiple classes, your new class will need to re-implement the methods of all but one of the classes you need your class to imitate.
- </p>
- </blockquote>
- <blockquote>
- <p>
- It's not actually all that difficult to understand the differences between classes and objects.
- A class is like a variable type.
- An object is an instance of that type.
- For example, <code>5</code> would be like an instance of a hypothetical <code>int</code> class.
- The object is the value.
- <code>int example = int + int;</code> wouldn't make any sense.
- Instead, you need the instances of <code>int</code> for the right side of the expression: <code>int example = 3 + 5;</code>.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I wouldn't say there's no point in defining a class without instantiating any objects from it.
- That statement may be true for some languages, but not others.
- In my native language, <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, a class definition can be used to make functions and constants autoloadable.
- They can also be used to create new variable name spaces.
- These aren't uses I actually make use of, but they could be useful to some programmers.
- As for Java, Java won't run without a class.
- For a Java program to function, a class must be defined with a static <code>main()</code> method.
- No class means no execution.
- </p>
- </blockquote>
- <blockquote>
- <p>
- It's true that there's a difference between objects and non-object values, but at the same time, their example is useful.
- Objects share many qualities with non-object values.
- <code>int</code> in their example is very much like a class of non-object values, and <code>9</code> is like an instance of it.
- Or to put it better, a class is like a value type and an object is like a value of that type.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I like how you bring up that a class should focus on one particular type of task.
- While a class could be designed to tackle the entire problem solved by the program, it's a messy way to build your code.
- If each class focuses on a single type of task, the code will be easier to build, easier to test, easier to read, and easier to understand.
- Focus each class on a single task and have the class perform that task well.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I don't think the purpose of a static variable is to increase memory efficiency.
- It's true that this results in the value only being stored in one place in memory, but even with an unlimited amount of memory, this would be useful in the same situations as now.
- Specifically, a static variable allows all instances of the class to share the same value.
- If one object changes the value, all other objects of the same class know about the change immediately.
- It might not be possible to check the variable space and find all instances of the class, just to find out if one object changed the value, then update all other objects to match.
- When the value needs to be recorded independently of all instances and shared between instances, that's when a static variable comes in handy.
- </p>
- </blockquote>
- <h2 id="Unit6">Unit 6</h2>
- <p>
- My main interactions of the week were probably through grading assignments.
- Two of the three students I graded for wrote up their assignment submissions for last week not to use a static set of numbers, but to take user input.
- That was awesome, and I wish I'd done the same.
- However, their implementation could use some work.
- Both students used the number zero as a terminator.
- The user could input as many numbers as they liked, as long as none of the numbers were zero, then enter a zero to indicate that the full set had been input.
- What if a user needed to add a zero or zeros to their set?
- These programs wouldn't allow it.
- The third student instead defined an array of the numbers we were told to use for the assignment, then looped through them, providing a more concise solution than mine, though with the same static result.
- What I especially liked about this solution wasn't its conciseness; I'd considered the same idea myself and decided against it.
- Instead, what I liked was how it used the loop not only to add the numbers to the <code>StatsCalc</code> object, but also to output the numbers visually.
- I hard-coded each number in two places; one that displayed the set of numbers and one to add the numbers to the object.
- If I wanted to update my code, I'd need to remember to do so in two places.
- This student's code removed that problem, and only had the numbers hard-coded in one spot without any loss of output.
- </p>
- <p>
- I continued the multi-week programming assignment.
- I wanted to add helpful comments, but the code doesn't even do anything yet.
- I know how the classes operate, but not how they're actually used.
- I'll have to wait until next week to know what comments to add.
- Again though, the assignment submission form didn't allow me to upload the whole project.
- The upload limit was cut in half from last week too, so I was only able to upload two classes.
- I chose <code>FullTime</code> and <code>PartTime</code>.
- As before though, I included all five classes using <abbr title="Extensible Hypertext Markup Language">XHTML</abbr> in the text area of the form, so hopefully that won't be a problem.
- </p>
- <p>
- The main assignment for the week was a mess.
- We were to define a <code>getVolume()</code> method that returns no value, but outputs the width, height, depth, weight, and volume to the output buffer.
- Weight isn't even related to volume, so it shouldn't be output by the <code>getVolume()</code> method.
- Plus, because the output is to the buffer instead of being returned by the method, it can't be used elsewhere in a program.
- Additionally, we were to define a <code>weight</code> property and a <code>calculateWeight()</code> method. This method was to calculate the weight and, again, output it to the buffer instead of returning it.
- Since the weight should only be output once, I ended up calling this method from within <code>getVolume()</code>, making it so <code>getVolume()</code> output the weight by calling <code>calculateWeight()</code>.
- I also had <code>calculateWeight()</code> set the value of the <code>weight</code> property, though this property isn't actually used by any other method.
- It's a useless property.
- I originally had my code calculate the volume as well, but then deleted that code and replaced it to a call to the superclass' <code>getVolume()</code>.
- The superclass we were assigned to work with still output the information to the buffer instead of returning it, but at least it output only the volume, truer to the method name.
- Our child class is supposed override that method to output five statistics instead; we redefine what the method is even supposed to output.
- As a practice project for learning concepts, this assignment is just fine.
- However, if we were developing this class for an employer, we should be fired.
- Finally, our <code>main</code> class' <code>main()</code> method is supposed to output the five statistics, but the instructions specifically state to have the <code>getVolume()</code> method of the <code>MatchBox</code> class output that information.
- That means we either output the information twice or we have the <code>main()</code> method output it by calling the <code>getVolume()</code> method.
- The obvious choice is the latter, as it provides cleaner, unduplicated output, but that means our <code>main()</code> method is composed of two lines.
- To kind of show a point, I merged the lines into one, which worked just as well.
- Literally, the only line you need in that method is <code>new MatchBox(5, 10, 3).getVolume();</code>.
- I could've provided an option to take user input to decide the dimensions (and perhaps the density) of the box, but I decided the one-liner with explanation comment was a better route.
- I've been learning from the code of other students, and I hope they're learning from me.
- I think this version of my code has more to offer education-wise in exchange.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- Polymorphism is the ability to have multiple classes of objects that meet the type requirements of a single variable type.
- Java is a strongly-typed language, so all variables are of a specific type.
- Each class is a type, so a variable meant to hold an <code>exampleZero</code> object (declared with <code>exampleZero var0;</code>) can't contain a reference to an object of the <code>exampleOne</code> class.
- At least usually.
- However, with polymorphism, this becomes possible in some situations.
- For example, if the <code>exampleOne</code> class is a child class of the <code>exampleZero</code> class, <code>var0</code> could hold an object of the <code>exampleOne</code> class because objects of that class <strong>*also*</strong> belong to the parent class.
- This is useful in a number of situations.
- </p>
- <p>
- First, we have the case of direct inheritance.
- I've written a class called <code>st.y.uri</code>.
- This class parses, normalises, and enforces the syntactic validity of <abbr title="Uniform Resource Identifier">URI</abbr>s in accordance with <a href="https://tools.ietf.org/html/rfc3986"><abbr title="Request for Comments">RFC</abbr> 3986</a>.
- By making the most important methods final, I ensure that this class and all child classes adhere to this standard.
- However, for example, <code>telnet:</code>-scheme <abbr title="Uniform Resource Identifier">URI</abbr>s must not only adhere to <abbr title="Request for Comments">RFC</abbr> 3986, but also <a href="https://tools.ietf.org/html/rfc4248"><abbr title="Request for Comments">RFC</abbr> 4248</a>.
- If I specifically want to ensure a <code>telnet:</code>-scheme <abbr title="Uniform Resource Identifier">URI</abbr> is syntactically valid and normalised, I use one of the child classes: <code>st.y.uri.telnet</code>.
- I still might want to use objects of this child class with a method that requires an <code>st.y.uri</code> instance as an argument though, and because of polymorphism, I can do that.
- (Remember that a (formal) parameter and an argument combine to form what is essentially a local variable.)
- The method signature requests an object of type <code>st.y.uri</code>, but my object of class <code>st.y.uri.telnet</code> is of two types: <code>st.y.uri.telnet</code> and (because it's a child class of it) <code>st.y.uri</code>.
- Had my class been written in Java (it's actually written in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>), this object would additionally be of a third type, the <code>object</code> type.
- </p>
- <p>
- As a quick example, this code should work fine:
- </p>
- <blockquote>
- <pre><code>public class Unit6_parent {
- public void parent_method() {
- System.out.println("Parent method called.")
- }
- }
- public class Unit6_child extends Unit6_parent {
- public void child_method() {
- System.out.println("Child method called.")
- }
- }
- public class Unit6 {
- public static void use_object(Unit6_parent object) {
- object.parent_method();
- }
- public static void main(String[] arguments) {
- Unit6_child object = new Unit6_child();
- use_object(object);
- object.child_method();
- }
- }</code></pre>
- </blockquote>
- <p>
- In this example, the <code>use_object()</code> method expects an object of type <code>parent_class</code>.
- When it gets an object of type <code>child_class</code>, that works too, as objects of type <code>child_class</code> <strong>*are*</strong> objects of type <code>parent_class</code>.
- Objects are of multuple types; a type for their own class and a type for each of their ancestor classes (not just their direct parent class).
- </p>
- <p>
- The second important use for this is pretty much the only reason interfaces exist.
- An interface allows you to define the <abbr title="application programming interface">API</abbr> your methods will use while not defining the implementation.
- This is more useful for libraries and other code that doesn't do anything on its own.
- An interface demands that implementing classes implement methods with specific method signatures.
- Objects of classes implementing interfaces then take on the types of the implemented interfaces, in addition to the type of their own class and types of their ancestor classes.
- This allows you to write a method that takes an object of type <code>exampleInterface</code> and call that objects <code>exampleInterfaceMethod()</code> method, provided the <code>exampleInterface</code> interface requires implementing classes to implement that method.
- The object's implementing class then decides what to actually do with that method.
- </p>
- <p>
- I'm told this is really useful for database extraction, though I've never had any luck getting database extraction to work well.
- It's something I need more practice at, as well as perhaps more education about.
- An interface is useful when you need an object to do something on a high level, but you don't know or care about the low-level implementation.
- This allows multiple, very different, low-level implementations of the same concept for different use cases.
- </p>
- </blockquote>
- <blockquote>
- <p>
- There are a lot of differences, but some of those differences are effectively disappearing.
- For example, the newer <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> versions have the capability to require function/method arguments to have specific types like Java always requires.
- I haven't gotten to work with that yet, as my <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> version's too old, but next time my operating system has a new release (Debian 9!), I'll have <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> 7.* at my disposal.
- I'm going to love adding type declarations to all my (formal) parameters.
- </p>
- </blockquote>
- <blockquote>
- <p>
- Thank you!
- I try to give full acronym names when platforms support it.
- I often run across acronyms I don't know, so I try not to make other people look up the ones I use.
- </p>
- <p>My sample code <strong>*was*</strong> in Java though.
- That code would never run in <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>.
- </p>
- </blockquote>
- <blockquote>
- <p>
- Thanks for sharing an example of method overloading with us!
- Overloading is something I struggle with.
- The concept is easy enough to grasp, but having multiple methods with the same name like that makes it difficult for me to debug.
- It's something I need to work on.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You mention having multiple constructors as a form of polymorphism.
- I hadn't thought about it that way, but you're right.
- Method overloading in Java is having multiple methods of the same class with the same name but with different (formal) parameters.
- Having multiple constructors is an example of this.
- I hadn't considered it before, but having multiple versions of a method like that would certainly be a form of polymorphism if I've ever seen one.
- Thank you for the insight!
- </p>
- </blockquote>
- <blockquote>
- <p>
- That's a great real-world example of how one object can be multiple things or fit into multiple categories.
- I tried to explain that in my post, but I don't think I did it as effectively as you did.
- Your code example was also funny, seeing as you used <code>foo</code> both as being short for "food" and as one of the common example variable names people use.
- </p>
- </blockquote>
- <h2 id="Unit7">Unit 7</h2>
- <p>
- The main programming assignment had an interesting twist to it, if you actually look at how two-dimensional arrays in Java work.
- A two-dimensional array is just an array of arrays, obviously.
- However, we're using two-dimensional arrays as matrices.
- The arrays in the array aren't guaranteed to be the same length as one another!
- The assignment asks us to make sure the matrix is square.
- If not, the matrix should be rejected and an error message output.
- We are to use two variables, <code>numberOfRows</code> and <code>numberOfColumns</code>, to compare the dimensions of the matrix.
- This assumes that the matrix is even rectangular though!
- Take for instance this two-dimensional array:
- </p>
- <blockquote>
- <pre><code>int[][] = {
- {0,1,2},
- {3},
- {4,5}
- };</code></pre>
- </blockquote>
- <p>
- If we try to just compare the length with the width, we'd probably check just the width of the first row, as (unless we have a zero by zero matrix, another corner case we should check for), it'll look like a standard 3*3 matrix!
- No, what we need to do is loop through the first dimension and check the width of each row.
- As we're supposed to use the <code>numberOfColumns</code> variable, we can set that variable each time we look at the next row, then compare it to <code>numberOfRows</code> before moving to the next row.
- Making sure I was accounting for corner cases was the bulk of the assignment for me.
- The actual computation after being sure we had a valid square matrix was easy.
- The row key and the column key will always intuitively add up to the same number because we're looking at the secondary diagonal.
- (Had we instead been looking at the first diagonal, the row key and column key would intuitively equal one another instead.)
- All reasonable programming languages properly index from zero, so the two keys combined add up to one less than the array length.
- Just go through the keys of the rows, always subtracting the row key and one from the length to get the column key.
- </p>
- <p>
- On the other programming assignment, the interface of the application we build doesn't match the internal terminology.
- For example, we prompt the user for an employee's bonus, but save it to <code>hourAndHalf</code>.
- Hour and a half is overtime; hours spent on the clock beyond a certain threshold (I believe forty hours per week, here in the States).
- A bonus is extra money for good performance, not at all related to hours worked.
- You can work fewer than the prescribed number of hours and still receive a bonus, if you work hard enough and/or your efforts are appreciated.
- I'm also failing to understand the purpose of this assignment.
- I think this is the final week for this assignment, and all we've done is copy code from screenshots.
- We didn't design anything, nor did we write code based on the designs of someone else.
- We just verbatim copied text from images.
- Personally, I also re-factored the code, making it (in my opinion) better without changing the output.
- However, that wasn't actually part of the assignment, so it doesn't really qualify as a purpose of what we did.
- I'm not sure what the deal is, but this week, we're only allowed to upload three of our five files, according to the submission form.
- I was going to upload <code>Employee</code> and its two subclasses, but this week, I have JavaScript disabled.
- There's a major bug in the University of the People website, and it causes the site to constantly lock up.
- The only method I've found for getting around this is to disable JavaScript in my Web browser.
- I think this is a great example of poor Web design.
- With JavaScript disabled though, it seems the submission form didn't accept any of my uploaded files, but at least the whole project was accepted in the <abbr title="Extensible Hypertext Markup Language">XHTML</abbr>-enabled text area.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- In many languages, such as Python, <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>, and Lua, arrays do not have a fixed size.
- Elements can be added to and removed from the array at will and as needed.
- However, standard Java arrays don't have this property.
- Instead, they're of a fixed size, and elements are decided when the array is introduced.
- The value contained in each element can be modified, but new elements cannot be added and old elements cannot be removed.
- </p>
- <p>
- In Java, dynamic arrays fix this problem.
- This type of array doesn't have a fixed number of elements, so the array can be adjusted at run-time as needed.
- As far as I can tell, there's no actual data structure in Java that can do this.
- However, Java is a functioning object-oriented language.
- What this means is that we can emulate new data types, such as dynamic arrays, as classes of objects.
- It seems Java even has a built-in class that implements this: <code>ArrayList</code>.
- The advantage of this is obvious: we don't need to know how many elements we need before we start.
- A fixed-size array must have its size declared right away, and we'll most likely get the size wrong if we don't know beforehand what size to use.
- If we make the size too big, we uselessly waste system memory; our program won't be very efficient.
- If we make the array size too small, we can't handle all the data; our program won't be very effective.
- With dynamic arrays, such as those implemented by the <code>ArrayList</code> class, we have perfect-sized data structures at all times.
- As new information is added or old information removed, the <code>ArrayList</code> adjusts to conform to our changing needs.
- </p>
- </blockquote>
- <blockquote>
- <p>
- In Java, a key word is something such as <code>new</code> or <code>public</code>.
- It's a language construct build from letters instead of non-alphanumeric characters.
- They're similar to operators or curly braces, and are built into the language; they don't exist in packages.
- <code>ArrayList</code> is instead a class, which is why it's instantiated with the <code>new</code> key word.
- </p>
- <p>
- It's interesting that the <code>ArrayList</code> class can't handle primitive data types.
- I wonder why that design decision was made.
- </p>
- </blockquote>
- <blockquote>
- <p>
- Thanks for the great example!
- I wasn't sure if you meant that the <code>add()</code> method, when given an index, overwrote the value already at that value or if you meant the value already there got pushed aside to the adjacent index, pushing all later elements forward by one.
- Your example makes it clear that the <code>ArrayList</code> does indeed grow, and the elements are pushed aside as needed.
- </p>
- </blockquote>
- <blockquote>
- <p>
- That's a great example of why large arrays are a bad idea in comparison to dynamic arrays, when dynamic arrays are the right tool for the job.
- You mention that it saves memory, and depending on the program, memory might be a scarce resource.
- It's worth noting though that your user might run your program alongside other applications.
- Memory saving doesn't just help your program function, it also helps your program play nicely with other software the user might be running at the time your program is run.
- </p>
- </blockquote>
- <h2 id="Unit8">Unit 8</h2>
- <p>
- My main interaction this week was through the grading of assignments.
- No one seemed to figure out how to complete last week's assignment.
- Everyone got something wrong, and it made me feel bad.
- There was a lot there to over-think, but there wasn't much to get wrong if you looked at what the assignment asked.
- In fact, the assignment instructions seemed to ask for more than we actually graded on.
- It was possible to get a perfect score without meeting expectations, yet no one did.
- </p>
- <p>
- It feels like this course was mostly review for me.
- And yet at the same time, it feels like I actually got somewhere this time.
- Last time I took a Java course (outside University of the People), I frequently ran into compilation errors and such.
- I often took way too long to complete assignments, and on the final assignment of the course, I didn't even have time to perfect my submission.
- I had to hand in code that triggered a compiler warning, but technically did run and meet the requirements.
- That project did meet the professor's expectations, but it shouldn't have.
- At a bare minimum, code shouldn't result in any errors or warnings, but I couldn't figure out how to restructure the code to remove the warnings.
- Maybe that other course was just poorly-structured, and University of the People's course is better.
- </p>
- <h3>Epilogue (Unit 9)</h3>
- <p>
- The final exam was longer than I thought it'd be, but not quite as long as the review quiz.
- I didn't do as well on the review quiz as I'd like to have, but I feel like I did better on the actual final exam.
- I won't find out for several days how I did though.
- </p>
- <p>
- My final thought for the term is that I hope <span title="Programming 2">CS 1103</span>, which I'll be taking next term, gets into either the development of graphical applications or the development of mobile applications.
- </p>
- <h3>Discussion post drafts</h3>
- <blockquote>
- <p>
- According to the reading assignment, each component of a <abbr title="graphical user interface">GUI</abbr> application can be represented by an object.
- These objects are able to interact with one another via each other's interfaces.
- The way in which these components interact is called messages.
- </p>
- <p>
- The reading material also says though that in <abbr title="graphical user interface">GUI</abbr> programming, messages are referred to as events.
- One component will send a message, while another will receive it.
- An event handler is a component that acts as a listener, responding in some way when certain events are triggered.
- Events can be triggered in any number of ways.
- For example, one might be triggered because a certain amount of time has passed or because a user (remote or local) did something.
- Data reception from the network is also an event.
- Responses are a varied lot as well.
- A response may be that a window a user clicked on gains focus.
- Another response might be that a sound is played from the speakers.
- </p>
- <p>
- Listeners have to be registered before they can listen for events.
- This is because the system needs to know to try passing the event to that listener.
- Once registered, the event handler is able to respond to new events as they occur.
- In responding, an event handler may generate a new object that will complete the response, but this isn't strictly necessary.
- In Java, registered event handlers must implement a specific interface based on what kind of even they're listening for.
- </p>
- </blockquote>
- <blockquote>
- <p>
- This has been a long week for me, not because of coursework, but because I've been dealing with a health-related problem and some legal stuff, among other things.
- My health issue has cleared up as far as I can tell, and the legal stuff is all but finished, but I was thankful to have a slow week, coursework-wise.
- </p>
- <p>
- If not for that though, I would've loved to have been able to try out some stuff.
- Oddly enough, I've never built a graphical application in any language, so it would be nice to get a chance to change that!
- It looks like <span title="Programming 2">CS 1103</span> might be a continuation of this course, so maybe we'll both get a chance to try out graphical programming in Java later.
- I'm not sure about you, but for me, that'll be next term.
- <span title="Programming 2">CS 1103</span> is a prerequisite to six other required courses, so it seems like a good idea to take it early on.
- </p>
- </blockquote>
- <blockquote>
- <p>
- I like how you bring up that the change in an object's state can be an event.
- As most of the program will be made up of objects, that means there's a lot of places where an event handler could react.
- You also mentioned something I had taken for granted myself.
- If there's no listeners, nothing reacts to any events that occur.
- It's a simple concept, but very important to understand.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You mention that if we can read your post, we're making use of a <abbr title="graphical user interface">GUI</abbr>-based application.
- That's not necessarily true.
- Some Web browsers, such as Lynx, are text-based.
- Using one, we can read your post, as well as most other websites, without any need for a <abbr title="graphical user interface">GUI</abbr>.
- </p>
- <p>
- Your explanation of events, event sources, and event handlers seemed very well thought-out.
- </p>
- </blockquote>
- <blockquote>
- <p>
- You make a good point about a component only existing as long as the parent does.
- As you said, the <code>JFrame</code> is the root object, which makes it the exception to this.
- However, for example, a button cannot exist once the window it was a part of is removed.
- I also like how you mentioned that events are <strong>*usually*</strong> triggered by the user.
- This isn't always the case, of course, but the vast majority of events will probably be user-triggered in some way.
- </p>
- </blockquote>
- <hr/>
- <p>
- Copyright © 2017 Alex Yst;
- You may modify and/or redistribute this document under the terms of the <a rel="license" href="/license/gpl-3.0-standalone.xhtml"><abbr title="GNU's Not Unix">GNU</abbr> <abbr title="General Public License version Three or later">GPLv3+</abbr></a>.
- If for some reason you would prefer to modify and/or distribute this document under other free copyleft terms, please ask me via email.
- My address is in the source comments near the top of this document.
- This license also applies to embedded content such as images.
- For more information on that, see <a href="/en/a/licensing.xhtml">licensing</a>.
- </p>
- <p>
- <abbr title="World Wide Web Consortium">W3C</abbr> standards are important.
- This document conforms to the <a href="https://validator.w3.org./nu/?doc=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1102%2F"><abbr title="Extensible Hypertext Markup Language">XHTML</abbr> 5.1</a> specification and uses style sheets that conform to the <a href="http://jigsaw.w3.org./css-validator/validator?uri=https%3A%2F%2Fy.st.%2Fen%2Fcoursework%2FCS1102%2F"><abbr title="Cascading Style Sheets">CSS</abbr>3</a> specification.
- </p>
- </body>
- </html>
|