Chapter:Programming introduction

From Juneday education
Revision as of 17:36, 18 November 2018 by Rikard (Talk | contribs) (added div with noprint class to the embedded video)

Jump to: navigation, search


Programming introduction

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

In this presentation we will be introduced to programming by comparing programming to normal everyday actions, such as washing clothes.

We will also get a better understanding of the compiler and the steps performed when compiling source code.

Purpose

The purpose of this presentation is to make it easier for the student to understand the following lectures.

Writing software in languages such as C, C++ and Java means you at some point must transform your source, which is in text form, into a form executable by either the operating system or a virtual machine.

Requirements

It is assumed that the student is familiar with OS (operating system), program, file system, file and file types.

No other previous programming knowledge is required.

Goal

After this lecture the student shall have basic understanding of what a program is, how it is written and how it relates to everyday actions we humans do.

The student should:

  • understand the role of a compiler

Instructions to the teacher

Common problems

Make sure the students understand the edit - compile - run workflow. This chapter is however mostly a chapter to introduce programming by showing some examples.

All videos

All English videos in this chapter:

All Swedish videos in this chapter

Note that some videos are in English but included here for completeness.

See below for individual links to the videos and slides.

Introduction to programming

Description

Programming is the practice of writing text files (so called source code files) where you express instructions to be performed by a computer. You express these instructions using a so called programming language, which is a set of rules for how to write said instructions.

The product of programming is a set of source code text files which typically are transformed into a a file or several files in language which is also understood by the computer or some existing computer program. This transformation from source code to files that are meaningful to the computer (or some existing computer software) is generally referred to as compiling.

Further on in this book, we will also address different ways to analyse what we want our programs to do, as well as different ways to accomplish that. Those activities may also be seen as activities belonging to the practice of programming.

Programming language

Programmer

A programmer is a person who develops (creates/writes) computer software. A programmer can be specialised in many different fields of programming: web, system, embedded, real-time, test and so on. In this course we will give the basics of Java programming. Enough to get you started to become a programmer and also enough for you to take on other roles in system development, such as project manager or product owner.

Videos (introduction)

  1. Introduction to programming (eng) (sv) (download presentation)

Links

  • No links here

Program examples

Description

We will show you some examples of programs, how we write them and the different procedures to get them to be executed. See the video lectures below for some examples of programs in various programming languages.

Videos for examples in various languages

These videos contain examples of programs not written in Java. You will most likely not be able to compile and execute these. We provide these videos and source code as examples. If you still want to compile and execute them, we provide links to pages of how to set up your environment for these languages. (no additional setup needed)

Compiler

Description

A program that transforms (or transform) software written in one programming language to another programming language. It will take an entire course to give you enough information about compilers and how to write them but in our courses this will do fine.

Examples of compilers are Java Compiler which translates from Java source code to Java byte code (to be executed by the Virtual Machine) and C Compiler which translates from C source code to machine code.

Expand using link to the right to see an example of compilation of a C program.

C compilation

Let's give an example in C. Here's the source of a file called hello.c:

#include <stdio.h>

int main(void)
{
  printf("Hello world\n");
  
  return 0;
}

Source code can be found at github: hello.c (download with curl: curl -O https://raw.githubusercontent.com/progund/junedaywiki/master/compiler/hello.c).

To compile the file above you type the following:

$ gcc hello.c -o hello

Now we have transformed hello.c (yet kept the original) into a program called hello.

To execute the program (as result from compiling hello.c you can type the following:

$ ./hello
Hello world

If we want to check the file type we can do the following:

$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=59c0391f9cb05af1e15cf6f96db55b6bf141ad8b, not stripped

Note: the printout from file is from the author's laptop running GNU/Linux. The printout may differ on your computer.

Expand using link to the right to see an example of compilation of a Java program.

Java compilation

Let's give an example in Java. Here's the source of a file called Hello.java:

public class Hello {

  public static void main(String[] args) {
    System.out.println("Hello world");
  }

}

Source code can be found at github: Hello.java (download with curl: curl -O https://raw.githubusercontent.com/progund/junedaywiki/master/compiler/Hello.java).

To compile the file above you type the following:

$ javac Hello.java

Now we have transformed Hello.java (yet kept the original) into a file called Hello.class.

To execute the program (as result from compiling Hello.java you can type the following:

$ java Hello 
Hello world

If we want to check the file type we can do the following:

$ file Hello.class 
Hello.class: compiled Java class data, version 52.0 (Java 1.8)

Note: the printout from file is from the author's laptop running GNU/Linux. The printout may differ on your computer.

Videos

  1. Compiler (eng) (sv) (download presentation)

Questions and Answers

Expand using link to the right to see the full content.

Q: “There seems to be many different programming languages. Do we need so many?”
A: Luckily, we only need to learn one language in order to learn how to program. But there are many languages being used for programming and, of course, all programming languages differ more or less from each other. After learning on language, it is wise to at least gain some basic knowledge of a few other common languages. Reasons for learning any particular language may range from career decisions (some languages are more common on the job market than others) to reasons based on curiosity. If the languages differ, there must be reasons for this! What makes any one particular language different from the rest? Beware that if you ask any proponent (or sales representative, or member of the fan club etc) for one programming language, they will tell you that the language in question solves all problems with the rest of the programming languages in existence. However, there are some main differences between at least families of programming languages, which stem from the fact that different families of language take different approaches to describe the task for the program to be written together with different approaches for how the task should be solved and executed. For this reason, there is much to be gained from learning a few languages (at least on a basic level) from different families of languages, because that might help you gain insights about how to think about the practice of programming - modelling the components that are part of the task for a program and also how to solve problems or perform the tasks using the modelled components.

Other reasons for needing different programming languages could be the domain for the programs to be written. There are languages that are more suitable for writing software that is very close to the hardware the program is running on (like programs that are part of an operating system, or programs that are running on some small piece of hardware perhaps even without an operating system). There are other languages that are specialized on solving problems of a mathematical nature, languages specialized for statistical analysis and visualization, languages more targeted at the world wide web etc.

Programming has been around for quite some time now, so this could also help explain why there are so many languages for this activity. Languages of course evolve over time and it is not uncommon for the industry to take part in the creation of new languages. Often there are competing languages from different vendors, where the languages do not differ all that much, but that’s not what the vendors’ marketing departments will tell you!

Q: “Is a program really nothing but a sequence of instructions?”
A: At least to the computer, it is! As you have learnt from the previous answer, there are many different families of programming languages which all take a different approach to how to organize the instructions and how to express what the program should do. But in the end, the computer is the one responsible to do the actual work that the program describes, and the computer will do this in a way that very well might be described as executing a sequence of instructions. Java is a so called Object Oriented programming language. As we will learn throughout the course, the ‘Java way’ of describing what the program deals with and what to to with that, is focused around the notion of objects representing the stuff our program deals with, and what those objects can do (to help us get our program to do what we want). For this reason, programs written in Java don’t lend themselves as easily as programs written other languages, to the idea of “programs as sequences of instructions”. As you will discover, however, when we write the code describing what our objects can do, we express that behaviour in the form of “sequences of instructions”.

Q: “What are the steps to create and execute a Java program?”
A: You will learn this throughout the course, but it is really simple and only three steps:

  1. Create the program using a text editor by typing in valid Java code and save the file (according to some file naming conventions).
  2. Compile the file using the javac compiler (javac is the name of the Java Compiler) giving your source code file as the argument to javac.
  3. Run the Java program using the java command (java invokes the Java Virtual Machine and takes the class name of your program as an argument).

Q: “There are naming conventions for the Java source code files? And what did you mean by ‘class name’?”
A: All this will become clear in coming chapters of this course! In short, every Java program has at least one ‘class’ which is a kind of unit for your programs. A class you define has a name (that you choose). The file must be saved using the name of the class followed by the suffix ‘.java’. When you run the program, the java command expects as an argument the name of the class and nothing else. But don’t worry if this sounds complicated! It will be clear in a chapter coming soon!

Links

Further reading

Wikipedia links for the keen student:

Where to go next

« PreviousBook TOCNext »