Craig Mattson (Personal Website)
Home - Blog, News, About MePrograms - C#.Net, Java, VB6MusicWebsites

Viewing News Article

Programming Concepts (30/05/2007 11:45:41 AM)

 Hi all,

I'm sure you are well and truly sick of reading about useless (useful?) crap about the wonderful world of Technology, but the way I look at it is if I can help someone out, even the slightest, then it's been worth posting a new blog up. Todays topic (yes, I'm talking about the same day I wrote this on (12:44AM in the morning) is about programming concepts as once again, exam times are coming up and too many students will fail this exam, not because they haven't studied their specific programming language in great detail (ie know the commands, know the importance of programming structures whether it be an Object Oriented approach or a Structured (line by line) approach.) but because they simply do not know how to use logic to create a program.

One of the many things I am appauled to see still happening is there is no emphasis on teaching logical programming. That is, teaching someone how to write a program rather than teaching them syntax. Take any other spoken language such as Japanese, French or even Arabic. You see, it's quite easy to memorise the alphabet, words and even string a few words together to form some sort of coherent speech, but to actually think off the top of your head how to put those words, especially when not taught how to talk pretty much renders your memorisation of words useless. For example, a typical greeting would be "Hello, my name is . Nice to meet you". There is a reason we string these words in this order, and that is because it is considered a logical way of doing so. You wouldn't say "Nice to hello you, my name is meet " because it means something totally different, or even cause so much confusion that no one knows where to begin. The same applies to computing and programming languages.

A Simple Structure:

Lets assume we have a program to store a list of your contacts phone numbers and e-mail addresses. Quite an easy example if you know what you are doing, and can be implemented in all languages. As the people who read this blog have some knowledge of Java, I suppose I had better write this concept using Object Orientation Smile

If I was to hand this example to my current group of tutee's, I am sure they wouldn't know where to begin simply because they have not learnt any concepts in how to think out the program, so here are some basic steps in solving this problem.

Step 1. Collect your data

One thing that most people seem to forget about doing is working out what data is needed for the program to work. Typically in this phonebook example, we have 3 things we need. We need a persons name, the persons email address and the persons phone number. Nothing more, nothing less. We however want say 100 people stored maximum.

Step 2. What do we need to do with this data

Now that we have the data we want, we have to work out what we are going to do with the data. Obviously we want to be able to store these 3 attributes (name, email, phone), and at some stage we would like to manipulate these attributes aswell such as changing the name, email or phone and even retrieving a persons name, email or phone. To do this, we have 6 things we want to do.

Step 3. How are you going to manipulate the data

In this example, we will want to have a user to enter some information, and we also want to get this information aswell.

Step 4. What are the steps to achieve this manipulation

Now that we have all of the information we require, we can start investigating the program. As you can see in step 3, there is only 2 lines there and for those who know any programming language, there isn't a method that randomly springs out of a box to do these things exactly, so we have to work out how we are going to do each of these tasks.

"have a user enter some information..."

So, thinking about this logically in a precise nature, this is the order one would expect to enter data in:

  1. Enter a users name
  2. Enter a users phone number
  3. Enter a users e-mail address
  4. Store the name
  5. Store the phone number
  6. Store the e-mail address

"get the information..."

Again, thinking logically in how to get the information, we need to do the following:

  1. Enter a name
  2. Search for the name
    1. If the name is in the phone book then get the phone number and email address
    2. If the name is not in the phone book, then tell me its not there.

Now that we have some methods written up in some way, we need to use all information gained and put it into a program.

Step 5. Apply this information

Ideally, you will have written your methods suitable to the programming language you know. In this case, Java will be used and as it has an emphasis on Object Orientation, I'll implement it using Objects.

The Java Program:

As we seen from Step 1, we needed 3 pieces of data. To store data, Java includes data types and variables. Therefore, we need 3 data types that will store text and numbers beginning with 0 into this book. Anyone who knows any programming language will realise String achieves this, therefore we can place the following information down:

String name;
String phone_number;
String email_address;

You will notice aswell, these are all properties of a persons phone number, and as they all deal with people, then we can create a class to store these in.

public class Person {
    private String name;
    private String phone_number;
    private String email_address;
}

Above now is an object that contains 3 properties that are hidden away from the application. Anyone who has learnt Java syntax should realise this.

Step 2 contained what we wanted to do with the data, that being we want to store it and get it. We use accessors and mutators in Java to do this. Accessors return something and Mutators set something. Again, if you know Java syntax, this next step isn't tricky.

public class Person {
    private String name;
    private String phone_number;
    private String email_address;

    public Person(String new_name, String new_phone, String new_email)
    {
        this.name = new_name;
        this.phone_number = new_phone;
        this.email_address = new_email;
    }
}

This above code now creates an instance of the object using the constructor method when the person is made. In other words, very important for this program. Now, we need to implement the methods from Step 2 which were we need to get the data and set the data.

public class Person {
    private String name;
    private String phone_number;
    private String email_address;

    public Person(String new_name, String new_phone, String new_email)
    {
        this.name = new_name;
        this.phone_number = new_phone;
        this.email_address = new_email;
    }

    public void setName(String newName)
    {
        this.name = newName;
    }

    public void setPhone(String newPhone)
    {
        this.phone_number = newPhone;
    }

    public void setEmail(String newEmail)
    {
        this.email_address = newEmail;
    }

    public String getName()
    {
        return this.name;
    }

    public String getPhone()
    {
        return this.phone_number;
    }

    public String getEmail()
    {
        return this.email_address;
    }
}

Now all appropriate methods are implemented, we need a driver class, that is - something to run the program. We design this class by thinking about the overall scope of the application. For starters, we need to store 100 peoples details, and we need to take care of the methods in Step 4. This is how it would be structured, and should be easy if you know the Java syntax.

import java.util.Scanner;

public class AddressBook {
    private Person person[];
    Scanner kb = new Scanner(System.in);
   
private boolean isRunning = true;

    public AddressBook()
    {
        person = new Person[100];
    }

    public static void main(String args[])
    {
        AddressBook ab = new AddressBook();
        while(ab.isRunning)
        {
            System.out.println("Menu:");
            System.out.println("");
            System.out.println("1. Add Details");
            System.out.println("2. Search Details");
            System.out.println("3. Exit");
            char choice = kb.findInLine(".").charAt(0);
            switch(choice)
            {
                case '1':
                    ab.addDetails();
                    break;
                case '2':
                    ab.searchDetails();
                    break;
                case '3':
                    System.exit(0);
                    break;
            }
        }
    }
}

There's the basic outline for the menu, and the rest of the methods will be implemented in the next news post. Right now, I have things to do... like sleep Yell

Thanks,
Orb!ter

[Print View]