Jump to content

Hitchhiker's Guide to Programming

About this Writers Block

Greetings all fellow Ember folks!

This block will be all about programming, teaching you about anything from the basics to how to make your own app. There will be live streams going through the content uploaded, in order to deepen the understanding since there is just so much you can do in text (plus, some find it easier to learn if they also watch/hear someone giving lectures about the subject). Feel free to add suggestions on content!

Cheers!

/Kyathil

Entries in this Writers Block

kyathil

Greetings mortals!

I am Kyathil, a Swedish programmer and content creator on Twitch. I do apologize in advance if this entry is full of grammatical errors since the clock is way to early on a Monday morning and I need about 53 more cups of coffee to function properly.

The idea of this writer's block is to supplement my future programming streams, providing some "course notes" to those sessions. This specific entry is a short introduction about myself, the idea and a test on how to use this writer block function on Ember.

So, what will be covered? Well, to begin with I will do a couple of sessions with the goal of making a basic android app. The reason here is that it is just so much more fun to learn something if you can put it in practice fairly quickly  and android app development got great tools which anyone can use for free. But, the viewers and readers can also request topics to cover on future session... Thus, what will be covered is partly up to you! This includes the possibility of covering other programming languages/tools in the future.

Will we make an app on the first session? No, I will start off with some basics in Java and some general information about programming and tips on good online courses and tools to use. It is, after all, good to learn a bit about what's going on beneath the hood before you start driving!

 When will the course notes be published? Hopefully before every stream, making it possible for anyone to learn a bit before the live sessions. The good ol' "read the theory and then watching it in practice" way of learning!

Are you a teacher or something, or why are you doing this? No, I'm just working full time as a developer and got 2 university degrees in programming areas (and a few grey hairs thanks to that). Been getting questions about programming during streams for such a long time that it seemed like a good idea to do this partly to answer these questions and partly to give some insight in what I do for a living.

When will the live sessions take place? I will post dates with every block entry, except this one, and if anything changes I will notify you on social media.

images.png.1a9debd42dc830d7d7a2f1b94eecabb4.png

 

kyathil

Greetings Emberlings!

As we all know the greatest tool you can get is a towel, but there are also plenty of other tools available for those of you who wishes to dive into the coding world! In this post I will give some personal favourites of mine and also a couple of sites with great online courses.

Development Environments

There are a bunch of great environments that you can use, for free, while developing projects in different languages and for different uses. The pros with using a complete development environment is that you as a developer will have most tools you need to code, debug and compile in one place without having to put the time to develop these yourself (which also means a deeper understanding). The cons are that there are a bit of "magic" underneath the hood, there might be licensing issues and the build/compiling time will be a bit slower vs creating your own environment from scratch. Just to mention a few things. As a newbie within programming I definitely do recommend using a development environment at first!

Might be good to add that these environments are available for Windows, MAC and Linux so you can choose whatever platform you want! There is a hickup with android development in Android Studio if you got an AMD CPU... But will cover that in a future post.

Eclipse - Mainly Java/C programming, but got extensive available addons which make it possible to code in many other languages too. OBS! You need to install a JRE (Java Runtime Environment) with it!

https://www.eclipse.org/downloads/

Visual Studio - Development enviroment by Microsoft. Have very little personal experience with it, but compared to Eclipse it offers a wider range of use (such as more extensive web development and app development)

https://www.eclipse.org/downloads/

Android Studio - THE tool for making android apps. Features development of apps in both Java and Kotlin (which I will introduce later on). 

https://developer.android.com/studio

Text Editors

Depending on what you are developing, you might ditch the development environments and instead use a text editor for your code while building and compiling using a terminal (for example if you want to create a website using React.js). Here are my 2 favourites for these situations:

Sublime (got some really sweet features to make your syntax look absolutely dashing)

https://www.sublimetext.com

screenshot.png.7ce2ab75d638be7795100ff24e97d261.png

So pretty!

Notepad++ 

https://notepad-plus-plus.org/download

Online Compilers

If you're not making an entire project, but rather a shorter program in order to learn a programming language, then you don't even need any environment. There are plenty of online compilers for any programming language. They do not offer the same amount of debugging etc as a full environment, but they work well enough for learning purposes or if you want to try out some algorithm. Will provide you with a few here, but it is possible to just do a simple google search to find a compiler to use (most of them are fiiine and rather similar to each other).

Java

http://www.browxy.com/

https://www.compilejava.net/

d7e2f1957c399b7ddfcf3b5aafcb444c.thumb.png.b9809968a5abd6dc711053d18cac2131.png

C

https://www.codechef.com/ide

https://www.onlinegdb.com/online_c_compiler

Online Courses

Feeling ready to dive even deeper into the coding world, but don't wanna enroll to university? There are a lot of great online courses, even on youtube, which anyone can take. Most are not free, unfortunately, but there are free trial periods on most and discounted offers appear every now and then. A few let you code in the browser, while most are more like lectures where you have to get the tools yourself. I'm using some of these myself to learn new things!

Free

https://www.codecademy.com/

Pay per course (really good)

https://www.udemy.com/

Subscription based

https://learn.pluralsight.com/

https://teamtreehouse.com/

Until Next Time!

Phew! Such a meaty post! 

Next post will be up tomorrow or on Friday, starting with some basics in Java. Remember: DON'T PANIC!

Cheers!

programmer_joke1.jpg.f7b0b572e4b810101f9b3141c9ffa06c.jpg

kyathil

Good Day!

Been a bit of a hectic time for me lately, hence the lack of updates. Today we will talk about loops, if/else and a little bit about arrays. I am also planning on having my first coding stream on Sunday, 16th, starting at 7 pm CET. Expecting the lesson to be 1-2 hours or so, thought it might be a decent time for such things.

LOOPS, so many LOOPS!

Loops are a big part of programming languages and Java got them too, of course. There are mainly 3 different loops: the while-loop, for each-loop and for-loop. You will use either of these depending on what you need to loop over, I will show you how to accomplish the same programming task in all 3 loops with the help of an array of int. An array is basically a list of objects and in this case it will be a list of integers, aka numbers (remember that you cannot mix object types in regular arrays). 

loops.thumb.png.d8328c225014681e21ca8de92f304cba.png

In our example here we can see a couple of things. The object we are looping through is an array called... array, consisting of 9 objects (1,2,...9). The first loop we will talk about is the for-each loop. The for each loop will check a container, such as a list or an array, for certain elements (in this case it checks for objects of type int). Note that you cannot use the reference int i outside of the loop. This variable is only visible to this specific loop. In our code example the for each loop will check every element in the array to find elements of type int and then print this element to the console. The for each loop is especially good for looping through collections or arrays where you know the object type you are looking for, but don't know the length of the collection/array/container. Note that I have commented out two loops, which is useful to do for example while debugging in order to find where in the code a bug is hiding.

loops2.thumb.png.88f198f3e3bfbc04a7d1c746f3715058.png

Alright! Looping is so much fun! Our next loop is the classical for-loop. For-loops starts at  certain index, in this case the index int i is set to 0. In programming we count from 0 and not 1. After the index we have a condition to check, i < array.length which checks that the index i is less than the length of the array, and as long as our condition is true the index will increase with 1 (this can be decreasing with 1 or set to something else). For every index i in our array, we print out the object on this position to the console (array[1] would refer to the object on position 1 in the array which would be the number 2 in our example).

The last loop is the while-loop. Unlike the other loops, we are initializing a variable outside of the loop (see what happens if you put it inside the loop instead!). The while loop will continue as long as the condition in the while loop is true, in this case for as long as the variable i is less than the length of the array. After printing out the object, just like the for-loop, we increase i with 1.

All of these loops are doing the same thing in my little coding example and they can usually be used to perform the same job. Which loop you want to use depends on the situation! I do want to alert you about creating inifinity loops, which happens to the best of us. The infinity loops happen because the programmer forgot to add a statement which will break the loop and are very common bugs in programs.

IF/ELSE/ELSE IF

As a programmer you will use if/else/else if cases a lot. Basically what they are doing is that they will check if a condition is true, or another statement is true, the program will perform a certain task and if not it will do something else (or nothing at all). 

ifelse.thumb.png.629c39b534f41c7d26451b4364c8c7d5.png

In order to make our code a bit cleaner I removed 2 of the loops. Now, inside our for-loop we are checking a certain condition, array % 2 == 0. Wait... what? Why do we add a percentage after our array?!  And why == and not = ?? Well, I actually though this was a good time to show off the modulo operator and ==! Let us break this condition down. We have the object on position i in the array, modulo 2, and the condition is that the result should be equal to 0. The modulo operation finds the remainder after division of one number by another. So, the object to the left of % , array, is divided with by object to the right, 2. If there is no remainder of the operation, it will return 0... So 4 % 2 would return 0, but 5 % 2 would return 1 (5  = 4 + 1).

Great! Useful info! Now, what about ==? Well, in Java == is used to compare objects to check for equality (this is not always going to work though). This means that the object to the left of the == will be compared to the object to the right and if there is a match it will return true, else it returns false. In our code snippet we will get a number from our modulo operation, which is the object to the left, and this will be checked if it equals to 0. To write the whole operation in English: if the object on position i in the array is dividable by 2, then "I am even!" will be printed out to the console. Else, if this condition is false, "I am odd!" will be printed. We can see that it is correct through the prints in the console (1 is odd, 2 is even, 3 is odd, 4 is even... etc)

So... In a if/else case we check a certain condition and either perform operation X or Y depending on whether or not the condition returns true or false!

elseif.thumb.png.967580ceac9224e456a1cbf940ca354d.png

It is possible to build larger if/else cases, with more statements, by adding else if conditions too. These conditions always occur after the initial if-case and do not have any limit. You could, if you want, add 100 else if-cases in your program (not recommended though). The understanding here is that if the first condition returns false but the next condition is true, the program will perform the operation after the else if case. In our updated program we will first check if the object array is dividable is true and if not, we check if it is less than 3... Else we perform the last operation. We can see that the program works by observing the console. 1 is not dividable by 2, but is less than 3, and "I am less than 3" will be printed out to the console. 2 is also less than 3, but since the program checks for whether or not the object is dividable by 2 first, it will print out "I am even!" instead.

There is another conditional that is worth mentioning, just for you to remember the phrase: switch-cases. This can be useful to use instead of those 100 if else-cases in your program!

Up Next

This time I haven't decided on the topic of next post... Either more about operations, for example !=, ||, and &&, or starting with Android Studio. What would you like to see?

Also, cya on Sunday at www.twitch.tv/kyathil !

2305879_0.jpg

kyathil

Welcome back!

In today's post I will be going through some common variable types. Be prepared, it's a pretty long and meaty post! I was originally planning on also introducing if/else cases in this post, but I think it would be a bit too much and will thus introduce that in the next post instead!

Variable types

Alright! Let me introduce you to some standard variable types: int, String, char, boolean, null and double. These are the variable types that you will be using pretty much all the time while coding. I will explain a bit about each one, starting off with int (and Integer...). But first I have to explain a little about primitive types. As you might have noticed, all of these variable types were written in lower case letter, except for String. This is because every mentioned type, except for String, is a so called primitive type. Primitive types represent raw values, for example a variable of type int will be storing the binary number of whatever value you set the variable to. All primitive types have equivalent wrapper classes, Integer for int as an example, that are inheriting from the Object class (Integer inherits from int) and store references to these objects... A String is actually not a primitive type, but a class referencing a certain object (hint, not a primitive string type!) but will explain this later on.

Did you understand that? If yes, good job! If no, no worries... This is nothing you have to focus on. I just wanted to point these things out so that you might have a vague memory of it for the time when this information actually matters. For now though, have a coffee and rest your head!

int and basic arithmetic operations

The primitive type int actually stands for integer, with a wrapper class called Integer, and you can think of this type as storing non-decimal numbers. It is a bit more complex in reality, but nothing any new programmer need to worry too much about (but if you are curious, I do encourage you to check what an integer actually is). With int you can do basic arithmetic operations such as addition, subtraction and multiplication. How you do that I will show you in the following code snippets.

 

integer1.thumb.png.f599f26661ac5864b7c9c16a05371ea4.png

As you can see I removed pretty much all code from our Hello World program and added a couple of new things. First of all, in order to initialize a variable in a method (there are other ways too...) you follow the pattern seen in the code: int number = 15; , which basically means that you set a variable named number of type int to 15. Don't forget ; Secondly, adding comments in the code that are explaining what you are doing is a good practice! Comments does not affect the program and will only be visible for the person reading the code and you add comments by using //. There is another way to comment, but we don't need that yet. Thirdly, I added 3 different ways to increase a number with 1. Not that writing number++2; will only make the compiler angry and it will also be grumpy if you would set an int to a decimal number, like 15.5. Feel free to check for yourself! Lastly, you may have noticed something different with out System.out.println (this method is going to be your best friend btw).

In the previous post you were introduced to this lovely method which will print out whatever you need to the console and is a great way to make sure that the code is doing what you expect it to do (hence all the prints in the code snippet). However, this method will only accept text, or String objects, which means that we cannot simply write System.out.println("This is number " + number + "\n"); , but we will have to parse the int to a String object. There are a few methods available, but I chose to use Integer.toString(number) in this case. The + is how we put together String objects ("This is number"  Integer.toString(number) will also result in an angry compiler) and the "\n" tells the compiler to start a new line (which can sometimes make the prints a lot more readable). 

integer2.thumb.png.3875e39768dab3104b6d6755c1047f88.png

Here I simply demonstrated how to subtract and multiply. 

 

 

integer3.thumb.png.f80923058f7f106eb2b95948005ca0d9.png

Some division... I do recommend that you play around with this yourself for a bit before moving on to the next variable type(s), String and char!

String and char

A String is actually an array of char, and you can think of a variable of type char (or character) to be any single unit of textual representation (such as letters, commas, digits etc). An int can never be set to a letter, while a char can. You can initialize a char by simply following the following syntax: char c = 'E'; (you set this to any single unit, such as '1' or '*'. 'Em' will result in an error though). In order to initialize a String you will follow a slightly different syntax: String s = "Name"; (the "" tells the program that a sequence of characters will follow). It is perfectly fine to set a String to anything, even a so called empty string, but you cannot set it to a character (String s = 'E' will result in an error, but String s = ""; is fine). 

strings.thumb.png.aeda283d0115a64b0dc0efa7efab2f8e.png

In this code snippet I wanted to show you what a String and char look like in code, but also what an array of char would looks like (as I mentioned, this is basically what a String is). As you can see here, there is no need to parse characters if you want to print them out, unlike int, but if you try to print out an array of char you will see something unexpected in the console. Simply printing the array will not produce any error, because what is shown is in fact a String... But it's a String of the reference to the array object and not what the object contains ('E', 'm', 'm', 'a').  In order to print out the content of the array in the example you either will have to print each element on it's own (myNameInChar[0]...myNameInChar[3]) or use the very handy String.copyValueOf(), using a toString() will not do what you want. This is a little bit too complicated to dig in right now though and will not explain the nature of arrays in this post, but it will be explained in a future post. Note that the first letter in the array was at 0 and not 1!

double, null and boolean

Almost done with explaining some of the basic types! You might be glad to know that this section will be a bit shorter than previous ones, since explaining everything about these types is a bit too advanced. A boolean is an algebraic notation used to represent logical propositions by means of the binary digits 0(false) and 1(true). A boolean can only be set to true or false. Using variables of this type is common if you are about to check logical expressions or statements (examples in the next post). A double is, very simplified, a decimal number and all numbers with decimals in Java are assumed to be of type double. The last type I will mention is null, which is a kind of a special flake. You cannot set a variable to null, but you can check in logical representations that a variable is/is not null. Wait... What now? You see, null indicates that a variable does not refer to any object or array... What exactly this means will be shown in the next post, since it's easier to explain it using if/else cases.

doublebool.thumb.png.b79498b926d6f91ead27a105afa094b0.png

As you can see you can use basic arithmetic expressions with a variable of type double too! Also, you would need to convert both boolean and double to String if you want to print out a variable of any of these types.

Up next

Whew....! That was a very informative post! In the next post, hopefully up in a few days, I will be getting into if/else... And maybe some loops!

I hope you found this post informative and if you have some suggestions on what to add in the future, or if I should have longer/shorter posts, feel free to add that in a comment!

Cheers!

72036883039a3491283727d23a4adf9f.jpg.0e97d2eac61f1b1dae78c3560e39ed0a.jpg

kyathil

Let's start with pretending it's Friday...

Sorry for the lateness of this post, life got in the way! Anywho, let's get into Java!

Java is a well-established (and sort of old) object-oriented programming language and widely used in software. I'm not going to explain in detail what "object-oriented" means, but you can think of it as basing the language around building pieces of code in small units and then assemble them together to create a full program. A little like bricks to build a house. If you're curious, I do recommend googling on the subject to deepen the understanding (it's a bit complex to explain in a blog post).

The reason why I chose Java to start with and not, for example, C is that the goal of my lessons is to teach you how to make simple android apps which happens to be using Java(or Kotlin) for the logic. The idea is that it's easier, and more fun, to learn something if you can at the same time put it in use!

In this post I will be using http://www.browxy.com/ for compiling code since it's such a handy thing to use, but will be using Eclipse in the future too! FYI, you can create an account on browxy, which is free, and that will make it possible for you to save projects and code for future use. Might be good to know! 😄 

Hello World!

We are going to start off with the classical "Hello World!" code base which is what you're usually greeted with if you create a new project in a development environment. So, first navigate to browxy (or any other online compiler) and hit try on "Hello World !", then press start to compile the program. After running the program you will see the text "Hello World" in the Console View.

helloworld1.thumb.png.43f18517c9c616047f713d070ecfc55b.png

What does all of these things mean? I'll do my best to explain the different parts briefly, feel free to comment if you need further explanations! 

public class HelloWorld {

}

public is one of two access level modifiers for classes, the other being package-private (rarely used). This means that the class is visible everywhere, while in the other case it would only be visible in the package the class is included in. A class is a user defined blueprint from which objects are created... Inside the class you will contain methods, variables and eventually constructors (which I will explain in a future post). HelloWorld is the name of our class, class names are always supposed to start with a big letter (note that you cannot use spaces in the names and the general naming convention is to use a big letter for where you would normally include a space: Hello World -> HelloWorld). In Java we are always starting and ending classes, methods and constructors with curly brackets { }. So... In this code example we have a class called HelloWorld which is visible everywhere.

public static void main(String[] args) {

}

Here we have our very first method, called main. A method is a piece of code which will perform an operation in the program. You can create your own methods, use built in methods and import classes with methods from other libraries. In this case it's our own constructed method. Once again we see a access level modifier, public, and methods can have a few more access levels (private - only visible within it's class, protected -visible within it's package). void refers to the return type and in this case the method will not return anything -> void, but in future posts we will be creting methods with other types of return types. Methods are always starting with lower letters and will always contain a parentheses which may or may not contain arguments. In this case we have an argument, String[] args. String is one of the common variable types in Java, will be going through a few further down in this post. String[] means that it's an array of strings, think of it as a list. args is the name of the argument to this method, just like method names we should always start argument names with a lower letter.

The so called main method is not just any method, it's the starting point for your Java application and will always follow the syntax above! You need to have at least one method like this in one of your classes in your Java application! Yes, you can have more than one class and it will be covered later on 🙂

Let's go through the last bit of our very first Java program:

System.out.println("Hello World");

This is a method not created by the user but already existing in the system. What we're doing is basically telling the program to print out "Hello World" to the console. You always need to use ; to call a method and "Hello World" is an argument to this specific method. Yes, you can sometimes use text as an argument to methods (or numbers...). I'm actually not going to explain this further because I think they did an excellent job in explaining everything at https://javapapers.com/core-java/system-out-println/ .

Great!

I will actually not cover anything else in this post, since it's quite a lot to take in, but in the next one I will be going through some standard types, how you can call on methods from other methods, what different types of variables exist and perhaps what the good old if/else case is! Expect another post soon!

h-he-hel-hello-world-omg-his-first-words-skill-3412731.png.cb6f448c9e58b29a2ac9bb6710990606.png

×
×
  • Create New...