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().

Sunday, May 20, 2007

Repetition Structure

This week we continue with the 3rd control structure of programming, namely Repetition Structure.

Repetition structure is used to make certain portions of the program code run repeatedly, to achieve certain logic that you want. There are 3 Java statements to do repetition, while(), for() and do-while(). Refer to your lab 4 or the textbook for the syntax.

The characteristics for these loops are:

while() loop:
  • this is a pre-test loop, meaning the condition is tested before going into the loop.
  • for a counter-controlled loop, need to specify the starting value for the counter (int i = 0;), the continuation condition (while(i<5)) and the increment (i++;). The starting value is to be specified before the loop, and the increment is to be within the loop.
  • for a sentinel-controlled loop, need to ensure that the value to be checked/compared against the sentinel value is being changed within the loop, either by some calculations, or by requesting input from the user.
for() loop:
  • a for() loop works exactly the same as a counter-controlled while() loop by collecting all the 3 necessary statements and putting them together in the for() statement.
  • a for() loop is particularly useful when we are dealing with arrays.
do-while() loop:
  • a do-while() loop is a post-test loop, meaning the statements within the loop is ran first before the condition is checked/tested if the loop is to be repeated.
  • the only difference between do-while() loop and while() loop is that statements are ran at least once for a do-while() loop, and statements may not be ran at all for a while() loop.
  • for the while() statement in a do-while() loop, you need to put a semi-colon (;) at the end of the statement.
That's about the important things that you should take note.

Tuesday, May 15, 2007

More selection structure

Another selection structure which achieves multiple branching is the switch() structure. This structure is a multiple if() and the broadest use it to create a menu structure for a program.
switch(integer variable)
{
case C1: //Write statements to do if variable == C1
break;

case C2: //Write statements to do if variable == C2
break;

default: //Write statements to do if variable is other values
}
Things to note for a switch() structure:
  • it can only evaluate an integer value (including char)
  • values for the cases are exact values and it does not allow range of values (e.g. C1 > 20)
  • all cases should have a break statement to prevent statements from falling through

Selection structure

The Selection structure is primarily achieved by using the if() condition (flowchart attached below).


The if() condition allows branching of program codes based on a condition. The general structure looks like this:
if(condition)
{
// Write statements to do if condition is true
}
else
{
// Write statements to do if condition is false
}
We can also nest (put) if() conditions within another if() conditions to form complex logic for our programs. Do take care of the curly brackets, and always use tabs to indent the statements properly to aid debugging.

Things to note about if() conditions:
  • an if() can have one and only one else or not at all
  • a else must come with a if()
  • if() conditions are independent of each other

Sunday, May 6, 2007

Comparing Strings

As mentioned in the previous post, relational operators are meant for comparing variables declared in the primitive data types, and not strings. Since strings are objects of the String class, we need to use methods to perform the comparison.

We introduced 3 methods for comparing strings.
  • compareTo() - This method compares 2 strings and returns an integer value. If the 2 strings are exactly the same (including the case), a 0 will be returned. Else, the method will return the difference between the 2 strings. Using the return value, we can use it to help us sort strings. Refer to Lab 3 for an example, which I will also elaborate in class.
  • equals() - This method also compares 2 strings but returns only the boolean values TRUE (when the strings are exactly the same) or FALSE (when the strings are not the same).
  • equalsIgnoreCase() - This method does the same thing as equals() but ignores the case.
We will be using these methods in Lab 3 and I hope it becomes clearer to you how the method works, when you get to use them in real programs.

As usual, leave comments if you have questions.

Comparing Numbers

In Java, we use relational operators (==, !=, >, <, >=, <=) to help us compare numbers, which are represented by variables of the 8 primitive data types. Take note that we cannot compare a string with a number; we can only compare (use the relational operators) variables declared in the primitive data types.

The common error that students always make when writing relational operators is to mixed up the double equal sign (==) which is to check if 2 values are the same, with the single equal sign (=) which is to assign a value to a variable.

Control Structures in Java

Control structures are the basic building blocks of any programming language, and hence is not specific to only Java.

There are 3 basic control structures for Java:

  • Sequential - This means that all Java statements are executed line-by-line, one after another. This is already inherent within the language itself as you write the codes.
  • Selection - This control structure instructs the computer to make a selection (or branching) of the portion of the program based on a decision, made by doing a comparison. There are 2 selection structures in Java, the if() and switch() structures.
  • Repetition - This structure instructs the computer to perform a repetition of a block of code, and there various methods to terminate the repetition. There are 3 repetition structures, the for() loop, the while() loop and the do-while() loop.
A complete computer program often needs to make use of a combination of these control structures. Do make a note of these structures as we will go through them in detail in the coming weeks.

Sunday, April 29, 2007

Simple Input and Output

We have also started on doing some simple input and output using Java, because a program is only useful if it can interact with users to get inputs to the program and produce output for the user to see.

In Java, there are 2 ways to perform input and output, namely through the command line and through GUI.

Command Line
  • To perform an output to the command prompt window, you would use System.out.print() or System.out.println().
  • To get an input from the user, you would use a Scanner class, as in the example program in Lab 1 Practice 4.
  • You use entry.nextInt() to get an integer value, entry.nextDouble() to get a double value, and entry.nextLine() to get a string.
Graphical User Interface
  • To perform an output to a GUI window, you would use the showMessageDialog() method in the JOptionPane class.
  • To get an input from the user, you would use the showInputDialog() method, as in the example program in Lab 1 Practice 5.
  • Do take note that getting input from the user in a GUI is a 2-step process. The input value is always captured as a String object and you need to convert it into the appropriate data types in the 2nd step.

Data in Programming

This week we started with basic programming in Java. First off, we need to understand the concept of Data in Programming.

Every computer program needs data to manipulate to make it useful. Data are values that the program uses when it is performing some mathematical calculation or simple using it as a counter for loops. Data are stored as variables in Java and we went through the way to declare variables in Java. Declaring variables ensures that the computer reserves memory storage areas for our variables. In declaring variables, the key thing to note is that every variable has to be associated with a data type, so that the computer knows how much memory space to allocate. Do read up Chapter 2 of the Johnson book to get more details of variable declaration and data types.

Some examples of variable declaration:
   double myLength, myDepth;
int englishMarks;
boolean isOK;
We have also seen how to perform arithmetic operations (+, -, *, /, %) in Java. Basically, arithmetic operations are evaluated the same way as in mathematics, with the exception of the '=' operator, which acts as the assignment operator. The assignment operator will take the result (value) of the evaluation on the right-hand-side and store it into the variable on its left-hand-side.

For example, in the program code:
   a = 2, b = 8, c = 4;
answer = b * b - 4 * a * c;
the evaluation on the RHS will result in a value of 32 and this will be stored in the variable answer.

As usual, do leave comments if you have questions.

Sunday, April 22, 2007

First Week

As with all first weeks of your life (and mine too, although its a little longer than yours 8-), its hectic and full of unknowns. Not knowing where the classrooms are, not knowing what the module is about, not knowing if the lecturer/s is/are good or not. Welcome to the real world. 8-)

As I have mentioned in the previous post, this blog will primarily contain the archive of the salient points of the module as we go along. I will strive to update it once a week, the keyword being strive. 8-) If you have any questions, please feel free to leave a comment so that we can all learn from each other.

Ok, for the first week, as it is only the start of the semester, we introduced the concept of programming and what writing computer programs is all about. Remember the PacMan game that I mentioned, as an example of a simple computer program? Right, so:
  • a computer program is simply a set of instructions that you give to the computer for it to perform some actions for you,
  • programming languages are the languages used to 'talk' or 'tell' the instructions to the computer,
  • Java is just one of the many programming languages
So in the weeks to come, we will be learning the syntax, or the rules, of the Java programming language, as well as the logic, or algorithm, of computer programming so that we can write useful computer programs.

We also started with Lab 1 on writing simple logic or algorithms, in pseudo codes, to get our brains to think logically so that we can later formulate the logic into Java codes to program into the computer. The key thing to note in writing pseudo codes (whether using pen and paper or mentally in your brain) is to try to be as explicit and clear as possible. Remember that the computer cannot think, so it will not be able to do intelligent actions for you unless you instruct it to. Hence instructions, in the form of pseudo codes, need to be thorough and clear.

In the lab, we wrote (or at least you, I have written mine long ago 8-) our first Java program, HelloWorld.java, using TextPad and JDK. Do remember these steps on writing a Java program, as you will be doing this on your own from next week onwards.
  • Open TextPad and type in your Java codes.
  • Save the file as .java.
  • Open up a command prompt window and navigate to the folder where you saved the Java file.
  • Use the command: javac .java to compile the Java program.
  • Use the command: java to run the Java program to see if it works according to what you want it to work.
Do leave comments if you have questions on what was covered this week!

Sunday, April 1, 2007

Welcome!

This is the first post for the IT1838 mPowered blog!

The orientation is starting next week from 2 April to 5 April and semester will start on 16 April. This new semester we will do something different from other semesters, and I intend to use this blog to document down the salient points discussed in the lectures and the labs. This not only serves as an archive, for you to refer to when you study for your exam (yes, this module is examinable) but also as a communication platform for us to exchange ideas on the module.

And since this is the first post, I will not bore you with what the module is about. 8-)

Enjoy your stay in NYP!