Exploring the object oriented programming concepts using JAVA as programming language makes experience delightful. JAVA offers numerous features to model real world entities to their closest forms. The book explore syntax of JAVA with understanding of all OOP concepts. The book explains object oriented programming concepts and implementation using JAVA language. All beginner level learners will experience hands on session through use of included material in the book.
INDEX
Java Installation
Command Line Arguments in Java
Simple Java Programs
Operators in Java
Accepting Data through Keyboard and Displaying
Concept of Arrays
Objects and Classes in Java
Concept of Strings
Constructors in Java
Interface in Java
Utility Package in Java
Inheritance us in g Java
Exception Handling using Java
Multithreading using Java
Graphics using Applet
Java Installation Instructions
Java Installation Instructions
1. Downloading and installing JDK software
Click on the following link to download the latest version of JDK installer program: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Check the option “Accept License Agreement”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x32
After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):
You would see the JDK is installed in the following directory, for example: C:\Program Files\Java\jdk1.7.0_21.
Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:
java-version
You would see the following result:
Abbildung in dieser Leseprobe nicht enthalten
That shows version of the JRE, e.g. “1.7.0_21” 7
Now try to type the following command: javac-version
You would see the following error 7:
Abbildung in dieser Leseprobe nicht enthalten
That’s because Windows could not find the javac.exe program, so we need to set some environment variables which tell the location of javac.exe.
2. Setting up environment variables
set environment variables so that the javac.exe program can be accessed anywhere from command line. On Windows 7, go to My Computer and click System Properties 7:
Abbildung in dieser Leseprobe nicht enthalten
Then click Advanced system settings7:
Abbildung in dieser Leseprobe nicht enthalten
The System Properties dialog appears, select Advanced tab and click Environment Variables...:
The Environment Variable dialog appears, click on the New… button under the System variables section.
That opens up the New System Variable dialog. Type the following information 7:
Abbildung in dieser Leseprobe nicht enthalten
The field Variable name must be JAVA_HOME, and the field Variable value must point to JDK’s installation directory on your computer. Here it is set to c:\Program Files\Java\jdk1.7.0_21. Click OK to close this dialog.
Now back to the Environment Variables dialog, look for a variable called Path under the System Variables list, and click Edit…7:
Abbildung in dieser Leseprobe nicht enthalten
In the Edit System Variable dialog, append the following to the end of the field Variable value:
;%JAVA_HOME%\bin 7
Abbildung in dieser Leseprobe nicht enthalten
Note that there is a semicolon at the beginning to separate this value from other ones. Click OK three times to close all the dialogs.
Now we have to quit the current command prompt and open a new one to see the changes takes effect. Type the following command again in the re-opened command prompt window:
javac-version
You would see the following output7:
Abbildung in dieser Leseprobe nicht enthalten
You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.
3. Writing a Java hello world program
Open a simple text editor program such as Notepad and type the following content:
Abbildung in dieser Leseprobe nicht enthalten
Save the file as HelloWorld.java (note that the extension is .java) under a directory, let’s say, C:\Java.
Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:
Abbildung in dieser Leseprobe nicht enthalten
Figure 1. Syntax of Java main function 1
public class HelloWorld
In above class definition
- public means accessible from outside
- class is keyword to create a ‘type’
- HelloWorld is class name
Every Java program starts from the main() method. This program simply prints “Hello world” to screen.
4. Compiling it
Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored: cd C:\Java
And type the following command: javac HelloWorld.java
That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file name ends with .java extension. You would see the following output7:
Abbildung in dieser Leseprobe nicht enthalten
If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command line, we’ll see the .class file7:
Abbildung in dieser Leseprobe nicht enthalten
So remember a Java program will be compiled into bytecode form (.class file).
5. Running it
It’s now ready to run our first Java program. Type the following command: java HelloWorld
That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output7:
Abbildung in dieser Leseprobe nicht enthalten
It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!
6. What we have learnt
The following things are learnt:
- JDK is the Java SE Development Kit that contains tools and libraries for Java development.
- JRE is the Java Runtime Environment that enables running Java programs on your computer.
- JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language).
- How to install JDK and configure environment variables.
- javac.exe is the Java compiler. It translates Java source code into bytecode.
- java.exe is the JVM launcher which we use to run our program.
- Every Java program starts from the main() method.
- When compiling, the compiler generates a .class file from a .java file.
Experiment No.: 01
Name of Experiment: Write a program for command line argument in java.
Aim: program for command line argument in java
Equipments/Machines: Win XP OS, JDK
Theory:
Java contains two types of Command Line Arguments:-
The Java application can accept any number of arguments from the command line. Command line arguments allow the user to affect the operation of an application. For example, a program might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command line argument-verbose.
When invoking an application, the user types the command line arguments after the application name. For example, suppose you had a Java application, called Sort, that sorted lines in a file, and that the data you want sorted is in a file named ListOfFriends. If you were using DOS, you would invoke the Sort application on your data file like this:
C :\> java Sort List Of Friends
In the Java language, when you invoke an application, the runtime system passes the command line arguments to the application's main method via an array of Strings. Each String in the array contains one of the command line arguments. In the previous example, the command line arguments passed to the Sort application is an array that contains a single string: "ListOfFriends".
1. Echoing Command-Line Arguments.
This simple application displays each of its command line arguments on a line by itself:
Algorithm:
1. define class Echo
2. declare main method public static void main (String args[]){
3. for (int i = 0; i < args.length; i++)
4. Print args[i].
5. Stop
2. Parsing Numeric Command-Line arguments.
Algorithm:
1. public class Switch
2. public static void main(String key[])
3. int x=Integer.parseInt(key0);
4. switch(x)
5. case 1: System.out.println("Monday")
6. case 2: System.out.println("Tuesday")
7. case 3: System.out.println("Wednesday")
8. case 4: System.out.println("Thursday")
9. case 5: System.out.println("Friday");
10. case 6: System.out.println("Saturday")
11. case 7: System.out.println("Sunday")
12. End of switch
13. End of main.
14. End of class
Conclusion: Thus we have studied command line argument in java.
Sample Expert Viva-vice Questions:
1. What is Command line argument?
2. Explain disadvantage of command line arguments.
3. Why to use command line argument.
Solution:
Abbildung in dieser Leseprobe nicht enthalten
Experiment No.: 02
Name of Experiment: Write simple java programs.
Aim: Study of simple java programs.
a) WAP to calculate area & circumference of circle
b) WAP to swap given two strings
c) WAP to separate out digits of a number
d) WAP to convert temperature from Fahrenheit to Celsius
Equipments/Machines: Win XP OS, JDK
Theory:
Java contains two general categories of built in data-types:-
1.Primitive data-types:
At the core of Java are eight primitive (also called elemental or simple) types of data. The term primitive is used here to indicate that these types are not objects in an object-oriented sense, but rather, normal binary values.
Integers: Java defines four integer types: byt e, shor t, in t, and lon g, which are shown here: Type Width in Bits Range:
Abbildung in dieser Leseprobe nicht enthalten
All of the integer types are signed positive and negative values. Java does not support unsigned (positive-only) integers. The most commonly used integer type is in t. Variables of type int are often employed to control loops, to index arrays, and to perform general-purpose integer math.When you need an integer that has a range greater than in t, use lon g. The smallest integer type is byt e. Variables of type byte are especially useful when working. The short type creates a short integer that has its high-order byte first.
Floating-Point Types:
The floating-point types can represent numbers that have fractional components. There are two kinds of floating-point types, float and doubl e, which represent single and double-precision numbers, respectively. Type float is 32 bits wide and type double is 64 bits wide.
Characters:
In Java, characters are not 8-bit quantities like they are in most other computer languages. Instead Java uses Unicode. Unicode defines a character set that can represent all of the characters found in all human languages. Thus, in Java, char
is an unsigned 16-bit type having a range of 0 to 65,536. The standard 8-bit ASCII character set is a subset of Unicode and range from 0 to 127. Thus, the ASCII characters are still valid Java characters. A character variable can be assigned a value by enclosing the character in single quotes.
The Boolean Type :
The boolean type represents true/false values. Java defines the values true and false using the reserved words true and fals e. Thus, a variable or expression of type boolean will be one of these two values.
2. Non primitive data-types
Composite, or reference, data types consist of more than a single element. Composite data types are of two kinds: classes and arrays. Class and array names start with an upper case letter and are camel-capitalized. Any class can be used as a data type once it has been created and imported into the program. Because the String class is the class most often used as a data type.
Algorithm :
1. Read radius of circle as r
Abbildung in dieser Leseprobe nicht enthalten
3. Read a number n1,declare an integer number d
Abbildung in dieser Leseprobe nicht enthalten
4. Read temperature t in Fahrenheit, declare c
Abbildung in dieser Leseprobe nicht enthalten
[...]
- Quote paper
- Prof Sheetal Thakare (Author), Dr M. A. Pund (Author), 2021, JAVA. Experiencing Object Oriented Programming, Munich, GRIN Verlag, https://www.grin.com/document/1044961
-
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X.