Sunday, June 3, 2007

Arrays

Using arrays is a way to extend the storage of variables to include a list of values, instead of just a single value. It is a way for programmers to handle large quantity of data efficiently.

Arrays are specified using a square bracket (int marks[]) and individual elements in an array can be referred to by their index:
   marks[21] = 68;
marks[14] = 79;
Because the index is just an integer value, we can always use a variable to represent the index so that we can parse through the array elements one by one.
   for(int i=0; i<marks.length; i++)
{
marks[i] = 0;
}

Friday, June 1, 2007

Methods

Sorry for the non-update last week; was busy attending a conference.

The concept of methods is central to the OO (Object-oriented) methodology, and is also central to the understanding of any programming languages, since programming languages always comes with loads of pre-defined methods or functions (in the form of libraries) that programmers can use to simplify their programs.

As mentioned in the lecture, there are broadly 2 types of methods, the pre-defined ones and the ones that programmers write themselves within an application.

Java has loads of pre-defined methods that comes in packages and can be accessed at this URL (http://java.sun.com/j2se/1.5.0/docs/api/) for the JDK that we are using. You might not need it now, but the list will come in handy during your projects in later semesters when you need to write complicated Java programs.

For programmer-defined methods, you will need to remember to write the method header and method body (refer to Q1 of Lab 5), and to do a method call (refer to Q2 of Lab 5) in the main method so that the method gets executed.

Take note that the variables declared in a method is only valid within a method, and cannot be accessed outside a method. So if you used a variable num1 in a method findAverage(), you need to declare it in the method itself and not in main().