Chapter 1

Object Oriented Programming

 

Required terminology and general information for this chapter:

 

Program – a series of instructions for a computer to execute.

Programming languages: Machine, Assembly, and High Level

How the first computers were programmed? By manually connecting wires.

Machine language: programs and data were entered into the computer using zeros and ones.  All operation codes (opcodes) and operands were entered in binary numbers.  Machine language is the machine “understandable” language.  It does not matter what other languages are used to program, eventually all programs and data will have to be translated into a “machine executable” format.

Assembly Language: An easier programming language than machine language, where  programs are written using familiar symbols (for example A for add).  The operands could be in binary, hexadecimal or decimal format.  For every machine language code, there needs to be one assembly language code.  The program was not shorter, just easier to write.  Once the program was written in assembly language, it had to be compiled to produce the machine executable code in order to run the program.  The compiler used is called an ASSEMBLER.

High Level Language: Included are languages such as Fortran (Formula Translator), COBOL (Common Business Oriented Language), BASIC (Beginner’s All purpose Symbolic Instruction Code), Pascal, C, Ada, and many more.  Programs are much shorter than lower level languages.  Each instruction is converted to several machine executable codes. This conversion is done in three steps: preprocessing, compiling and linking.  An original program written using an EDITOR in a high level language is known as the Source Code.  A compiled program is called the Object Code and the linked program is called the Executable Code. Examples of files created in each stage are: Carpet.CPP, Carpet.OBJ, and Carpet.EXE.

Interpreter: Not all languages are compiled as indicated in the previous paragraph.  Each line of a program can be interpreted at the time of execution.  Such programs run slower because each time a line of code is executed it has to be interpreted regardless how many times it was interpreted previously.

Syntax: Rules for constructing a legal sentence in a programming language. If a program has syntax errors, it will not compile. Therefore, it produces compile time errors. 

Semantics: Rules that determine the meaning of instructions written in a programming language.

Runtime and Logical errors: A runtime error occurs when the program cannot execute what is being asked to do.  For example, open a file when it does not exist.  Logical errors are most difficult to fix; it is caused by faulty logic by the programmer.  For example, suppose you wanted write a program to give discount if purchase is greater than $100.00, instead you programmed to give discount if purchase is less than $100.00.

C++:  The C (Dennis Ritchie) language is a modification of another language called BCPL (Ken Thompson).  C language was written for programming under the Unix operating system environment which continues to be a very popular operating system for universities and businesses. C++ (Bjarne Stroustrup) is an object oriented version of  C.

JAVA:  A programming language developed by Sun Microsystems, Inc. to run on all modern computers that is able to browse the word wide web.  Java runs on a virtual machine not on a physical machine.   A Java program can be downloaded as part of a web page and run on a client computer.  Such web page downloadable programs are called Java Applets.  Java can be used to create stand alone programs also.  Java is 100% object oriented language.

WWW: World Wide Web is based on the principle of Client/Server computing.  Programs and/or data are kept on the server and many clients can have access to it.  In WWW, the client occupies the web  server only for the duration of sending a requested document.  The server is then free to handle other requests.  The clients use a web browser (Explorer, Netscape, etc.) to access the web pages on a server.  The web servers make up a subset of the Internet that use a special protocol called HyperText Transfer protocol (HTTP).

Internet: A network of many networks connected using the Information Super Highway (Back Bone).

HTML: Web pages on the web servers are coded using  the HyperText Markup Language.  Each line of text is tagged.  These tags are essentially instruction to the browsers (or other client software)  how to handle that line (example, Center, Bold, Font Size, etc.).

URL: The location of a web page is identified by the Uniform Resource Locator).  URL is an address.

JAVA DOWNLOAD: Java is provided free by Sun Micro.  Download the latest version from http://java.sun.com .  At the time of this writing Java 2 SDK version 3.x.x was the latest.  It would be good to download some integrated environment as well.  I like JCreator.  Jbuilder from Borland also is very good.  Follow directions provided by the manufacturers to install these.  Install Java first, then install the integrated environment of your choice.   You need to let the integrated environment find Java or tell it where Java is.

 

 

 

Object Oriented Programming

 

                We humans are very good recognizing and working with objects, such as a pen, a dog, or a human being.  We learned to categorize them in such a way that make sense to us.   We may categorize them as animate object, inanimate objects, pets, friends, etc.  We some times classify objects based on their attributes, for example, green apples or red apples, fat or slim people, etc.  If you think about it each object has many attributes.  If I ask you list the attributes of an orange, you probably could list many things such as color, shape, weight, smell, etc.

 

                In addition to attributes, all objects exhibit behaviors.  A dog eats, barks, wags its tail, plays, and begs.   A dog exhibits many more other behaviors than this short list.  It is a good idea to practice listing attributes and behaviors of many of the objects you come across each day.  Another thing we need to remember about objects is that objects interact between each other.

 

                Programmers, for over thirty years programmed using functions and procedures.  Each function and procedure was called to carry out certain tasks on the data that were given to it and to return (or not return) certain results back.  With this type of procedure oriented programming, we had to adapt our thinking procedurally.  Working with objects is a more normal and suitable approach for human beings.  Such programming approach is called Object Oriented Programming (OOP).

 

                In Object Oriented Programming, objects are packages that contain data and functions (methods) that can be performed on the data.  Data could be considered to be attributes and functions are considered to be behaviors of the object.  We can say that the attributes and behaviors are encapsulated into an object.  The objects interact between each other through their interfaces.  As an example a date object may have a set of data consisting of month, day and year, and methods consisting of assign date, display date, yesterday and tomorrow. 

 

Data abstraction is another important feature of Object oriented programming.  OOP hides the details of data structures from the users.  For example, to set a date in the aforementioned example, values of month, day and year are passed to the assign-date method.  The actual representation of the date is hidden from the user.

 

                There can be various objects made of a particular class (recall that many variables can be made from type).  In OOP the general type with which objects can be made is called a class.  An object is an instance of a class.  Each class contains data (data members) as well as set of functions  (member functions) that manipulate the data.  Classes have the following features:

1.        the capability to control access

2.        Constructors

3.        Destructors

4.        Data Members

5.        Member functions

6.        A hidden, special pointer called this

 

Your first Java Program.

 

                All my instructions and examples assume that you have a integrated environment including an editor installed in your computer to handle Java.  I recommend JCreator, netBeans, or JBuilder.  If you do not have one installed, you will have to write your program using the Note Pad, save the file using .java extension (example Myfile.java), invoke the compiler javac (example javac Myfile.java), and then run the program (example java Myfile.class).  The integrated environment does it all for you.

 

 Java is 100% Object Oriented programming language; a program must have at least one class defined-.  Even the simplest program requires a Class and class should contain at least one method.  A program must begin execution at a particular method.  In Java ‘main’ method is such a method.  In all Java programs main method or some other similar method (described later) will be used.  Java is case sensitive, type the program exactly as it appears here into your computer.

 

/*****************************

Chapter 1, Program 1 - JAVA

Objective:  classes, methods

*****************************/ 

 

 

class One_1

{

                public static void main (String arg[])

                {

                                System.out.println("Hello Mom! \nThis is my First Java Program!!\n");

                }

}

 

Type this program using any editor.  Save it to a file called One_1.java.  Compile it and the compiler will produce a file called One_1.class. The compiler, javac, takes your source file and translates the program into instructions that the Java Virtual Machine can understand. The compiler converts these instructions into a bytecode file. Java is executed in a virtual machine regardless of the physical makeup of that machine.  The Java interpreter installed on your computer implements the Java virtual machine. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.   Executing this class will give the following output:

 

 

Hello Mom!

This is my First Java Program!!

 

Press any key to continue...

 

 

 

                It is an accepted practice to begin the class identifier with a capital letter as shown here.  This class has one method, main.  The brackets signal beginning and ending of a block.  There are two blocks here, one for the class and one for the method.  Comments are enclosed in /*  */ pair.  Classes, methods and variables can be either private or public.  Those that are public are visible to the outside world and those that are private are visible only within where they are declared.  The word ‘static’ causes the contents of the memory locations to be available to (shared by) all objects of that class.

 

                A method does not do anything until it is called.  When a Java program is executed the main is automatically called by the operating system.  Therefore, the main has to be public.  Every method can have inputs into the method and an output out of the method.  The input here is the String arg [] and the output is void.  Void indicates that it does not return anything. 

 

                One of the outstanding features of object-oriented programming is the reusability of objects.  The System.out is an object referring to the standard output device of your computer (Monitor) and println is a method of that object.  The println method takes inputs.  In this example, "Hello Mom! \nThis is my First Java Program!!\n" is the input into the method.  The method receives this input and displays it on the video monitor.  The ‘\n’ tells it to put the remainder of the text in a separate line.  The parenthesis is required to put all the inputs, separated by commas.  These inputs are referred to as parameters or arguments.  In our example there is only one argument which is enclosed in the quotes.

 

                A lot of information was presented in this chapter.  I do not write anything that is not absolutely essential in any of the chapters.  Therefore, it is important that you read and understand everything in a chapter before proceeding to the next one.

 

You must do this:  Write several programs that display messages on the screen.  Use the program in this chapter as a guide.  Keep writing programs until you do not have to look at the notes anymore.  My blessings to you!!