Showing posts with label Java Program. Show all posts
Showing posts with label Java Program. Show all posts

Tuesday, 22 December 2015

Program to accept a number and display corresponding Month from an array

Java Program to print the Month Name

Demonstrate array operation using Month Name display program in Java


public class MonthNameDisplay
    static String months[] =
        null , "January" , "February" , "March" , "April", "May",
        "June", "July", "August", "September", "October",
        "November", "December"
    public static void main( String[] args )
        int m = Integer.parseInt( args[0] );
        System.out.println( months[ m ] );


java MonthNameDisplay 2

Thursday, 10 January 2013

Java Tokens

Tokens used in Java Programs

Reserved Keywords, Identifiers, Literals, Operators, Separators

A Java program is basically a collection of classes. A class is defined by a set of declaration statements and methods containing executable statements. Most statements contain expressions, which describe the actions carried out on data. Smallest individual unit in a program are known as tokens. The compiler recognizes them for building up expressions and statements. 

Java Character Set

The smallest units of Java language are the characters used to write Java tokens. These characters are defined by the Unicode character set, and emerging standard that tries to create characters for a large number of scripts world wide.
The Unicode is a 16-bit character coding system and currently supports more than 34,000 defined characters derived from 24 languages from America, Europe, Middle East, Africa and Asia. However, most of us use only  the basic ASCII characters, which include letters, digits and punctuation marks, used in normal English. We therefore, have used only ASCII character character set (a subset of UNICODE character set) in developing the programs. 

Java language includes five types of tokens and they are:

Saturday, 5 January 2013

Creating First Java Application

Java Simple Hello World! Program

A program to print the Hello World! message on the Computer screen.

You can use the notepad as an editor to write your first program. Read the below codes and carefully write on your editor as Java is case sensitive.

Hello World! code

class HelloWorld {
    public static void main (String args[]) {
System.out.println(“Hello World!”);

This is the simple base program of Java. I will explain you line by line to get an idea about the unique features that constitute a Java Program. 

Class Declaration: class HelloWorld

This line declares a class, which is an object oriented construct. We must place all codes inside a class in Java. class is a keyword and declares that a new class definition follows. HelloWorld is the Java identifier that specifies the name of the class to be defined. 

Opening and Closing Braces :{  }

Every class definition in Java begins wih an opening brace "{" and ends with a matching closing brace "}", appearing in the last line in the example.

The Main Line: public static void main (String args[])

This line defines a method named main. Conceptually, this is similar to the main() fuctnion in C and C++; Every Java application program must include main() method. This is the starting point for the interpreter to begin the execution of the program. 

public : The keyword public is an access specifier that declares the main method as unprotected and therefore making it accessible to all other classes.
static:  Static declares this method as one that belongs to the entire class and not a part of any objects of the class. The main method must always be declared as static since the interpreter uses this method before any objects are created. 
void: The type modifier void states that the main method does not return any value.

The Output Line: System.out.println(“Hello World!”);

This is similar to a printf() statement in C++. Since Java is Object Oriented, every method must be part of an object. The println() method is member of the out object, which is a static data member of System class. 

Every Java statement must end with a semicolon.

Note: We must save this program in a file called ensuring that the file name contains class name properly. This file is called the source file. If the program contains multiple classes, the file name must be the class name of the class containing the main method. 

Compiling the Program

To compile the Program, we should run the Java Compiler javac with the name of then source file on the command line as shown below:

Running the Program

We need to use the Java interpreter to run stand alone program. See the below line. 

java Helloworld




Friday, 4 January 2013

Changing the PATH Environment Variable

Setting up PATH for Java Program

We may have to set the PATH for environment variables for the easier execution of the Java Program. 

We can run the JDK without setting the Environment variable. However, it is easy to compile and run the codes written in java after setting the PATH. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it. 

Eg: C:\> "C:\Program Files\Java\jdk1.7.0\bin\javac"

To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the PATH variable. Typically, this full path looks something like C:\Program Files\Java\jdk1.7.0\bin. Set the PATH variable as follows on Microsoft Windows:

  1. Click Start, then Control Panel, then System.
  2. Select Advanced, then Environment Variables.
  3. Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.7.0\bin

Notes: You can add multiple directories separated with semicolons(;).
      PATH Environment variables are Not Case Sensitive. 

Installing Java

How to Install Java on a Windows Computer

The following instructions show how to install Java jdk to run your first Java Program

  1. Click here to open a page where you can download Java directly from Oracle. 
  2. Find the latest version, by the time I write this, the latest available on the page is "Java SE7u10". 
  3. Click on the download tab and save a known location in your Computer. 
  4. Make sure that that its byte size provided on the download page. After the download has completed, verify that you have downloaded the full, uncorrupted software file.
  5. Double Click the downloaded file to start the installer. 
  6. Follow the instructions on the Installer Wizard to complete the installation. 
Note: The JDK installer will automatically set path for the environmental variable. No need to worry about the path as of now.

Thursday, 3 January 2013

Object Oriented Programming Vs Procedure Oriented Programming

Difference between Object Oriented Programming and Procedure Oriented Programming

What is the difference between object oriented programming and procedure oriented programming?

Procedural Oriented Programming(POP)

Procedural Oriented Programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. The program is divided into small parts called functions. Full importance is not given to data but to functions as well as sequence of actions to be done. It follows Top Down approach and it does not have any access specifier. When using Procedure Oriented Programming, data can move freely from function to function in the system. However, it is difficult to add data and function on a later point of time. The functions in the Procedure Oriented Programming uses Global data for sharing that can be accessed freely from function to function in the system. Hence the system does not have an option to hide the data and it leads to insecurity.

Object Oriented Programming (OOPs)

Object Oriented Programming is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Importance is given to the data rather than procedures or functions because it works as a real world. It follows Bottom Up approach and has access specifiers named Public, Private, Protected, etc. In Object Oriented Programming, objects can move and communicate with each other through member functions. This design provides easy way to add new functions. Data cannot move easily from function to function, it can be kept public or private so we can control the access of data which provides higher data security. There are so many other features available in Object Oriented Programming compared to traditional Procedure Oriented Programming; some of them are as follows. 

The ability to create GUI (Graphical User Interface) programs

Through inheritance, we can eliminate redundant code and extend the use of existing classes.

We can build programs from standard working modules that communicate with one another rather than, having to start writing the code from scratch. This leads to saving of development time and higher productivity.

Software complexity can be easily managed.

Tuesday, 1 January 2013

Introduction to Java

An Introduction to Java Programming.

Java is an object-oriented programming language with a built-in application programming interface (API) that can handle graphics and user interfaces and that can be used to create applications. Java is a high-level, third generation programming language, like C, FORTRAN, Perl, and many others. You can use Java to write computer applications that play games, store data or do any of the thousands of other things computer software can do. Compared to other programming languages, Java is most similar to C and C++. However, it is not mandatory to learn C or C++ to learn Java. 

One major difference is that Java does not have pointers. However, the biggest difference is that you must write object oriented code in Java. Procedural pieces of code can only be embedded in objects. In the following we assume that the reader has some familiarity with a programming language. In particular, some familiarity with the syntax of C/C++ is useful.

To actually execute Java programs, they developed Java interpreters that ran on various machines and under various operating systems. Thus, Java became a language that would execute on a number of systems and now has implementations for virtually all common computers and operating systems.The most string feature of Java is that it is platform-neutral language. Java is the first programming language that is not tied to any particular hardware or Operating System. Programs developed in Java can be executed anywhere on any system. 

Java is an Object Oriented Language. It enables us not only to organize our program code into logical units called objects but also to take advantage of encapsulation, inheritance, and polymorphism.