Luet oppimateriaalin englanninkielistä versiota. Mainitsit kuitenkin taustakyselyssä osaavasi suomea. Siksi suosittelemme, että käytät suomenkielistä versiota, joka on testatumpi ja hieman laajempi ja muutenkin mukava.

Suomenkielinen materiaali kyllä esittelee englanninkielisetkin termit. Myös suomenkielisessä materiaalissa käytetään ohjelmaprojektien koodissa englanninkielisiä nimiä kurssin alkupään johdantoesimerkkejä lukuunottamatta.

Voit vaihtaa kieltä A+:n valikon yläreunassa olevasta painikkeesta. Tai tästä: Vaihda suomeksi.


Chapter 1.2: Introduction to Programming

About This Page

Questions Answered: What are programs and programming like? Where do I write my program code? How do I run a program I’ve written? How do I submit programming assignments for assessment?

Topics: Basic concepts and characteristics of programming. The tools of a programmer: programming languages, program code, programming in an IDE, packages.

What Will I Do? Read. Experiment with programming tools. Modify given application programs.

Rough Estimate of Workload:? One and a half hours. Could be more if you also install the programming environment onto your own computer.

Points Available: A12.

Related Projects: GoodStuff (new), O1Library (new), Pong (new).

../_images/person01.png

Creating and Running Programs

A traditional way to describe programming is to compare it to cooking. Here is that comparison in interactive form:

Running a program

../_images/computer_as_cook.png

The programmer defines how they want the computer to act as it runs the program. For instance, a programmer writing an application for handling course enrollments at a university might include an instruction to this effect: “Whenever the user clicks this particular button, add the user to the list of people enrolled in the course.”

The user of an application program has access to a copy of the program, perhaps stored on the user’s own computer. When the user starts up the program, the program is run (or executed; ajaa, suorittaa) and the computer follows the instructions that are set down in the program.

Consider an example. A user clicks an icon to tell the operating system that they wish to launch a word-processing application. The computer will now act as defined in the chosen application program. Assuming the programmers who created the application have done their job well, what happens next is what the user wants: a new window pops up onscreen and provides useful word-processing functionality.

Does the user really have a copy of each application?

The above description of running programs in a computer is a traditional one. It and continues to apply to many present-day situations, and it’s sufficient for our purposes in O1. However, developments in computing have affected how some programs are run.

Optional exercise: with the help of the internet, find out what the following terms mean: web application (web-sovellus), cloud computing (pilvilaskenta), Software as a Service.

Programming languages

The processor that causes the computer’s behavior does exactly what it’s instructed to do, and nothing else. It executes commands one by one, in order. Unlike the knowledgeable cook who reads a recipe, the processor comprehends nothing about the purpose of each command within the overall program. It doesn’t know how to interpret meanings from context, how to conjecture, or how to adjust to new situations. Ultimately, the computer is stupid. It is, however, fast and precise and has a reliable memory.

As we write a program, we need to describe, literally and in great detail, what we want the computer to do when the program is run. For that, we need instructions that are precise and unambiguous. Natural human language is neither precise nor unambiguous and isn’t well suited for programming: consider expressions such as “add salt to taste” or “until it has a beautiful brown crust”.

Programs are written in a programming language (ohjelmointikieli). Programming languages are formal and allow only a limited set of well-defined words and expressions. Any acceptable expression in a programming language has a precise meaning. A perhaps useful point of comparison is the molecular formulas used in chemistry, which are likewise governed by precise rules; those formulas are, however, rather simpler than most programming languages.

A huge number of programming languages have been developed over the past several decades. Wikipedia, for instance, has a list thiis long, and even that is by no means comprehensive.

Some programming languages are better suited for some purposes, others for other purposes. Some are intended as general-purpose tools, others have a more specific raison d’etre. Some have been consigned to the trash heap of history, others march on from one decade to the next. New languages continue to be created, as do new versions of existing languages.

../_images/ohjelmointikieliä.png

The names of some programming languages. We’ll be using one of them.

Scala, a programming language

../_images/scala_logo.png

The Scala logo. In Italian, a staircase is called scala.

In this course, we’ll use a modern and versatile programming language by the name of Scala. As you use Scala, you’ll learn general programming concepts that you can also apply when programming in other languages. An experienced programmer can pick up new programming languages relatively quickly.

Just as an example of what the language looks like, here are three commands written in Scala. The theme for these commands is a (simple, imaginary) accounting application that keeps track of employment information.

val hiredPerson = new Employee("Sophie Student", 1992, 3200.0)
hiredPerson.workingTime = 0.5
hiredPerson.raiseSalary(1.2)

At this point, you don’t have to (and can’t) entirely know what this example means. But you can read a short commentary on it. Move the mouse cursor over the explanations in the green boxes and the associated bits of the Scala example will be highlighted. Try it! You can also click the explanations to “lock” a highlight.

The first line instructs the computer to record information about a new employee in its memory. An employee has a name, a year of birth, and a monthly salary.
The second line marks the person as a part-time employee: their working hours are set to be 0.5; that is, 50 percent.
The third line records a 20-percent raise for the employee.
A programmer is free to name many of the concepts and entities in programs. Some of the words in this example are specific to this program and have been chosen by the programmer using English words as a basis. The programmer could just as well have used words in Finnish or Latin here without affecting what the program does.
Some of the words are part of the Scala language itself and have not been chosen by the programmer. Scala’s vocabulary is based on English words.

You’ll learn more and more Scala throughout O1. Nevertheless, we won’t be covering all aspects of this multifaceted language in one course. Scala is also used in multiple other courses at Aalto and in many contexts outside the academic world.

We use Scala version 2.12.6.

A program — is what exactly?

Right from the start, it’s good to notice that the word “program” refers to two separate things:

  1. the program text, known as the program code (ohjelmakoodi), which defines a sequence of instructions for a computer to follow; and
  2. a program run: the things that happen as a computer executes the instructions in the program code.

This is why you can say of a program you wrote “My program has 15,000 lines of program code.” as well as “Go ahead and click that button, and my program will send the results to the printer.”

Rephrasing the above, a program is, on the one hand, a static (staattinen), passive piece of writing on what a computer should do. On the other hand, a program is a dynamic (dynaaminen), active process that acts within a computer and can interact with its users. A similar duality exists in many programming concepts as well, and we’ll be returning to this theme multiple times as we progress.

Inside and Outside an Application Program

../_images/app_components-en.png

An end user and an app’s components.

Many application programs are thought of as having two main components:

  • A user interface or UI (käyttöliittymä), which is the part of the program that is visible or otherwise observable by the user and that the user may interact with. Many modern applications have a graphical user interface (GUI) that consists of windows, buttons, and the like.
  • A model of the program’s domain: what the program is about. This is the program’s “guts”; it’s what defines the program’s internal logic. For instance, the domain model of a word-processing application defines how proof-reading works and how documents are internally structured as paragraphs, headings, etc.

An application’s user will usually be familiar only with how to use the app (if that). In contrast, the programmer needs to be able to describe both the user interface and the program’s internals. Moreover, unlike the user, the programmer needs to examine and manipulate both the program’s dynamic behavior and its static representation as program code.

In the remainder of this chapter, we’ll explore two existing application programs. We’ll look at them from the inside as well as the outside. As we do so, you’ll get to grab your programmer’s toolbox.

The Tools of Programming

Programmers routinely use auxiliary programs previously created by other programmers, which help them create new programs. For instance:

  • Program code is written and saved in files with an editor program. (More about that very shortly.)
  • A compiler (kääntäjä) is a program for automatically converting program code written by a human into a form that’s more readily executable by a computer. (More on that in Chapter 5.4.)
  • A debugger helps the programmer monitor a program’s behavior as the program is being run. This is particularly useful when looking for errors and when learning to program.
  • As a piece of software is being gradually developed, version control (versionhallinta) can make the evolving project more convenient and reliable to work on. (More on that in O1’s follow-on courses.)
  • Virtual machines are commonly used as an enabler when running programs. (More on that in Chapter 5.4.)

Integrated Development Environments (IDEs)

Editor
Compiler
Virtual Machine
Debugger
Version Control
Debugger
Compiler
Editor
Version Control
Virtual Machine

Integrated or separated?

A programmer can use a standalone utility for each need such as editing or debugging. The alternative is to use an integrated development environment (IDE; sovelluskehitin). An IDE provides a combination of tools for authoring software and helps the programmer divide their work in different projects, which is often convenient.

In O1, we recommend that you use an IDE. This ebook has been phrased to assume that you use one particular IDE:

Eclipse with Scala IDE

../_images/eclipse_logo.png

The Eclipse logo.

O1’s official programming toolkit is an IDE called Eclipse.

All in all, Eclipse is versatile and complex, as IDEs tend to be, and it can be further extended with various plugins. However, we’ll use only a small subset of Eclipse’s functionality and one plugin.

The basic version of Eclipse works for writing programs in the Java programming language. Since we’ll be using Eclipse for programming in Scala, we need a plugin called Scala IDE.

We use version 4.7 Oxygen of Eclipse and version 4.7 of Scala IDE.

Both Eclipse and Scala IDE are free to install and use.

This isn’t the only way

Some students in O1 may already be familiar with programming tools that they’d prefer to keep using. Even though we officially use Eclipse, you’re free to use other tools instead. However, you choose to do so at your own risk, counting on your own ability to work out any kinks that may arise. O1’s staff will probably not be able to assist you with unofficial tools.

Local students at Aalto can use Eclipse on Aalto IT’s Linux workstations, which have the right version of Eclipse installed.

Eclipse at Aalto

The right version of Eclipse and Scala IDE has been installed on Aalto IT’s Linux workstations. These workstations are O1’s official working environment. You can program on Aalto IT’s Linux workstations without having to install anything yourself.

Please note that at least most of Aalto’s Windows workstations do not have the necessary tools installed.

Installing Eclipse on Your Own Computer

If you want to program, say, on your home desktop, you’ll need to install Eclipse and Scala IDE. We have a separate page that explains how to do that. To ensure that you install the right versions, download the tools via the links on that page.

The rest of this page assumes that you are at a computer with a working installation of Eclipse and the Scala IDE plugin. The same goes for the chapters that follow: you should have access to Eclipse as you read so that you can try things out and experiment as you work your way through each chapter.

If you’re using Eclipse on your personal computer and have already installed it as per the instructions linked above, you can skip the section immediately below and continue from Programs in This Ebook, further below.

Launching Eclipse for the First Time

Preparations

Before you start, it’s a good idea to create a folder (directory) for storing the programs you’ll be working on:

  1. Come up with a name for the folder, such as O1Workspace or MyPrograms.
  2. Create a folder with that name wherever you prefer. You could place it within your home folder, for instance.
  3. If you’re working in a multi-user environment such as Aalto IT’s computers, please make sure other users have no access rights to the folder, so you won’t inadvertently participate in plagiarism.

If you’re working at the Linux command line, you can do the above with the following commands:

dodo ~ 51 % mkdir O1Workspace
dodo ~ 52 % chmod go-rwx O1Workspace
dodo ~ 53 %

The mkdir command above creates a O1Workspace folder within the home directory. The chmod command removes any read, write, and execution privileges of all other users on that folder.

Starting Eclipse and the Scala Perspective

Here’s how to start Eclipse for the first tme:

  1. On Aalto IT’s Linuxes, you’ll find Eclipse in the menu as Eclipse Scala IDE 4.7.1. Make sure you pick the right version.)

    In other environments, you can launch Eclipse by clicking the executable file (eclipse.exe or similar) within the installation folder.

  2. When you start it, Eclipse will ask you which workspace it should load up. Select the folder you created earlier.

  3. Eclipse loads after a brief wait and displays the contents of the selected workspace. Not that there’s much to display there yet.

    • In some Eclipse installations, you may first find yourself at a “Welcome” page with Eclipse-related news and the like. You can close it by clicking the X in the top corner.
  4. Eclipse supports multiple configurations known as perspectives, each with its own purpose. For instance, there’s a perspective for programming in the Java language and a perspective for programming in Scala. Depending on how Eclipse and its Scala plugin have been installed, when you first launch Eclipse, you’ll be in either the Java perspective or the Scala one. In O1, you need the Scala perspective, so make sure it’s active: check that this icon with an S is selected in Eclipse’s top right-hand corner: scala_perspective_icon.

    • If you see that icon, but it’s not “pressed down”, press it. If you don’t see the icon at all, go to the menu and select Window ‣ Open Perspective ‣ Other ‣ Scala.

Assuming everything went right, you should now see a window that looks pretty much like this:

../_images/eclipse_first.png

O1’s settings file

Now that you’ve launched Eclipse for the first time, you should also import O1’s preferences file. This makes a few adjustments to Eclipse’s default settings so that Eclipse works better for you in O1. Do this:

  1. Download the preferences file o1_2019.epf and save it on your desktop or somewhere else.
  2. In Eclipse, select File ‣ Import... ‣ General ‣ Preferences and press Next.
  3. At From preference file, indicate the location of the file you saved, o1_2019.epf.
  4. Make sure that Import all is ticked.
  5. Press Finish.

Now you’re finally ready to start using Eclipse.

Programs in This Ebook

This is going to happen frequently in O1: we take a more or less complete program and discuss it in the text, then have you extend or develop it in an assignment.

One reason we do this is that you get to learn from examples; another is that this is actually a very typical scenario even for a professional programmer. Programmers often don’t start from scratch but modify an existing system or use existing program code as a part of a new program.

Here’s one:

The GoodStuff project

Let’s take a look at GoodStuff, an example program designed for the purposes of this course. Goodstuff is an “experience diary” application: it lets the user record their experiences and the ratings they’ve given to those experiences. An experience could be a stay at a hotel, for instance.

As given, GoodStuff is quite basic and lacking in many ways. It does contain the core of a better application, however, and most importantly, it’s suitable for introducing many programming concepts. We’ll be making use of GoodStuff repeatedly in the chapters to come.

Even though we haven’t yet taught you enough about programming to understand the code that defines GoodStuff, you can already explore the program. This will give you an overview of what an application looks like in Eclipse and how you can manipulate it as a programmer. This is what we’ll do next in this chapter:

  1. Fetch the GoodStuff application into Eclipse.
  2. Paint a broad-brush picture of the app’s main components.
  3. Run the app within Eclipse and see how it looks from the user’s perspective.
  4. Take a peek at the program code and fix a tiny error in it.

A Program in Eclipse

Fetching an Existing Project into Eclipse

  1. Download GoodStuff.zip from the online folder. Save it to your desktop, for instance.
  2. In Eclipse’s menu, select File ‣ Import... ‣ General ‣ Existing Projects into Workspace and press Next.
  3. Choose Select archive file and indicate the location of the GoodStuff.zip file that you just saved.
    • Exception: Some browsers (at least: some versions of the Safari browser) extract the contents of any zip file automatically and discard the original zip archive. In such cases, you should instead choose Select root directory and find the folder where your browser put the contents of GoodStuff.zip. Also make sure that Copy projects into workspace is ticked.
  4. You should now see two projects listed: GoodStuff and O1Library. Make sure both are ticked and press Finish. This extracts the files you need from the zip file and copies them into your O1 workspace.
  5. If you want, you can now remove GoodStuff.zip. Your workspace already contains a copy of its contents.

Projects and packages

../_images/eclipse_imported.png

Two projects in Package Explorer.

In Eclipse, programs are located within projects. A project is not a programming term as such; it’s just Eclipse’s way of letting us organize files and settings. In O1, we’ve placed each program — with the exception of some very small ones — in a separate project. For instance, now that you imported GoodStuff, it’s visible as a project in the Package Explorer tab on the left-hand side of Eclipse’s user interface.

The other project now in your workspace is called O1Library. Keep it in your workspace throughout the course. Many of our example programs, GoodStuff included, don’t work without the O1Library toolkit. Apart from that, you don’t have to pay O1Library any mind at this point.

../_images/eclipse_project.png

The GoodStuff application’s program code is divided in four .scala files.

Browse the files in the GoodStuff project in Package Explorer. As is typical of Scala programs, GoodStuff’s components are defined in files whose name ends in .scala. These files are divided into packages (pakkaus); you can think of packages as a Scala equivalent of file folders. One use for packages is to structure a program into groups of related components, which is particularly helpful with large programs. For instance, we can define a package that contains a program’s user interface to be separate from the package that defines the internal domain model.

The GoodStuff program has four parts: Category, Experience, GoodStuff, and CategoryDisplayWindow. The first two model the program’s “guts”, namely the experiences that are recorded in the diary and the categories that those experiences fall into. These parts are located in package o1.goodstuff. The latter two implement the program’s user interface and are located in package o1.goodstuff.gui.

Those two packages contain the program code that defines the GoodStuff app. A project may also contain other files. For instance, GoodStuff and many other O1 projects have a doc folder that contains the project’s documentation. We’ll have more to say about documentation later on (in Chapter 3.2).

Let’s run it already, please

Any Scala application will have a file that serves as the application’s launch file. In the case of GoodStuff, the launch file is GoodStuff.scala, which is in package o1.goodstuff.gui. Select this file in Package Explorer, then go to Run ‣ Run As ‣ Scala Application in the menu. The application’s GUI launches in a separate window and looks like this:

../_images/godstuff.png

Since this example application will show up multiple times in forthcoming chapters, take a moment to get familiar with it, beginning with what the app’s user sees. Spend a bit of time using GoodStuff: click Add new hotel and make up some data to enter into the program. Notice the following:

  • This simple application supports only a single category of experiences: accommodation in hotels.
  • The application computes a value-for-money figure for each experience by dividing its rating by its price.
  • The user’s favorite experience grins at you. The experience with the highest rating is considered the favorite.

Finally, close the GoodStuff window. The program run terminates.

Hint: alternative ways to launch an app

The Run command is also available in the context menu that pops up if you right-click the file name GoodStuff.scala in Package Explorer. A third way to launch an app is to click the play icon play in the toolbar near the top of the window; a fourth is the keyboard shortcut Shift+F11.

During O1, you’ll be launching apps a lot. Do it whichever way you prefer.

Those ways of lauching an app require you to first select the launch file. Alternatively, you can press Ctrl+F11 (Cmd+F11 on a Mac) to launch whichever app you most recently launched (without selecting it). This is often convenient.

A Peek at Program Code

Double-click the names of code files to load up the files’ contents in Eclipse’s editor. Try it!

Comments

You’ll soon notice that the code doesn’t consist of commands in Scala only. It also contains a substantial amount of text in English, which has been colored green by Eclipse.

It’s common for program code to include such explanatory text, known as comments (kommentti). Comments can help other programmers figure out the purpose of the program’s components and how to use them. Even the programmer who originally wrote the comment may benefit from it when they return to work on a bit of code that isn’t fresh in their memory.

Program code isn’t just for computers!

In comments, we’ve come across something more general and important: even though programs are written for computers to execute, we must also take human readers into consideration. When used sensibly, comments can be a cornerstone of good programming style.

Marking comments in Scala

In the Scala language, text between the tags /* and */ is a comment and has no effect on program execution. You can find examples throughout the GoodStuff application.

An alternative is to use the double-slash //, which marks the start of a comment that runs until the end of the line. Eclipse’s Scala editor, like many other program editors, automatically uses color to highlight comments so that they stand out from the rest of the code.

In O1, we usually won’t require you to include comments in the programs that you write. It can be a good idea to write them nonetheless. In any case, you’ll be reading a lot of example code that contains comments.

Take a look around

Spend a few minutes browsing GoodStuff’s code. Even though most of it is likely to be incomprehensible — even the comments contain unfamiliar programming terms — you’ll get a sense of how a Scala program looks like. Perhaps you’ll also find bits of code that make sense to you or whose behavior you can guess. But if nothing makes any sense yet, that’s okay, too.

In the next chapter, 1.3, we’ll start, little by little, to get familiar with specific programming concepts and the matching commands in Scala. Eventually, you’ll be able to understand GoodStuff’s code, and more complicated programs, not to mention being able to write programs of your own.

Let’s Do Some Editing

Did you notice the typo in the title of the window that hosts the GoodStuff GUI? It says GodStuff!

It doesn’t take master programmer to fix that. Let’s apply the fix and take the opportunity to submit a program for automatic assessment.

The window title is defined as part of the GoodStuff GUI, more specifically on line 35 of CategoryDisplayWindow.scala.

Do the following:

  1. Add the letter o where appropriate on that line.

  2. Save the file in the File menu or by pressing Ctrl+S (Cmd+S on a Mac).

    Remember to save!

    From past experience, we know that students sometimes forget to save their programs. They then submit out-of-date drafts for assessment, weep, and gnash their teeth. So remember to Ctrl+S!

  3. See if it worked: relaunch the program from the Run menu, for instance. The window title should be okay now.

  4. Send the file you modified (CategoryDisplayWindow.scala) for assessment through the form below.

    • Many web browsers let you drag and drop the file straight from Eclipse into the field below, which is convenient. (If it doesn’t work for you, click the button below and locate the file within the workspace folder that you created earlier.)
    • A+ will give you feedback and a few assignment points.

Submission form

A+ presents the exercise submission form here.

Another Editing Task: The Game of Pong

Preparations

Explore another application, a version of the classic video game Pong. Find the program within the given Pong project and try it out, like you did with GoodStuff. Here are the steps in brief:

  1. Use the link at the top of this page under Related Projects to pick up the zip archive that contains Pong.
  2. File ‣ Import...
  3. Existing Projects into Workspace and Next.
  4. Select archive file, indicating the appropriate zip file.
  5. Make sure that Pong is ticked and hit Finish.
  6. Run the program: choose PongApp.scala within o1.pong.gui and launch it (use, e.g., the play icon or Shift+F11).
  7. Use W or S to move the paddle on the left and the arrow keys to move the one on the right.

Your assignment, part 1 of 2

In the package o1.pong there is a file called package.scala. Among other things, this file defines some numerical values that shape the game. Open it in the editor.

One of the lines near the top defines something called BallRadius. On that line, replace the number 10 with 50.

Save (Ctrl+S). Relaunch the game. You’ll observe that something changed.

Your assignment, part 2 of 2

In the same package, there’s a file called Ball.scala, which defines the rules that the ball follows as it moves. One of the lines says:

this.velocity = this.velocity.switchY

This command causes the ball to change direction as it bounces off the top or bottom of the playing area. Comment out this line. That is to say, write two / characters at the beginning of the line, turning it into a comment and causing it to no longer be an executable part of the program.

Save. Relaunch the game.

Does our game now have a bug, an error in behavior? Well, we did intend to make that change, so it’s not a bug, it’s a feature.

Submission form

Use this form so submit the files you modified.

A+ presents the exercise submission form here.

Fooling around with Pong

If you want, you can experiment with other alterations to Pong. Here are some ideas:

  • What happens if you change Size and Thickness in package.scala to have different numeric values?
  • Can you locate the bit in package.scala that defines how fast each paddle moves?
  • Can you find any bits of code that define the colors used in the GUI? See if you can change the colors.
  • Looking at PongApp.scala, can you figure out how to change the keys that control the paddles?
  • What if PongApp.scala said push(Right), not push(Up)?

Addendum

If you enjoyed messing about with Pong and seeing what happens, you might also enjoy this quote, which also contains a deeper message about the static-and-dynamic duality of programs:

One of the first items covered in an introductory physics course is the difference between potential and kinetic energy. You can think of potential energy as the energy stored in, say, a battery or a rock placed on top of a hill. Kinetic energy is energy that is in the process of being converted, as when the stored electricity in a battery drives a motor and when a rock rolls down a hill.


Similarly, when a human designs a program, there exists a potential computation that is unleashed when the program executes within a computer. Thus, one can think of the computation as being kinetic and in motion.


Moreover, just as a child with a firecracker can be surprised by the difference between potential and kinetic energy, so computer programmers are often surprised (even pleasantly) by the difference between potential and kinetic computation.


—Gary Flake (in his book The Computational Beauty of Nature)

Programming as an Activity

Let’s finish the chapter by reflecting on what programming is like.

Programming as problem solving

Typically, a programmer’s work begins with a problem or need that calls for a solution. Here are some examples of problems that can be solved through computing.

  • One wishes to speak to people on the other side of the planet over the internet.
  • One needs a novel accounting application for a company.
  • One wants to know the best route for a car to reach its destination. Or for a spaceship.
  • One seeks a model that describes or predicts people’s behavior in a social network, the spread of diseases, climate change, or historical trends in how common different words are.
  • One wants to determine which additional products to recommend whenever customers make purchases in an online store.
  • One needs a tool that converts scientific measurement data into a format that is convenient for analysis.
  • One wishes to simulate the movement of subatomic particles in order to predict phenomena.
  • One wishes to simulate the movement of emotional avians accelerating through a gravitational field towards hoofed mammals.
  • And so on. And so on.

Interesting problems are invented all the time, both by programmers themselves and by others. Even if a problem is familiar, we may seek solutions that are better than those that exist.

Algorithms

Programmers often talk about algorithms (algoritmi). Roughly speaking, an algorithm is a stepwise method for solving a particular problem.

Baking, too, involves algorithms, even if the term isn’t used by bakers: in our example, the master baker’s notion of how to make the cake is an algorithm that she invented. The baker wrote the method down as a recipe. In the same way, a programmer implements algorithms — solutions to problems — as program code.

Parts of a problem may be solved with different algorithms. For instance, a word processor will use one algorithm for proofreading and another to provide a find-and-replace function. As a programmer, you can invent the algorithms you use for some subproblems and apply algorithms previously created by others to solve others. In any case, you’ll need to figure out how to implement and combine the implementations into a complete program.

A mini-assignment: algorithms and programs

Of the following statements, which one can be best described as correct? Please note that you receive feedback for both correct and incorrect answers. That feedback is part of the text of this ebook; please read it.

What programming is like

We’ve established that programming is one form of problem solving. A programmer needs to determine what algorithm solves a problem and how to present that algorithm to the computer as a program. There is practically never just a single possible solution, which is why programmers must compare different options and consider how to create a high-quality program. High quality might mean that the program needs to be fast, user friendly, secure, or easy to extend, for instance.

Programming calls for systematic thinking, creativity, and meticulosity. Many people consider programming to be fun aerobics for the brain, and nearly everyone agrees that programming can be used to create useful tools and products.

Unlike people sometimes think, creating programs often revolves around humans at least as much as it revolves around machines. For instance, the ease of use and extensibility of programs must be assessed from a human perspective. And of course programs are created to meet human needs.

Programs come in very different sizes and so do teams that develop programs. Programs are created by individuals and by large, international organizations. In day-to-day programming work, human communication often plays a large role.

Some of the people who program as part of their job are full-time programmers who seek to produce high-quality software for commercial or other purposes. A still larger group are the varied professionals who use programming as a tool for solving problems in a particular field: financial analysts, social scientists, researchers in medicine and genomics, physicists, graphic designers, linguists, climatologists, engineers, etc. Some have harnessed programming as an instrument for art.

Someone with programming skills can create tools that make them more productive as they use applications such as spreadsheets or games and as they engage in other everyday activities. One might even say that programming is increasingly becoming a basic skill of the educated citizen, a skill that helps us benefit from the enormous power of computing and to understand the computerized systems that surround us.

Can you think of a program that you wish existed? That you would like to create yourself? What kind of program would make the world a better place?

Summary of Key Points

  • As a computer runs, or executes, a program, it follows a set of instructions created by a programmer. It follows them step by step, to the letter.
  • Programming is a creative activity that seeks solutions to problems that people find meaningful.
  • Programs are written in a programming language. In this course, we use a language called Scala.
  • We use an integrated development environment (IDE) named Eclipse and its Scala IDE plugin. An IDE brings together various tools that assist the programmer.
    • In Eclipse, you can edit code and run programs to examine how your edits impact on program behavior.
    • The parts of a Scala program can be organized in packages. A package may contain multiple program components defined in multiple files.
    • In Eclipse, separate programs are typically stored in separate projects. A project may contain multiple packages.
    • Many of this ebook’s chapters come with given programs that are more or less complete. They are provided to you as Eclipse projects.
  • Program code isn’t meant for just computers to process. One indication of this is that code can contain comments that clarify its meaning to human readers.
  • Links to the glossary: programming, program; application, user interface; domain, algorithm; dynamic, static; program code, programming language, Scala; comment; integrated development environment or IDE.

Feedback

Please note that this section must be completed individually. Even if you worked on this chapter with a pair, each of you should submit the form separately.

Credits

Thousands of students have given feedback that has contributed to this ebook’s design. Thank you!

Weeks 1 to 13 of the ebook, including the assignments and weekly bulletins, have been written in Finnish and translated into English by Juha Sorva.

Weeks 14 to 20 are by Otto Seppälä. That part of the ebook isn’t available during the fall term, but we’ll publish it when it’s time.

The appendices (glossary, Scala reference, FAQ, etc.) are by Juha Sorva unless otherwise specified on the page.

The automatic assessment of the assignments has been developed by: (in alphabetical order) Riku Autio, Nikolas Drosdek, Joonatan Honkamaa, Jaakko Kantojärvi, Niklas Kröger, Teemu Lehtinen, Strasdosky Otewa, Timi Seppälä, Teemu Sirkiä, and Aleksi Vartiainen.

The illustrations at the top of each chapter, and the similar drawings elsewhere in the ebook, are the work of Christina Lassheikki.

The animations that detail the execution Scala programs have been designed by Juha Sorva and Teemu Sirkiä. Teemu Sirkiä and Riku Autio have done the technical implementation, relying on Teemu’s Jsvee and Kelmu toolkits.

The other diagrams and interactive presentations in the ebook are by Juha Sorva.

The O1Library software has been developed by Aleksi Lukkarinen and Juha Sorva. Several of its key components are built upon Aleksi’s SMCL library.

The pedagogy behind O1Library’s tools for simple graphical programming (such as Pic) is inspired by the textbooks How to Design Programs by Flatt, Felleisen, Findler, and Krishnamurthi and Picturing Programs by Stephen Bloch.

The course platform A+ has been created by Aalto’s LeTech research group and is largely developed by students. The current lead developer is Jaakko Kantojärvi; many other students of computer science and information networks are also active on the project.

For O1’s current teaching staff, please see Chapter 1.1.

Additional credits appear at the ends of some chapters.

a drop of ink
Posting submission...

Submission received.