Lab 1 - Development Environment and Basic Constructs in Java
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
4. Concept Map 5
4.1 Java Development Kit 5
4.2 Java VirtualMachine 5
4.3 Java class file 6
4.4 Class Modifiers 6
4.5 Static functions 6
6. Procedure& Tools 7
6.1 Tools 7
6.2 Setting-up JDK 1.8 [Expected time = 30mins] 7
6.2.1 Download JDK 7
6.2.2 Install JDK 7
6.2.3 Set Path 8
6.2.4 Set Classpath 8
6.2.5 Set Path Permanently 9
6.2.6 Compile a Program 10
6.2.7 Run a Program 10
7. Practice Tasks 10
7.1 Practice Task 1 [Expected time = 15mins] 10
7.2 Practice Task 2 [Expected time = 15mins] 10
7.3 Practice Task 3 [Expected time = 15mins] 10
7.4 Practice Task 4 [Expected time = 15mins] 11
7.5 Out comes 11
7.6 Testing 11
9. Evaluation criteria 12
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,
calledmain(). The first line of main() is always like the following
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.
4. 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 bytecode. 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 bytecode. 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
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.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to design
the solution of the following problems. You are required to bring this code with you and
submit to your lab instructor.
Write down codes to calculate the GPA of the student after getting the grades of all the courses
that the student has registered in the current semester.
5.3.1 Task-1
Make a list of features that are available in C++ but missing in Java.
5.3.2 Task-2
5.3.3 Task-3
6. Procedure& Tools
In this section you will study how to setup and configure JDK.
6.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
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.
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
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
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
screen.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\fs\assignments$\Advanced Computer Programming\Lab1
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.
7.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.
Sample Sample
Inputs-1 Outputs-1
0
1
1
2
3
5
8
13
Test Cases for Practice Task-3 Test Cases for Practice Task-4
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.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
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\