Featured Java Lecture Introduction

Discussion in 'Programming & Web Design' started by Marvic, Sep 6, 2018.

  1. Marvic

    Marvic Member

    Joined:
    Sep 6, 2018
    Messages:
    12
    Likes Received:
    1
    Java is an object-oriented programming language, designed by Dr. James Gosling of Sun Microsystems. By using Java, we can make Web pages interactive and alive. We can display command buttons, text fields, check boxes, option buttons, scrolling list, choice controls, and other controls that process the web surfer’s input and output data.

    In this book, we will focus more on the Graphical-User Interface (GUI) programming, meaning, we will deal with graphics or objects in our program. We will learn also the programming based on working examples with explanations on how they work. Java programs come in two ways. The first one is a stand-alone application and the second one is an applet - a small program we can embed in Web pages. Since the applet is the most popular and widely used, we will concentrate on its implementation.

    To explain it further, an applet is a special program that we can embed in a Web page such that it gains control over a certain part of the Web page. On that part of the page, the applet can display command buttons, text boxes, scrolling list, choice control, images, and more. With this applet, the Web page becomes “alive”. Each applet in the Web page is given the amount of space, usually measured in pixels. This is the space that the applet will use for its display in the Web page.

    Java Classes, Objects and Methods Explained

    The classes and objects are two fundamental concepts in Object-Oriented Programming (OOP) language like Java. A class is defined as a blueprint or schematic diagram of an object. It is a programming construct that groups together the function and data. This process is called encapsulation. The objects are encapsulated, meaning, they contain both their code and their data, making them more easier to maintain.

    The Java objects have properties, methods and events. We can manipulate an object through their properties, events, and methods. Each object recognizes and can respond to certain actions called events. An event is a specific and predetermined activity, activated by either a user or the system. Events, in most cases, are generated by user-interaction. In Java, user actions that trigger events include mouse clicks, mouse moves, and key-presses.

    Methods are functions that are members of the class. They are also procedures associated with an object. Events can have methods associated with them. For example, if we write a method code for the Click event, that code is executed when the Click event occurs. All of the properties, events, and methods for an object are specified in the class definition.

    Properties can be considered as an object’s attributes, methods as its actions, and events as its responses. For example, a command button’s attributes are its caption, color, and size. An object such as a command button can perform methods and respond to events. This event can be a click by the mouse or a key-press from the keyboard. The performed method could be to show a window object or hide it instead. In summary, properties are data that describe an object; methods are things we can tell the object what to do; and events are things the object does.

    The most important to remember about Object-Oriented Programming (OOP) is that the object itself is what holds the data we want to work with, while the class itself holds no data, but just describes how the object should be setup.

    Let Us Talk Overloading, Inheritance and Overriding

    In function overloading, you can assign a name to your two or more methods with the same function name, provided it has different data types to receive in passing parameters or different number of parameters listed.

    Inheritance is one of the most important concepts of Object-Oriented Programming (OOP). In inheritance, a derived class inherits the functionality, features, and capabilities of its base class, plus you can extend its unique or advance capabilities.

    This further means that our derived class

    can use the methods declared at the base class, then it can also construct its own method which is unique or advance from the base class. Remember that if the methods found in our base class have the same name with our methods in the derived class, this is still okay even if they are exactly the same; meaning the same data type, the same number of parameters. This technique is called overriding, which is one of the most important techniques in inheritance. The difference between overloading and overriding is that in overriding we connect the function or method name to the new derived class.

    This connection or relation of the two is used by the compiler on which to call. Meaning, the compiler prioritizes to search first all the methods within the derived class before it goes to the base class’s methods. Therefore, even if they have similar names and similar parameters, there is no conflict, because of this prioritization of method to search and execute.

    This means also that the methods declared at the derived class overrides the methods declared at the base class. This is in the case where they have both the same method names. To discuss shortly the difference between an overloading from overriding is that in overloading - a function can be called with different parameter lists, while overriding means a function redefines the version of the function that appears in the class’s base class.

    The new Operator, Constructor and Destructor
    The Java programming language new operator works like the C++ programming language new operator. Its purpose is to allocate memory space for objects, arrays, or variables. A constructor is a special method that is automatically run when we create an object of a particular class. It is used to initialize objects. Since a constructor is a method, we can pass data to a constructor. A constructor has the same name with its respective class. The destructor is just an opposite to the constructor. It destroys an object’s initialization after it is being used.

    How to Run Java Program
    You need a word processor such as Microsoft NotePad to type your Java program and save as a file for you to compile and run it. Next, you’ll need to install the Java Development Kit (JDK) 1.4 (or later version) in your computer. You can get a free copy of this JDK from this website: http://developer.javasoft.com. Just download it and follow the instructions for proper installation.

    Making a Batch File in MS-DOS
    We need to create a batch file in the MS-DOS (Disk Operating System) mode to automate the compilation of our Java programs. We can do this by typing the word command right at the Run menu item of the Start menu of the Windows operating system to create this batch file. Here are the complete steps:
    1. First, type the word command at the Run menu item of the Start menu.
    2. Then, at the C:\> prompt, create a subdirectory named javaprog by typing the following syntax:
    C:\> md javaprog (then press the Enter key)
    The md here means make directory.
    3. Now type the command edit. This command will open the text-mode editor of MS-DOS. Next, type the following batch file code below:

    rem This is a java path configuration.
    rem Activate this batch file before you create java programs.
    @echo Java path is now activated!
    @echo off
    cls
    path=c:\winnt;c:\winnt32;c:\jdk1.5\bin;c:\jdk1.5\lib
    @echo off

    4. Then save it with the filename called javap.bat. The filename extension .bat means it is a batch file. The rem is a batch file command which means “ a code remark”, while the command echo means to display the message on the screen. The cls stands for clear screen. And finally the path command in our batch file is to simply instruct the computer to look for the Java compiler and its associated libraries (which are needed in the compilation of our programs) in the listed directories.

    To check if the javap.bat batch file is working properly, you can issue the MS-DOS command such as the following syntax:
    C:\JAVAPROG>path (then press the Enter key)
    You need to see that the directory path looks like the following line:
    C:\JAVAPROG>path
    PATH=C:\WINNT;C:\WINNT32;C:\JDK1.5\BIN;C:\JDK1.5\LIB
    In case the javap.bat batch file will not work as it is intended, you have to manually type the path command at the MS-DOS prompt like the following:
    C:\JAVAPROG>path=c:\winnt;c:\winnt32;c:\jdk1.5\bin;c:\jdk1.5\lib
    (then press the Enter key)
    Then you can check again if the manual way works; by issuing the path command again with the following syntax:
    C:\JAVAPROG>path

    The Three Files Are Needed
    To successfully run our Java program, we need the three files. The first one is the source code which filename extension is .java then the second one is the compiled code which filename extension is .class. And the last one is the HTML (Hyper-Text Markup Language) file which filename extension is .htm (where our applet runs). For example, we want to write a program that will display a simple message on the applet. The message is “Hello Bianca!”. First we have to open the Microsoft NotePad and type the following:

    import java.awt.Graphics;
    public class hello1 extends java.applet.Applet
    {
    public void paint(Graphics g)
    {
    g.drawString("Hello Bianca!",60,30);
    }
    }

    Then save it with a filename: hello1.java. After saving it, we are now ready to compile. What we have to do is to execute the batch file we created to automate the compilation of our Java programs. Here is the syntax:
    C:\>cd javaprog (then press the Enter key)

    The cd here means change directory.

    At the javaprog directory, we have to execute the javap.bat batch file using the following syntax:
    C:\JAVAPROG>javap (then press the Enter key)
    You will notice the high-speed execution of our batch file in just a matter of a few seconds.

    This time we are ready to compile our Java program using the following syntax:
    C:\JAVAPROG>javac hello1.java (then press the Enter key)

    The javac is the filename of our Java compiler, while the hello1.java is our Java program source code. If there are no errors encountered during the compilation process, we can now proceed to create the HTML file for our Java program. So we have to type the following at the Microsoft NotePad:

    <html>
    <!- Web page written with Java Applet>
    <head>
    <title>hello1</title>
    </head>
    <body>
    <hr>
    <applet
    code=hello1.class
    width=200
    height=200>
    </applet>
    <hr>
    </body>
    </html>

    Then, we will save the HTML file with this filename: hello1.htm to simply correspond to the name of our Java program which is hello1.java and to the compiled version of it which is the hello1.class file. The hello1.class file can only be generated if our Java program which in this case is the hello1.java is successfully compiled, that is, if there is no error encountered during compilation.

    Here in our HTML script, we specify that the name of the compiled code of our Java program is hello1.class as we can see on the script above and its corresponding width and height is 200. This is the size of our applet.

    Now to finally see it in action, we have to type the following syntax at the web browser:

    C:\javaprog\hello1.htm
    And presto, the running applet is now displayed!

    Dissecting the HTML Script
    This time, let us dissect the HTML script above. Remember, that every time we program in Java, we need to come up with similar HTML script above to be able to connect our Java program to the HTML script in order to produce the applet we needed.

    Let us start with the <html> tag:
    <html> tag
    <!- Written for IE Web browser comment
    <head>
    <title>hello1</title>
    </head>

    Instructions in HTML pages are placed into tags surrounded by angle brackets: <and>. The tags hold directions to the Internet Web browser and are not displayed on the screen. Here, the <html> tag indicates to the Internet Web browse that this .html file is written in HTML.

    We write here a comment using the ! (exclamation) symbol. Next comes the header portion of the Web page, which we declare with the <head> tag, ending the header section with corresponding end header tag, </head>. In this case, the .html file gets the title set up tag hello1, to match our applet. The title is the name given to a Web page, and it is usually displayed in the Internet Web browser’s title bar.

    <html>
    <!- Web page written with Java Applet>
    <head>
    <title>hello1</title>
    </head>
    <body>
    <hr> ruler line
    <applet
    code=hello1.class
    width=200
    height=200>
    </applet>
    <hr>
    </body>
    </html>

    Finally we come to the body of the Web page. Here is where all the actual items for display will go. We start the page off with a ruler line using the <hr> tag.

    Now we come to the applet portion. Applets are embedded with the <applet> tag, and here we use the code keyword to indicate that this applet is supported by the hello1.class file. We indicate also the applet size as 200 x 200 pixels (200 wide and 200 high in pixel). We indicate to the Internet Web browser here how much space we will need (or to occupy in a web page) for our applet using the width and height keywords.
    By using the <applet> tag, we can embed applets in the Internet Web pages.

    The Arithmetic, Relational, and Logical Operators
    We usually use the arithmetic operators in our equation or formula for calculation purposes, while the relational operators are used in evaluating a particular conditional expression or expressions in a program whether it is true or false. Based on the result of the evaluation, the computer will decide what to do or to execute. Logical operators are applied to evaluate two or more conditional expressions and determine if they are true or false, true or true, false and false or true and true and other possibilities. In other words, the logical operators will evaluate a group of complex conditional expressions, and based its action on the final outcome of the evaluation.

    With this final outcome, the computer will decide how to perform the program code. Here are now its symbols and command syntax used.

    Arithmetic Operators
    + Addition
    _ Subtraction
    * Multiplication
    / Division
    ^ Exponentiation

    Relational Operators
    < Less than
    > Greater than
    <= Less than or Equal to
    >= Greater than or Equal to
    == Equal to
    != Not Equal to

    Logical Operators

    Operators Rules
    && (and) If both or all expressions are evaluated to True, then the result is True.
    | | (or) When either or any (or just one) of the expressions is True, the result is True.
    ! (not) If the expression is True, then the result is the opposite of the expression.

    The Simple If-Else Conditional Statement
    The simple if-else conditional statement allows the computer to choose one and only one of the given two alternatives. Its general syntax is:

    if condition {
    Statement1;
    <other Statement(s)>
    }
    else {
    Statement2;
    <other Statement(s)>
    }

    If the condition is evaluated to True, then Statement1 (and together with other statements (s) that follow ) is executed, otherwise, Statement2 (and together with other statement(s) that follow) is executed instead. In other words, this if-else conditional statement says, “if the condition is true, then execute its associated statements, otherwise skip this code”.

    [​IMG]
     
    #1 Marvic, Sep 6, 2018
    Last edited by a moderator: Sep 9, 2018
Loading...