Difference between revisions of "What is Programming?"

From CompSciWiki
Jump to: navigation, search
Line 218: Line 218:
  
 
Lets say we entered "Hello", the output would be "The first character, H, occurs 1 times in the string entered.".  However if we entered "Hello how are you" the output would be the same even tho there is 2 h's in the string but only the first is capitalized which makes it a different character then lowercase.
 
Lets say we entered "Hello", the output would be "The first character, H, occurs 1 times in the string entered.".  However if we entered "Hello how are you" the output would be the same even tho there is 2 h's in the string but only the first is capitalized which makes it a different character then lowercase.
 +
  
 
==Review Questions and Exercises==
 
==Review Questions and Exercises==
 +
Answers to these questions can be found [[What is Programming Solutions|here]]
  
 
#What is the most important part of writing a program?
 
#What is the most important part of writing a program?
 
#Are you finished coding when your program is running? If there are more steps, what are they?
 
#Are you finished coding when your program is running? If there are more steps, what are they?
 
#What part of a piece of java software drives and controls it? What is the first line of this part?
 
#What part of a piece of java software drives and controls it? What is the first line of this part?
 +
  
 
==Further Reading==
 
==Further Reading==

Revision as of 00:07, 20 March 2007

Programming, simply put, is the act of writing computer programs. Everything you do on a computer, from playing Minesweeper to typing your homework requires the use of a computer program of some sort. Programs are essentially a series of instructions written down for a computer to follow

Writing instructions is a deceptively difficult task. People are very good at intuitively breaking down tasks into simple steps. Someone can tell you "go buy some bread", and you immediately form a series of steps without thinking of them:

  1. go upstairs and get your wallet out of your sock drawer
  2. put your wallet into your pocket
  3. go back downstairs and get the car keys
  4. drive to the store and park in the parking lot
  5. walk to the bread aisle in the store
  6. choose a loaf and pay for it
  7. drive back home

Some of these steps can even be broken down into smaller, more detailed steps. How do you walk up a flight of stairs? How do you drive a car? There is a near-infinite series of steps that you could think of to do a task as seemingly simple as buying a loaf of bread.

Fortunately in COMP 1010 you will not be writing anything nearly that complicated. Computers may be fast, but they are only as "smart" as their programmers. This chapter will give you a brief introduction to writing instructions for computers to follow.


The Programming Process

Just like the writing process for conventional writing, there is a process for writing good programs. This process can be broken down into the following steps:

  1. Define the problem
  2. Determine how to solve the problem
  3. Write out the instructions
  4. Write the program

Defining The Problem

All programs are written to solve some problem(s). The first, and arguably the most important, step in writing a program is ensuring the problem is well-defined. Ask yourself "what is this program supposed to do?" Once you have a good idea what it is you want the computer to do the process of writing the code is much easier.

Determining How To Solve The Problem

Once you have the problem defined the next step is to determine how to solve it. Think of a program as a set of instructions the computer has to follow. Every step should be as simple as possible; computers don't actually know how to do anything. You need to give them step-by-step instructions. The simpler the steps the easier they are to write down and code.

But before you sit down and write any code, you should decide how the program should go about solving the problem(s) presented to you. Important concerns to take into concideration at this point are simply:

  • What inputs will the program be provided with?
  • What kind of data processing will the program have to do to solve the problem?
  • What outputs are the users expecting?

Writing The Instructions

A useful technique when preparing to write any program is to first "pseudo-code" it. Write down the processing your program will perform in an english style syntax. Pseudo-code can help you identify how many of what types of variables you will need as well as any control structures that may be necessary. Here is the earlier program from we wrote in pseudo-code:


Begin Main
{
    dataString = getFromUser;
    firstChar = getFirstCharOfString;
    numOccurences = 0;
   
    loop(until end of string)
    {
        If the current character == firstChar, increment numOccurences;
    } 

    OutputResults;
}


As you can see, there are no specific syntax being used here. It is more like a half-english, half-code language to express with simplicity the process of the program.

Writing the Program

Once you have identified the nature of the problem, what needs to be done to solve it, the process through which you will solve it, and the variables you will use, you can sit down and begin coding. For a program of any length it is best to start off coding one small part at a time and testing it to assure it works before coding the whole thing and trying to track down the errors. Once your program is coded you can run it through the compiler to see if you have made any syntatical errors. After the program is up and running, it must be tested to ensure it produces the correct results. Tracking down and removing faults from a program is referred to as "debugging".

Anatomy of a Program

The Main Method

It is hard to define what makes up a program. For the purpose of this course and writing in java we will define a "program" to be the main method. Main is the method that drives every piece of software that you will write this year.

The main method begins with the line:


public static void main(String[] args)
{
   //your code here
}

The meaning of this line is too complicated to explain at this point. All you need to know is all java programs require the main function and the main function starts with this line. With the majority of your code with in the braces following this line as shown.

Declaring Variables

After this line it is standard to declare any variables that will be used in the main function. Note the closing and opening braces around the "body" of the the function. These tell the computer where any method begins and ends after its header (the magical "public static void main" line).


public static void main(String[] args)
{
// Declare variables
    int anInteger;
    char aChar;
    String aString;
    bool aBoolean;
}

This program now has access to four variables: anInteger, aChar, aString, and aBoolean.

Variable initialization

Usually after the variables have been declared you either set them to some initial value to be used in the program. This can be done either directly with some sort of assignment or from user input.


public static void main(String[] args)
{

// Declare variables
    int anInteger;
    char aChar;
    String aString;
    bool aBoolean;

//  Get input from the user.
    aString = JOptionPane.showInputDialog("Enter a string:");

//  Assign aChar to the first character of aString;
    aChar = aString.charAt(0);

//  anInteger always starts as 0 in this program;
    anInteger = 0;

//  aBoolean always starts as false in this program;
    aBoolean = false;

}

Note: booleans can be set with numbers also with 0 = false and all others equaling true.

Processing Data

The rest of the main method performs the work of the program. Suppose the point of this program is to iterate (cycle) through the characters in the aString variable and calculate the number of times the first character appears in the string.


public static void main(String[] args)
{

// Declare variables
    int anInteger;
    char aChar;
    String aString;
    bool aBoolean;

//  Get input from the user.
    aString = JOptionPane.showInputDialog("Enter a string:");

//  Assign aChar to the first character of aString;
    aChar = aString.charAt(0);

//  anInteger always starts as 0 in this program;
    anInteger = 0;

//  aBoolean always starts as false in this program;
    aBoolean = false;

//  Increase anInteger by 1 every time aChar occurs in aString
    for( int i = 0; i < aString.length(); i++ ){
         if( aChar == aString.charAt(i) )
         {
             anInteger++;
         }
    }


}

Don't worry if the processing section makes no sense right now. All the details of syntax will be revealed to you later.

Output and Cleanup

Before the main method ends, output any results or neccessary information to the user. Sometimes cleanup/maintenance operations must also be performed before the program ends.


public static void main(String[] args)
{

// Declare variables
    int anInteger;
    char aChar;
    String aString;
    bool aBoolean;

//  Get input from the user.
    aString = JOptionPane.showInputDialog("Enter a string:");

//  Assign aChar to the first character of aString;
    aChar = aString.charAt(0);

//  anInteger always starts as 0 in this program;
    anInteger = 0;

//  aBoolean always starts as false in this program;
    aBoolean = false;

//  Increase anInteger by 1 every time aChar occurs in aString
    for( int i = 0; i < aString.length(); i++ ){
         if( aChar == aString.charAt(i) )
         {
             anInteger++;
         }
    }

//  Output the result to the user
    System.out.println("The first character, " + aChar + ", occurs " + anInteger + " times in the string entered.");

}

Lets say we entered "Hello", the output would be "The first character, H, occurs 1 times in the string entered.". However if we entered "Hello how are you" the output would be the same even tho there is 2 h's in the string but only the first is capitalized which makes it a different character then lowercase.


Review Questions and Exercises

Answers to these questions can be found here

  1. What is the most important part of writing a program?
  2. Are you finished coding when your program is running? If there are more steps, what are they?
  3. What part of a piece of java software drives and controls it? What is the first line of this part?


Further Reading

The following pages may be of interest, but are not examinable material

Template loop detected: