Background          Installation          First Program

THU JUN 14 2018 [FIRST PERIOD]

LECTURE 1

class apples{
        public static void main(String args[]){
            System.out.println("Your Name");
    }
}

1.1   BACKGROUND

Computers are electronic devices that process data into information.  They guide the computer in performing tasks.  For the various hardware and software components of the computer to be coordinated and work to achieve the set goal desired, it must be programmed.  Programming is creating instructions for the computer to follow it has to perform the tasks mentioned.

A programmer thus communicates with the computer in its machine language that it understands in order to tell it what to do.  Writing a program in the computer’s native language, which differs among different types of computers, is both tedious and cumbersome and are quite difficult to read and modify.  Thus assembly languages were developed to make programming easy.  However, since the computer cannot understand assembly language, a program called an assembler is used to convert assembly-language programs into machine the code.

Assembly programs are written in terms of machine instructions with easy-to-remember mnemonic names.  Since assembly language is machine dependent, an assembly program can be executed only on a particular kind of machine.  The high-level languages were developed in order to transcend platform specificity and make programming easier.  The high-level languages are English-like and easy to learn and program. Here, for example, is a high-level language statement that computes the area of a circle with radius 5:
  area = 5 * 5 * 3.1415;

A program written in a high-level language is called a source program or source code.  Since a computer cannot understand a source program, a program called a compiler is used to translate it into a machine-language program.  The machine-language program is then linked with other supporting library code to form an executable file, which can be run on the machine.  On Windows, executable files have extension .exe.  There are different kinds of programming languages we can identify.

Java is one of the programming languages that has gained popularity due to its design characteristics, particularly write once and run anywhere.  It is an object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, and dynamic software.  Java is a full-featured, general-purpose programming language that can be used to develop robust mission-critical applications.  Today, it is employed not only for Web programming, but also for developing standalone applications across platforms on servers, desktops, and mobile devices.   It was used to develop the code to communicate with and control the robotic rover on Mars.

Java is used to create programs known as applets, web browsing.  Unlike its HTML counterpart, Java Applets make the Web responsive, interactive, and fun to use.

1.2   INSTALLATION JDK

You need to first download the Java Development Toolkit (JDK).  Check the internet for this.  JDK enables you to have the Java Compiler downloaded.

Check whether you have the compiler by typing javac at the command prompt of DOS.

If you have an error, then you do not have the compiler.  Follow these steps to get the compiler indicated to the system:

  • Locate the local disk

  • Open the program files

  • Open the Java folder

  • Open the Jdk folder (select the version of jdk you have)

  • Open the bin folder

  • Right-click any file and select properties.  This give you the location of the file (where the compiler is).  You should have a location similar to this: C:\Program Files\Java\jdk-10.0.1\bin.  Copy this location.

  • Go to your computer properties

  • Go to the Advance System Settings

  • Select the Environment Variables under the Advance tab

  • Choose New to create a new user variable

  • Enter Path for the Variable name

  • Paste the Variable value with the location copied

Typing javac at the command prompt will tell you the compiler is now available.

You may try to run a Java code to test the availability of the compiler.  Use a text editor to type the following:

To run this program first save it as a java file from any text editor such as Notepad.

1.3   FIRST PROGRAM

For this course, we shall use the JCreator or Notepad++ for our coding.  At some points we will see the use of Eclipse and Netbeans for coding as well, but this will be for examples only.

For your first program, refer to the following listing and type it into your text editor.

1      public class Welcome {
2      public static void main(String[] args) {
3      // Display message Welcome to Java! to the console
4      System.out.println("Welcome to Java!");
5      }
6 }

Console 1.3 - Your First Program

Let's observe the lines of code for this program.  Note: Programs lines are not numbered.  Numbering is done to help students identify them.

  • Line 1 defines a class.  Every Java program must have at least one class.  Each class has a name.  By convention, class names start with an uppercase letter.  In this example, the class name is Welcome.  It is important to save your project file (source file) with the same file name as the class name and to have the extension .java.  This enables the program to run when executed.

  • Line 2 defines the main method.  In order to run a class, the class must contain a method
    named main.  The program is executed from the main method.  A method is a construct that contains statements.  The main method in this program contains the System.out.println statement.  This statement prints a message "Welcome to Java!" to the console (line 4).  Every statement in Java ends with a semicolon (;), known as the statement terminator.  Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.  For example, when the compiler sees the word class, it understands that the word after class is the name for the class.  Other reserved words in this program are public, static, and void.
    Line 3 is a comment that documents what the program is and how it is constructed.  Comments help programmers to communicate and understand the program.  They are not programming statements and thus are ignored by the compiler.  In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment.  When the compiler sees //, it ignores all text after // on the same line.  When it sees /*, it scans for the next */ and ignores any text between /* and */.

  • A pair of braces in a program forms a block that groups the program’s components.  In Java, each block begins with an opening brace ({) and ends with a closing brace (}).  Every class has a class block that groups the data and methods of the class.  Every method has a method block that groups the statements in the method.  Blocks can be nested, meaning that one block can be placed within another, as shown in the following code.

Lets take this second example in our next listing (1.2) to illustrate how we can have more than one line of text displayed by our code.

Continue with the third example in listing 1.3 for an operational activity.

Try the next example.  What do you notice.