Aqua Phoenix
     >>  Research >>  OOP  
 

Navigator
   
 
       
   

6. Basics of Programming

6.1 Summary

  • Data Types
  • Functions
  • Basic statements

6.2 Contents

6.2.1 Data Types

All data that are handled in a program are declared as some type. Numbers, for instance, can assume data types of integers (whole numbers), doubles (decimals), and some other types. Data types are necessary to define a piece of data, because certain operations are associated to certain data types. Integers can be added, subtracted, multiplied, etc. A series of characters, also called a String (e.g. "Hello"), can and should not be multiplied; after all, what is "Hello" * "World" anyway? Data types have several other properties; most prominently, each data type can assume a smallest and a largest value. When you multiply two very large numbers on a primitive calculator, you may get as an answer "E", for Error. That means the calculator cannot handle the operation on as large numbers as you have entered. After all, computing space is finite - extremely finite as we will find out.

An Integer can take values from -2,147,483,648 to +2,147,483,647; a double can take values from 4.94065645841246544e-324 to 1.79769313486231570e+308, so extremely large. These sizes actually relate to certain numbers of bits, but that is outside the scope of this class.

There are several more data types available, two of which we will discuss as well. Character is one of those; every letter, digit, or symbol on the keyboard and on-screen is part of a list of characters. It turns out that there are 256 defined characters in that list, that are sorted according to a standard. Because of this convenience, each character assumes a numbered id in the list of 256; the ids altogether range from 0 to 255. In effect, characters are handled just like numbers, only that we perceive them as characters instead. This schizophrenic behavior introduces another feature that is present in most programming languages, called Casting. Casting refers to the method of taking a data type and transforming it to another datatype, similar to a conversion. Since characters and integers are related by the previously mentioned relationship, one can be casted as the other. The captital letter 'A', for instance, converts into the integer 65. By the way, lower letters and capital letters have different integers associated to them.

The last data type we will be considering at this point is called Boolean. It assumes only two values: True and False. If we assigned a boolean value to something, then this something can either be true or false. E.g. Are you 21 years of age: True or False. Boolean values can be used for Boolean algebra. For example: "Are you 21 years of age?" AND "Did you ask for a cigarette?" would result in different boolean results, depending on how the questions are answered. We won't get into Boolean algebra in this class, but some simple Boolean algebra should be understood.

For an exercise, use the following Tool, which has, by the way, been developed in Java, to experiment with data types and casts:

Usage: Click on "Generate" below one of the four datatypes boxes in order to generate a random value for the specific data type. If you would like, you can type in a desired value in the box above, which is then generated for you instead of a random value. Each node can be moved around by dragging-and-dropping it around the window. Move the nodes near the gray base blocks, in order to invoke an operation on them. For 8 out of the 9 operations, you will need 2 nodes.

In order to cast a data type to another data type, use the 8 casts at the bottom of the screen. The can be moved around in the same way as regular data nodes. Once a cast snapped to a data node, you can remove it by clicking on the 4 arrows and then dragging-and-dropping. If you would like to remove a node from the screen altogether, you can move it over the garbage bin.

6.2.2 Functions

Functions in programming are similar to functions in Mathematics. For a very simple function, consider f(x) = y, that is for a given value of x, the function returns y. A function in Java, or any other programming language, works the same way: Give the function an x, called an argument, the function returns some value y, called a return value. Note: In object-oriented programming, functions are typically called methods.

Unlike the mathematical function that usually return numbers of some sort, the functions we are dealing with return any sorts of types, but always only one type and one value at a time. As for arguments passed to functions, they can take any number of arguments of all different types. The mathematical equivalent to this would be, for example, f(x, y) = z. Arguments x and y are passed to the function and one value z is returned. The following mathematical equivalent is not possible in Java, but some other programming languages: f(a, b) = (x, y).

For any type of function, we need to explicitely define the data types for both arguments and return types. We will be considering some examples now:

Function Argument(s) Return type Explanation
int yup() {
  return 5;
}
No arguments Integer This function returns 5 whenever called
int Bliver(int x) {
  return 10;
}
1 Integer argument Integer This function takes an argument, yet always returns the same integer (10), without using the argument to do anything. In Mathematics, this would be considered a constant function, f(x) = 10
int poorThing(int x) {
  return x * 10;
}
1 Integer argument Integer This function takes an argument, and returns 10 times the value.
int Rippenclipp(int x, char c) {
  return c + x;
}
1 Integer argument
1 Character argument
Integer This function takes two arguments of different types, and returns their sum. Remember that Characters are essentially Integers as well.
boolean IsCharacter1LessThanCharacter2(char c, char d) {
  if (c < d) {
    return true;
  } else {
    return false;
  }
}
2 Character arguments Boolean (i.e. true or false) This function takes two characters, compares them according to value (characters are integers!), then returns true, if the first character is actually less in value than the second character. This is the first function that actually does some type of evaluation with the arguments; we will be discussing simple statements like this one below. Notice though, how long the function name is. Whenever we call this function, we need to specify the complete name; it would therefore be better to have shorter function names that are still somewhat descriptive of what the function does.
Table 1.2

6.2.3 Basic Statements

Statements are essentially the heart of programming in general. Statements are used to make decisions, and repeat some operation for a certain number of times. When you examine your screen that is filled with colorful windows and moving graphics, millions and millions of these statements are executed to make all of this happen. We will discuss a few very basic statements here that can be found in every programming language, and in almost every function that does some type of computation.

IF-THEN-ELSE clause:

This clause evaluates an expression, and if the expression is true, it will do something, otherwise, if the expression is false, it will do something else. Let's look at an example:

if (x <10) {
  y = 0;
}
y = 1;
In this case, we are evaluating the expression "x < 10", meaning is x less than 10? Given x = 1, the expression is true, i.e. 1 < 10; however, if x = 15, then the expression is false, i.e. 15 not < 10. If the expression happens to be true, then we find ourselves in the middle of the curly braces { (open brace) and } (close brace), i.e. y = 0. If the expression is false, we skip the open and close braces, and jump to whatever comes after, i.e. y = 1. Notice though that if the expression is true, y is set to 0, but immediately afterwards, the value of y is set 1. This occurs, because the curly end brace ends the if clause altogether. Braces enclose expressions, and as soon as a given open brace is succeeded by a close brace, the expressions and stetements within the braces are done, and we continue evaluating the following statements.

Consider this:

if (x < 10) {
  y = 0;
} else {
  y = 1;
}
This expression is different from the one above. In this case, the value of y is set to 0, if the expression of (x < 10) is true. However, the value of y is not set to 1 immediately afterwards. It is only then set to 1, when the expression of (x < 10) is false, i.e. x >= 10.