Java: Introduction

Author: Ronald S. Holland
at
Total Application Works




To contact us





HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works


    Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <html>
    <head>
    <title>C#: Table of Contents </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/Java/JAV_Introduction.html">
    Java: Introduction </a>
    </table>
    </body>
    </html>
    Source code


Table of Contents

  • Read Me First
  • Introduction to Java
  • Programs
  • Programs used to solve Tasks
  • What is a Java Program?
  • What are program commands?
  • What is a Function?
  • So how do you get access to those prewritten/built-in functions?
  • First Java Program
  • Quiz questions
  • Definitions

    Read Me First

    The JAV_INTRO.zip file contains the following files:

    1. CompileRun_MyApp.bat - this file contains the code that when clicked will ompile and run the main program (MyApp.java)
    2. MyApp.java - this file contains the main method code and is the entry point when the program is loaded into memory,
    3. JAV_Introduction.html - is the tutorial that describes how to build the Switch program
      • MyApp.java.

    Introduction to Java

    This tutorial assumes that you have some prior programming experience. This tutorial is intended to be an introductory discussion on Java and programming. Java as a programming language is widespread and can be found on nearly all computers. This tutorial will prepare you to step through a set of tutorials on Java.

    What is Java?

    Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA),[ Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. As we ease into Java 3GL programming, you'll learn

    The following list outlines the tutorials we will use to introduce Java. In the rest of this tutorial will describe:
    • What makes up a Program
      • Source code
      • Source code compilation
    • How programs are used to solve Tasks
      • Create the Source code that builds calendars
      • Compile that Source code
      • Execute the compiled code to display the calendar
    • Exactly what is a Java Program to a computer
      • It is a set of executable instructions/commands
        • javac - is the primary Java compiler, included in the Java Development Kit (JDK).
            javac MyCar.java
        • java - launches programs on the Java Runtime Environment
            java MyCar
        • javadoc - generates documentation for your project.
            javadoc MyCalcApp.java
        • jar - compresses files.
            java -jar MyApp.jar
        • javah - produces C header files and C source files from a Java class.
            javah -jni MyCar
        • JConsole - uses the extensive instrumentation of the Java Virtual Machine (Java VM) to provide information about the performance and resource consumption of applications running on the Java platform.
        • javap - disassembles one or more class files.
            javap MyCar
        • etc.
    • What makes up a function
      • A function is a sequence of instructions within a larger computer program
    • How do you get access to those prewritten/built-in functions
      • Any function that is provided as part of a high-level language and can be executed by a simple reference with specification of arguments

    We will discuss what makes up a program in the next section.

    Programs

    A program is a set of instructions used to solve a task. More specifically, these instructions tell a computer what to do. These set of instructions are like a recipe used to make a cake; a set of computer instructions are used to make a program. To make/create a program, we do the following:

    1. Source code is created when we enter a sequence of commands into a text editor, like notepad.
    2. When we process the raw source/code with a compiler, it results in object code.
    3. Once a program is in an object code format, a link editor processes the object code to form an executable program. These steps are outlined in the following table.

       
      
          +---------------------+
          |   text editor       | Step 1: Open an Text Editor 
          +-----------+---------+
                      |
                      V
          +---------------------+
          |   source code       | Step 2: Enter raw source into Text Editor
          +-----------+---------+
                      |
                      V
          +-----------+---------+
          |      compiler       | Step 3: compiler processes file created 
          +-----------+---------+         by Text Editor that contains 
                      |                   source code
                      V
          +-----------+---------+
          |      Link Editor    | Step 4: Object code created by compiler
          +-----------+---------+         is processed by Link Editor
                      |
                      V
          +-----------+---------+
          | executable program  | Step 5: Result of link editor
          +---------------------+
       
      Figure 1: Steps taken to create an executable program


    Why do we write programs? With the aid of a computer, programs are written to solve a task. These tasks could be a game, a program to view contents on a drive e.g., Windows Explorer, or a text editor e.g., Notepad.

    Programs used to solve Tasks

    Application software consists of those programs written to perform particular tasks that are required by the users. For example, the following partial list shows programs that solve a specific task.

    • Browser - is a software application used to locate and display Web pages. Some examples of this type of software are Mozilla Firefox and Microsoft Internet Explorer.
    • Database Management System Software (DBMS) - A DBMS is the structured collection and management of data. It is a set of programs that allows you to store, modify, and access information from a database. DBMS software allows the users to store and retrieve data from databases. Examples are Oracle, MSAccess, etc.
    • Educational Software - The primary purpose of this type of software is teaching or self-learning. It has the capabilities of running tests and tracking student progress. It also has the capabilities of collaborative software. Some examples of areas touched by this type of software are:
      • Anatomy
      • Children's software
      • Dictionaries and reference
      • Environmental Education
      • Geography and Astronomy
      • Historical
      • Literacy
      • Managed learning environments
      • Mathematics
      • Music
      • Programming
      • Science
      • Simulation Games
      • Touch-Typing Instruction
      • Visual Learning and Mind Mapping
    • Enterprise Software - It is the software needed to run an organization's processes and data flow. If the organization is a retail enterprise, then it is the software needed to process and manage the enterprise's business. The customer relationship management or the financial processes in an organization are carried out by means of enterprise software. It might look like:
      • ERP (Enterprise Resource Planning) - this is a view of a company and all its parts as a synergistic connected whole, rather than small slices of independent activity.
      • CRM (Customer Relationship Management) - is a company-wide business strategy designed to reduce costs and increase profitability by solidifying customer loyalty, loyalty, and advocacy. It is a process or methodology used to learn more about customers' needs and behaviors in order to develop stronger relationships with them.
      • POS - Point of sale (POS) or checkout is the location where a sale/transaction occurs. A "checkout" refers to a POS terminal or more specifically to the hardware and/or software used for checkouts, the equivalent of an electronic cash register.
      • Business Intelligence
      • General Ledger
      • Accounts Receivable
      • Accounts Payable
      • Inventory Control
      • Manufacturing
      • Payroll
      • Human Resources
      • ecommerce connectivity
    • Information Worker Software - Individual projects within a department and individual needs of creation and management of information are handled by information worker software. Documentation tools, resource management tools and personal management systems fall under the category of this form of application software.
    • Multimedia Software - This software allows the user to create and play audio and video media. They are capable of playing media files. Real Player and Media Player are examples of this type of software include.
    • Operating Systems - The operating system is the most important program that runs on a computer. It manages the conduits that allow other programs to interface with computers peripherals. For example:
      1. Writing to disk;
      2. Displaying data on a display/monitor screen;
      3. Sending data to a printer;
      4. Reading/writing to a DVD or CD;
      Every general-purpose computer must have an operating system to run other programs. Some operating systems are:
      1. Microsoft Windows 98, ME, XP, Vista, Windows 7;
      2. Unix;
      3. Linux;
      4. IBM: OS/360, DOS/360, DOS/VS, MVS, VM/370, and TSS/370;
      All of these operating systems perform basic tasks, such as recognizing input from the keyboard, sending output to the display screen, keeping track of files and directories on the disk.
    • Presentation Software - The software that is used to display information in the form of a slide show is known as presentation software. This type of software includes three functions, namely, editing that allows insertion and formatting of text, methods to include graphics in the text and a functionality of executing the slide shows. Microsoft PowerPoint is the best example of presentation software.
    • Simulation Software - Used to simulate physical or abstract systems, simulation software finds applications in both, research and entertainment. Flight simulators and scientific simulators find a place in the list of simulation software.
    • Spreadsheet Software - Some examples of spreadsheet software are Excel, Lotus 1-2-3, and Apple Numbers. Spreadsheet software displays rows and columns of data that allow users to perform calculations on this displayed data.
    • Text Editor - is a type of program used for editing plain text files.
    • Word Processor - A word processor is a computer application used in the creation/composition, editing, formatting, and/or printing of documents. A word processor enables you to create a document, store it electronically on a disk, display it on a screen, edit it by entering commands (i.e., fonts, bold, italics, etc.) and characters from the keyboard, and print it on a printer.

    What is a Java Program?

    What is a navigation program? A navigation Program is an easy-to-use software application that provides step-by-step directions showing the user how to get from point A to point B. If you consider that the human brain is similar to a computer, then these instructions are commands to the user providing instructions on what steps to perform next. A Java program is a collection of commands, that tell the computer what steps to perform next. This collection of commands, written in a programming language, is usually called Java source code, source code or just code. As previously stated, human brains are similar to computers. As humans, we run programs all the time. Our daily routines could be considered human programs. For example, look at the following routine/program.

     
    Wake up;
    Go to the bathroom;
    Wash up;
    Exercise; 
    Eat breakfast; 
    Shower; 
    Get dressed; 
    Go to work; 
    Do work/job; 
    Return home;
    Watch TV;
    Log on to the Internet;
    Read/send email;
    Go to bed;
     
    Figure 2: A human daily routine/program


    Many of us have a similar routine/program that we follow every day. These routines consist of a set of commands. We will explore some of the Java commands in the next section.

    What are program commands?

    Commands are either "functions" or "keywords". Keywords are predefined reserved identifiers that have specified meanings/functions. Since they are predefined, they cannot be used as identifiers (variable names or function/method names) in your program. The set of rules of a language, Java in this case, that dictate how the various parts (keywords and/or commands) of sentences go together to provide instructions for a computer is called syntax. For example, the "int a;" statement tells the compiler that variable "a" is of data type int. Some of the Java commands/keywords are listed in the following table.

    abstract continue for new switch
    assert*** default goto* package synchronized
    boolean do if private this
    break double implements protected throw
    byte else import public throws
    case enum**** instanceof return transient
    catch extends int short try
    char final interface static void
    class finally long strictfp** volatile
    const* float native super while
    Figure 3: Java keywords


    If keywords are the bricks, then the syntax is the mortar that forms the building blocks of the language. Then, what are functions? Below, are some of the functions listed alphabetically.

    Name Description
    static double abs(double a) This method returns the absolute value of a double value.
    static float abs(float a) This method returns the absolute value of a float value.
    static int abs(int a) This method returns the absolute value of an int value.
    static long abs(long a) This method returns the absolute value of a long value.
    static double acos(double a) This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
    static double asin(double a) This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
    static double atan(double a) This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
    static double atan2(double y, double x) This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
    static double cbrt(double a) This method returns the cube root of a double value.
    static double ceil(double a) This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
    static double copySign(double magnitude, double sign) This method returns the first floating-point argument with the sign of the second floating-point argument.
    static float copySign(float magnitude, float sign) This method returns the first floating-point argument with the sign of the second floating-point argument.
    static double cos(double a) This method returns the trigonometric cosine of an angle.
    static double cosh(double x) This method returns the hyperbolic cosine of a double value.
    static double exp(double a) This method returns Euler's number e raised to the power of a double value.
    >static double expm1(double x) This method returns ex -1.
    >static double floor(double a) This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
    static int getExponent(double d) This method returns the unbiased exponent used in the representation of a double.
    static int getExponent(float f) This method returns the unbiased exponent used in the representation of a float.
    static double hypot(double x, double y) This method returns sqrt(x2 +y2) without intermediate overflow or underflow.
    static double IEEEremainder(double f1, double f2) This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
    static double log(double a) This method returns the natural logarithm (base e) of a double value.
    static double log10(double a) This method returns the base 10 logarithm of a double value.
    static double log1p(double x) This method returns the natural logarithm of the sum of the argument and 1.
    static double max(double a, double b) This method returns the greater of two double values.
    static float max(float a, float b) This method returns the greater of two float values.
    static int max(int a, int b) This method returns the greater of two int values.
    static long max(long a, long b) This method returns the greater of two long values.
    static double min(double a, double b) This method returns the smaller of two double values.
    static float min(float a, float b) This method returns the smaller of two float values.
    static int min(int a, int b) This method returns the smaller of two int values.
    static long min(long a, long b)

    This method returns the smaller of two long values.
    static double nextAfter(double start, double direction)

    This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
    static float nextAfter(float start, double direction) This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
    static double nextUp(double d) This method returns the floating-point value adjacent to d in the direction of positive infinity.
    static float nextUp(float f) This method returns the floating-point value adjacent to f in the direction of positive infinity.
    static double pow(double a, double b) This method returns the value of the first argument raised to the power of the second argument.
    static double random() This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
    static double rint(double a) This method returns the double value that is closest in value to the argument and is equal to a mathematical integer.
    static long round(double a) This method returns the closest long to the argument.
    static int round(float a) This method returns the closest int to the argument.
    static double scalb(double d, int scaleFactor) This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
    static float scalb(float f, int scaleFactor) This method return f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
    static double signum(double d) This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
    static float signum(float f) This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
    static double sin(double a) This method returns the hyperbolic sine of a double value.
    static double sinh(double x) This method Returns the hyperbolic sine of a double value.
    static double sqrt(double a) This method returns the correctly rounded positive square root of a double value.
    static double tan(double a) This method returns the trigonometric tangent of an angle.r
    static double tanh(double x) This method returns the hyperbolic tangent of a double value.

    static double toDegrees(double angrad) This method converts an angle measured in radians to an approximately equivalent angle measured in degrees.
    static double toRadians(double angdeg) This method converts an angle measured in degrees to an approximately equivalent angle measured in radians.
    static double ulp(double d) This method returns the size of an ulp of the argument.
    static double ulp(float f) This method returns the size of an ulp of the argument.
    Figure 4: Java Math built-in functions


    Considering the functions in Figure 4, what is their purpose?

    What is a Function?

    A function is a self-contained block of code that has a name and it has a property that it is reusable/reentrant i.e. it can be executed from as many different points in a Java program or many Java programs as required. A function allows a number of program statements to be grouped into a unit with an addressable name. This unit can be called/invoked from other parts of a program. A function is usually created to perform a task that will be needed many times in a program or needed by many program e.g., SQRT(). An outline of a function is depicted in the following figure.

     
    
    function_name( data_type parameter ) {
       needed_variables ;
       int a ;
       int b ;
    
       code line 1 ;
       code line 2 ;
       a = 9 ;  
       b = sqrt( a ) ;  
    
         ...
    
       code line n ;
    }
    
    /** **************************************
     * The main() function is the entry point 
     * that is called Java runtime.
     *****************************************/
    public static void main( String args[] ) {
    
      lines_of_code
    }
     
    Figure 5: Outline of a Function


    The name of the function is unique in a Java program and is global within the Java language supplied library. This means that a function can be accessed from any location within a Java program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. It should be noted that Java provides a great many common functions and keywords that you can use (see Figure 3 and/or see Figure 4).

    You may be wondering how does your program actually start? Every Java program has one entry point/function, which is called main. This function is the entry point that is called when your program first loaded into memory. From the main function, you can also call other functions whether they you wrote them or, as previously mentioned, they are provided by the compiler.

    So how do you get access to those prewritten/built-in functions?

    To access those standard functions that come with the compiler, you include a header with the #import directive. What this does is effectively take everything in the header and paste it into your program. Refer to Figure 5 to see an outline/template of a program written in pseudocode.

      
    #import Java.Lang.Math;
    
    import javax.swing.JOptionPane ;
    import java.io.*;
    
    class MyApp {
    
       String name ;     // name of user
     
       /** MyApp1 constructor */
       public MyApp() {
          String mess = " ", title = " " ,  ret = " "  ;
    
          /** read in the name of the user as a string */
          name =
             JOptionPane.showInputDialog( "Please enter your name." );
    
          System.out.println(  "Hello " + name ) ;
    
          System.out.println(  "This is a message from\n" );
          System.out.println(   "Ronald S. Holland\n" );
          System.out.println(   "         @\n"  );
          System.out.println(   "Sumtotalz.com\n\n\n"   );
          System.out.println(   " Hello...\n"  );
    
    
          ret = JOptionPane.showInputDialog( null ,
                       mess ,
                       title ,
                       JOptionPane.PLAIN_MESSAGE ) ;
       } 
    
    
    /*****************************************************
     * The main() function is the entry point that is called
     * when the program is loaded into memory.
     ****************************************************/
       public static void main( String args[] ) {
    
          MyApp app = new MyApp() ; /** creates an instance of MyApp  */
    
          System.exit( 0 );   /** end the program */
       }
    }
     
    Figure 6: Outline of a Function


    Let's look at the statements in the program in Figure 6.
    1. String mess = " ", title = " " , ret = " "
        This statement declares three variables of data type String.
    2. JOptionPane.showInputDialog( "Please enter your name." );
        This statement Prompts the user for some input..
    3. System.out.println( "" ) ;
        This statement allows the program to print text to the screen.
    4.  ret = JOptionPane.showInputDialog( null ,
                         mess ,
                         title ,
                         JOptionPane.PLAIN_MESSAGE ) ;  
        This statement holds the CMD screen to allow the user to view the screen.
    5. Upon reaching the end of main, the closing brace, our program will return the value of 0 (and integer, hence why we told main to return an int) to the operating system. This return value is important as it can be used to tell the OS whether our program succeeded or not. A return value of 0 means success and is returned automatically (but only for main, other functions require you to manually return a value), but if we wanted to return something else, such as 1, we would have to do it with a return statement:
       
      
      

    Definitions

    3GL

      First introduced in the late 1950s, Fortran, ALGOL, and COBOL are early examples of this sort of language. Most popular general-purpose languages today, such as C, C++, C#, Java, BASIC and Pascal, are also third-generation languages, although each of these languages can be further subdivided into other categories based on other contemporary traits.

    class-based

      or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance is achieved by defining classes of objects, as opposed to the objects themselves (compare prototype-based programming).

    Compiler

    concurrent

      is a form of computing in which several computations are executing during overlapping time periods.

    Holland's OOP Programming Design Model

        
                +--------------+
                |      Data    |<----+  
                +-------+------+     | 
                        ^            |
                        |----------->+
                        V            ^
                +-------+------+     |
                |   Top-down   |     | 
                |  Structured  |<----+
                |  programming |     |
                |  on Classes  |     | (step-wise)
                |  and methods |     | (refinement)
                +--------------+     | (as needed or as the) 
                        |            | (application ages)
                        +------------+ 
       
        

    interface

      An interface in Java is similar to a class, but the body of an interface can include only abstract methods and final fields (constants). A class implements an interface by providing code for each method declared by the interface.

      However, implementing multiple interfaces is allowed in Java and it is also safe. A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

    program

      is a collection of instructions[1] that performs a specific task when executed by a computer.

    program language

      is a list of instructions written in a programming language that is used to control the behavior of a machine, often a computer (in this case it is known as a computer program).

    Object-oriented programming

      A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms.(OOP) is a programming paradigm that uses "objects" data structures consisting of data fields and methods together with their interactions to design applications and computer programs.

    source code -

      are a list of programming statements entered into a "text file" that can be compiled and executed on a computer.

    Structured programming

    static

    stdin

    stdout












HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works