Lab 1 Development Environment and Basic Constructs in Java
Lab 1 Development Environment and Basic Constructs in Java
Lab-01
Development Environment and Basic Constructs in Java
Lab 1: Development Environment and Basic Constructs in Java
Table of Contents
1. Introduction 3
1.1 Writing “Hello World” program 3
1.1.1 System: 4
1.1.2 out: 4
1.1.3 println: 4
3. Concept Map 5
3.1. Java Development Kit 5
3.2. Java Virtual Machine 5
3.3. Java class file 5
3.4. Class Modifiers 5
3.5. Static functions 6
4. Procedure& Tools 6
4.1 Tools 6
4.2 Setting-up JDK 1.7 6
4.2.1 Download JDK 6
4.2.2 Install JDK 7
4.2.3 Set Path 7
4.2.4 Set Classpath 8
4.2.5 Set Path Permanently 8
4.2.6 Compile a Program 9
4.2.7 Run a Program 9
5. Practice Tasks 10
5.1 Practice Task 1 10
5.2 Practice Task 2 10
5.3 Practice Task 3 10
5.4 Practice Task 4 10
5.5 Out comes 10
5.6 Testing 10
6. Further Reading 11
6.1 Books 11
6.2 Slides 11
Page 2
Lab 1: Development Environment and Basic Constructs in Java
1. Introduction
You have looked at the basic characteristics of Java and the benefits of using Java over C++. The
objective of this lab is to get familiar with the Java Runtime Environment to execute your first
program. You will also learn and practice how to install and configure Java Development Kit
(JDK).
Every application created in Java must contain a class that has a method called main() which acts
as an entry point like the main() we used to define in C++. After compiling the program/ classes
you will use the name of class that contains the main to start/ run your program. There are no
restrictions for the name of your class, you can give any name to the class but the name of entry
point function is always called main(). When you run your program, main() method will be used
to call other methods from the classes in your program. For your first lab, you will only be
writing a program which has only one class and only one method called main().Following
section explains how to write your first program while highlighting the importance of all the
constructs used in the program.
Figure 1 shows a “Hello World” Program. You can understand the program by reading the
explanations given alongside of code.
You need to enter the program code using your favorite plaintext editor e.g. a Notepad;you can
change the editors at the later stages to modify the code. By writing the code in the Notepad you
will easily get familiar with the naming conventions of Java.
When you have typed the code, save the file with the same name as the one used for the class
and with the extension “.java”. Remember that the name is case sensitive.
Example:
The program has a single class called MyFirstProgram. The class contains only one method,
called main(). The first line of main() is always like the following
Page 3
Lab 1: Development Environment and Basic Constructs in Java
1.1.1 System:
System is the name of the class that is contained in the package called java.lang. The package is
by default included in you program. System provides you access to command line arguments and
helps to display the data on the console.
1.1.2 out:
The method out is a static data member defined in the class called System. “out” represents the
output stream.
1.1.3 println:
The statement println(“Hello World”) calls the println() method that is the property of out object.
Its purpose is similar to the cout<< statement of C++ as it displays whatever is passed in the
parenthesis on the console.In this case, it will display “Hello World” on the screen.
Page 4
Lab 1: Development Environment and Basic Constructs in Java
3. Concept Map
This section provides you the overview of the concepts that will be discussed and implemented
in this lab.
Java Development Kit commonly known as JDK is available in three different flavors targeting
all the operating systems. The three different flavors are as follows
In this course, we will be studying about developing desktop applications so we will be using
J2SE. J2ME is used to develop mobile applications and J2EE is used to develop server side
applications.
A Java virtual machine (JVM)is used to execute the class files or byte code. JVM's are available
for all the operating systems and can also be configured to run on any hardware directly. Java
Runtime Environment commonly known as JRE is provided by the JVM.
The file with extension “class” that you get after compiling the Java code is called “Java class
file”. This is the same file which is also called the byte code. Java class file is executed by the
JVM to generate output of our program.
Java also differs from C++ in a sense that the classes now have the modifiers too. In C++, classes
had modifiers i.e. public, private and protected only in case of inheritance but now in Java
whenever we declare a class we now have to define its modifier as well. Following line shows
the syntax for adding class modifier in “student” class
Page 5
Lab 1: Development Environment and Basic Constructs in Java
It is important to note here that any class that contains the method main() must be declared
public.
Static methods and static variables are the same thing. Both are the properties of the class and not
the properties of the object. That means that to call a static method or static variable you will not
need to create an object, you will be able to call it directly using class name.
4. Procedure& Tools
In this section you will study how to setup and configure JDK.
4.1 Tools
Go to www.oracle.com and search for the JDK that is compatible with your Operating system.
You will probably be using windows so, download the JDK version that is available for
Microsoft Windows. Figure 2 shows the download page for JDK 1.7
Download Link: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Page 6
Lab 1: Development Environment and Basic Constructs in Java
There are two different types of JDKs for the type of system architecture that you are using.
For example, if you were downloading the JDK installer for 32-bit systems then the file that you
have to download is namedjdk-7u1-windows-i586.exe.
Similarly, if you were downloading the JDK installer for 64-bit systems thenjdk-7u1-windows-
x64.exe is the name of the file that you have to download and install.
Install by executing the jdk-7u1-windows-i586.exe file or whichever exe you have downloaded.
It is recommended that you do not change the paths of installation directory as this manual is
written with reference to the default paths.
Path statement enables our environment to include the new utilities. Like for newly installed
JDK, the Windows environment/console does not know that there are new exe/utilities that can
be used to compile and run a Javaprogram. Therefore it is important to set the path for new
utilities using the following steps.
Now your environment will be able to recognize commands like java, javac.
Remember, that you will have to set the path for each new console that you open or you will
have to make bin as the current working directory. Figure 3 shows how to make bin directory as
your current working directory.
Page 7
Lab 1: Development Environment and Basic Constructs in Java
classpath in Java is a term that refers to directory or directories which are used by JVM to
resolve the classes in a Java program. Classpath can be specified using CLASSPATH
environment variable or –cp flag while compiling the program.
In Windows current working directory is always included in the class path. To include other
directories or packages in the classpath type the following command.
Set classpath= C:\”Directory Name”\;
As previously mentioned that you will have to set the path every time for each new console that
you open. To make things simple you can permanently set the path so it will persist after you
restart the system.
To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the
PATH variable. Following steps will guide you through the process on Windows.
C:\Program Files\Java\jdk1.7.0\bin
Page 8
Lab 1: Development Environment and Basic Constructs in Java
By now you have opened a console and completed set 6.2.4 and 6.2.5. You have also created a
Hello World program described in section 1.1. Now is the time to finally compile the program.
Your current working directory must contain the class named MyFirstProgram. Use the
following command to compile the program.
javacMyFirstProgram.java
After the execution of the above statement bytecode of your class will be generated with same
name as the .java file but its extension will be changed to .class. Now,you will need JVM to run
the program.
After successfully completing the section 6.2.6, the only thing left is to execute the bytecode on
the JVM. Use the following command to run the program.
javaMyFirstProgram
If the above command executed successfully then you will see “Hello World” printed on the
Page 9
Lab 1: Development Environment and Basic Constructs in Java
screen.
5. Practice Tasks
This section will provide more practice exercises related to development. You need to finish the
tasks in the required time. When you finish them, put these tasks into the
https://moellim.riphah.edu.pk/.
Display Fibonacci series according to the user requirement. User must enter start and end
number of Fibonacci series and display all the series in between star and end number.
Take 10 characters as input using command line arguments and tell how many capital letters,
small letters, digits or special characters are.
Convert the temperature from Celsius to Fahrenheit and vice versa by getting data from user
using the command line arguments. Use the following formula.
°C * 9/5 + 32 = °F
After completing this lab, student will be able to setup JDK. He/ She will also be able to compile
and run basic Java programs.
5.6 Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs then your program is right.
Page 10
Lab 1: Development Environment and Basic Constructs in Java
Sample Sample
Inputs-1 Outputs-1
Enter Start 0,1,1,2,3,5,8,13
number : 0
Enter Start
number : 13
Test Cases for Practice Task-3 Test Cases for Practice Task-4
6. Further Reading
6.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-
outreach/resources/java-a-beginners-guide-1720064.pdf
6.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at https://moellim.riphah.edu.pk/
Page 11