Project Nibble

Get help on programming - C++, Java, Delphi, etc.
Post Reply
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Project Nibble

Post by rustypup »

so, it has been suggested, from a number of independent sources, that it would be nice to have a simple intro to programming thread, with progressive lessons, allowing those not exactly dedicated to number crunching to dabble in a little coding, just for the excitement it may or may not engender...

Now, there are already a large number of tutorials available on the internet , making this bit of a silly task to start off with... what there isn't too much of is tutorials that provide you with the opportunity to develop a fully functional application from start to finish, including, along the way, some network code and/or graphics development... for this reason I felt it would be fun to fulfill another request, that for a simple chat application which could be used without the need for a central irc-server type arrangement during a LAN session..

Even with this in mind, simply leaping off of a tall building does not mean that everyone will be able to fly prior to being stopped by the pavement... and that leads us to the next bit... getting started in programming. so... considering the fact that some will be further along the road than others, I would ask that you bear with me as we begin the trek at a pace allowing for all interested parties to get involved... should you experience any difficulties, feel free to ask.. ask .. ask... if it doesn't make sense, chances are my explanation is insufficient to the task... :)

I have no intention of re-creating a complete programming tutorial here, so you will be required to hunt down information for yourself, (as and when required... ) . I will try to keep the lessons down to sensible chunks, (one reason for the title, the other will become evident later on)...

For the first few lessons, you will not be required to produce any code, just to spend some time studying the ground rules and becoming comfortable with the basic building blocks... in the interim I would advise you to get a head start by visiting:
1) The Java Tutorial -> we will be coding in java, so adding this to your favourites would be a good start.
2) Sun Microsystems - JDK -> The latest Java Software Development Kit. Includes the latest Java Virtual Machine and development tools.
3) Java 5.0 API -> Java Application Programming Interface Documentation. Your new best friend and support centre. (This page includes download links for Sun's own IDE, Netbeans).
4) The Eclipse Project -> not strictly necessary, but one the most popular Integrated Development Environments, (IDEs), out there... you will use this for writing/compiling your code.

I foresee the logical steps taken in lessons being:
1) Binary notation and primitive types
2) complex types, operators and keywords
3) octal, decimal and hexadecimal numbering
4) basic OO principals
5) classes, interfaces and packages
6) simple data manipulation, instantiation and coding practices
7) the project proper

The first six will be excessively abbreviated... so spend some time crawling through any tutorial you come across in order to flesh out the information provided... ;)
Last edited by rustypup on 15 Feb 2006, 07:31, edited 1 time in total.
Most people would sooner die than think; in fact, they do so - Bertrand Russel
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Binary notation and Data types

Post by rustypup »

A computer uses physical switches to manipulate data. In order to represent the switch's states in a logical manner, we use a sequence of 1's and 0's. 1 is used to represent the ON position, and 0 the OFF position. A 1 used in a test instance is implicitly understood to represent a TRUE case, while 0 would be FALSE. Depending on the primitive data type being processed the position and state of the switch determines the actual value represented. Binary notation is read right to left, as follows:
Image
So, taking an arbitrary number at random, we would write 73 as:
Image
The logic being (64+8+1) == 73. Several interesting points about this system. A single unit, (1 or 0), is called a BIT. 4 bits == a NIBBLE or NYBBLE. 2 nibbles or 8 bits == BYTE. (We will discuss data types later). The left most bit, (shaded above), is used to determine whether a value is negative or positive, (more commonly, this bit is called the Most Significant Bit, or MSB. In contrast, therefore, the bit on the far right would be called the Least Significant Bit, or LSB. If the MSB is set, the value is said to be SIGNED and NEGATIVE. If not, the determination of signed or not depends on the value, primitive type or compiler itself).
Image
Taking the above into account, we can see that the max value for the primitive type BYTE is 255, (if you said 256 you need to look again… zero is a number :).. While it is true to say the absolute number of attributable values is 256, this is 0..255. In the same manner, if this byte were SIGNED, (the MSB was set), then the value range would be between -128 and 127, (again... zero is a number). As far as the Java language is concerned, ALL BYTES ARE IMPLICITLY SIGNED... whether the value is positive or negative, a byte's maximum value is always going to be 127, and minimum -128..

While nibble and byte remain constant in length, the size of an int, for instance, would vary depending on which platform you are executing the application on or which compiler you are using. This ranges from 2 bytes, (16bits), up to 4 bytes, (32 bits). For all intents and purposes, in Java, this has little significance for you as all primitive data types are fixed length types, foregoing the need for dynamic memory requirement calculations and the eponymous Mr sizeof() so beloved of the c/c++ programmer...
Image
The above is a diagram showing the extent of each of the primitive data types available to you. Note the special types (float and double). These two types allow you to work with floating point numbers, (fractional numbers), as opposed to their whole number counterparts, int and long respectively.
Image
The table above should be printed out and stuck somewhere nearby in order to be used as a quick reference later, until such time as you become comfortable with the use of each. For some detail on each of these, check out one of the million or so tutorials available on this subject, or simply click here for one...

finally, it is important to note the issue of working with floating point numbers within the confines of a binary system. By their very nature, fractional values have no natural point of termination. The value of pie, for instance, never really ends... once you move past the decimal point it continues to infinity. Unfortunately for us, no computer can handle infinite values. As a result, we need to be mindful of the fact that floating point numbers:
i) Should NEVER be used in equality tests.. should two variables be initialised by the statement (7.23 / 4.89), there is no guarantee that their values will match EXACTLY... the actual value is determined by the algorithm being used to control rounding. (this is particularly true of double precision math...)
ii) You should be wary of having variables initialised to NaN, (Not a Number), or infinity. Either could occur and should be checked for prior to using the value stored within a variable. (Most exceptions here occur when working with tiny fractional values where the chances of NaN increase dramatically).

Up front we need to recognise the difference between assigning a value to a variable and checking for equality:
i) = This is the assign symbol. The value that occurs after this symbol will be assigned to the variable
ii) == this is the comparison symbol. the value on the left will be compared to that on the right.

When working with variables, we must first specify type. We must then provide the variable a name, then an initial value and finally we must indicate that the instruction is ended. As follows;

Code: Select all

int someInt = 1124768;
note the ";" at the end - this indicates the end of the instruction. every single instruction requires that this symbol be present, or you will experience trouble compiling your application. Note also that the requirement for a valid integer is built in. You cannot declare a variable of type "int" and then provide a value of type "String".

Code: Select all

byte someByte = (byte)32;
note the “(byte)”, referred to as typecasting. This is necessary as the compiler automatically assumes *all* whole number literals are of type "int" unless it includes a typecast literal, ("0x", "L", etc), or is typecast using the above method. Typecasting allows you to enforce type storage. It is important to note that the compiler will perform dynamic typecasting within the declared variables bounds. This means that, as long as the declared type has sufficient space to store the value provided, and the value is a numeric type, the compiler will accept it.
In the given case, omitting the typecasting would result in a compiler warning, "Possible loss of precision". Basically, there is a risk you will not store all of the value provided, (anything greater than 127 or smaller than -128...).

Code: Select all

char aChar = someByte;
note from the information provided that byte will fit comfortably into char. also that I can assign a value using another variable.

Code: Select all

float myFloatingPoint = 768.327f;
note the literal typecast "f" at the end? this tells the compiler that we are working with a float and not a double, (the default...).

Code: Select all

double aDoublePrecisionNumber = 98632569.3265545765;
the compiler will automatically assume *any* decimal point is a double value - ie.. auto-boxing will always be toward the largest, (safest), possible storage type...

Finally, it would be quite legal to assign a char value to an int type, as char is essentially a special representation of a number... only, the char type is implicitly UNSIGNED.

ok.. enough for now... spend some time hunting down whatever info you can locate on the primitive data types... don't worry too much about operators just yet as we have more ground to cover before we get there...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Post Reply