Programming Progressively: A journey into programming as problem solving

I am starting a little journey making an attempt to teach programming. Quite too often, programming is taught from the language point of view - showing the data types, the control structures, the libraries, etc. Here, we will take a different approach - or rather we will try to!I dont know how much my enthu will last for this adventure. It will also depend on how people (you, the readers/friends) react to this. Feel free to comment on the posts, I will try to absorb them into the writing as I go along.

I will use Java as the programming language. But I dont think, my approach and content will depend too much on the language. The attempt is not to make you a master of Java (or whatever language); but rather to make you understand programming. Introducing constructs, as we need them. Showing how the constructs help you solve problems.

Ready? Fasten your seat belt and come along….

We begin our journey here. We will spend a few minutes getting ready - setting up our rules and norms for the journey, making sure we have the needed resources, and the like.

First of all, this is a course on programming. It is like swimming - no amount of reading will make you good at swimming. You need to swim. You may not succeed at first. You may sink into the water, may drink a lot of water - much beyond your liking - it is all part of the learning process. But dont drawn - that is where systematic learning comes in. It is foolish to jump into deep sea, and try to learn swimming. It is foolish to go and practice with no security devices (floating devices, the guards on duty, etc). The lessons and specific targets play that role - so that you dont get hurt too much. Given that, dont be afraid to try. You will develop confidence - you will know what works well for you and what does not. You will know where your weakness is and where your strengths. Slowly you can build your own style of swimming (read, programming).

So, it is important you have a programming environment to practice. In the beginning I would suggest not using any development environment. The programs will be really simple, and an IDE will hide important details. As we go further, you can try them. For now, just make sure you have a simple text editor (the classic vi, notepad, and so on). Do not use word processors - they do not store the files in plain text format. They have to store formatting and the like, and hence makes it impossible for the compiler to read your program. So, ensure you have a simple plain text editor. And ensure you have Java installed. You can download and install it from java.sun.com. It will give you two commands: javac and java.

javac is the command which turns our Java programs into a format that the Java interpreter understands. For now, dont worry too much about what this means. And the command java executes your program. You provide inputs and receive outputs, at this stage.

So,

  • You write a program using the text editor. Let us say, the file is called pgm1.java; note that the extension is important. You can name the file in anyway you like. You can name them based on the chapter in which the program is described, or based on the nature of the program. Eventually, you should it more along the latter lines. Eg. chap1ex1.java (first exercise in first chapter), hello.java (program to print hello), etc.
  • Then you compile the program using javac. "javac pgm1.java". This will create a file called pgm1.class in your directory. Watch for any errors, etc. As a practice, make sure no warning is left. Even though some warnings may appear harmless, fix them. It is a good practice - like stopping swimming, when you notice mild discomfort. It prevents major problems later on.
  • And then you can run the program with "java pgm1" - it will run the compiled .class file.

For every exercise, do all these, and ensure you test out your program well that it works for a number of different inputs. Dont stop with one input. Programs can surprise you - working well for 99 inputs and failing on the 100th. We will later see such errors.

The second part of the preparation is to prepare some skeleton for us to start. For, even the simplest Java program to work, we need a few lines of code. Whenever you write a program (we will withdraw this restriction, later on), include this in your .java file exactly as it is. And your program will be written in the part marked "YOUR PROGRAM". Some examples in next chapter will make this convention clear. The significance of this skeleton part will become clear as we go along. For now, just trust me and come along.

public class Myclass {
     public static void main (String arg[]) {
            YOUR PROGRAM
     }
}

The name "Myclass" should be replaced with the name of the file you are going to save the program in.

We are now set to start our journey.

Next page

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License