Sunteți pe pagina 1din 8

UET TAXILA

Lab Manual

OOP
4thSemester Lab-01: Running Java Programs

Laboratory 01: Introduction to Running Simple Programs in Java


Lab Objectives: After this lab, the students should be able to

1. Install JDK
2. Run simple Java Programs

Software Development Kit (SDK) that contains the following:

Libraries: also known as Application Programming Interface (API), these files are previously
written classes and methods that contain some common functionality.

Compiler: the program that translates files written in Java language (human language) into
binary files (machine language) in order to execute them.

Interpreter: Some programming languages do not compile the source code file into directly
executable form (native code), but they instead compile it into partially compiled file that could
be executed by a program called interpreter.

Java Programming Language:

Java programming language is an interpreted programming language, that is, when the source
code is compiled into binary file, it needs an interpreter called Java Virtual Machine (JVM) to
run it. Java compiler is called javac.exe, and interpreter is called java.exe.

The Figure below shows a complete path of running Java programs.

1. JDK INSTALLATION:

For compiling and running java programs we need to install JDK.

A JDK distribution consists of a JRE distribution (a Java Virtual Machine implementation)


plus a collection of development tools including javac :a Java compiler, and javadoc: Java
documentation generator.

DOWNLOADING JDK 11:

Engr. Sidra Shafi Lab-01 1


4thSemester Lab-01: Running Java Programs

Follow the steps for downloading jdk from net.

 Go to this link and download JDK 11


https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html

 Click on the Java Download icon.

 Following window will appear on your screen. Check the accept license option and
click on the highlighted version to download jdk.

 After downloading double click the jdk icon to start installation process.

INSTALLATION STEPS:

Engr. Sidra Shafi Lab-01 2


4thSemester Lab-01: Running Java Programs

 Click NEXT to move ahead.

 Click NEXT

Engr. Sidra Shafi Lab-01 3


4thSemester Lab-01: Running Java Programs

 Now click Next Steps if you want to read more about JDK.
 Click OK.

 Click Finish to end installation process.

 Now you can create, compile and run java programs.

Note: For Lab, JDK 11 is given.

Use this link for Setting Environment Variable:


https://www.theserverside.com/tutorial/How-to-install-the-JDK-on-Windows-and-
setup-JAVA_HOME

https://www.java.com/en/download/help/path.xml //For other Operating Systems

Video.

https://www.thecoderworld.com/windows/how-to-install-java-jdk-11-on-windows-10/

Important Changes and Information


The following are some important changes in and information about this release. In some
cases, additional details about the changes described below are provided in these Release
Notes.
 The deployment stack, required for Applets and Web Start Applications, was deprecated in
JDK 9 and has been removed in JDK 11.
 Without a deployment stack, the entire section of supported browsers has been removed from
the list of supported configurations of JDK 11.
Engr. Sidra Shafi Lab-01 4
4thSemester Lab-01: Running Java Programs

 Auto-update, which was available for JRE installations on Windows and macOS, is no longer
available.
 In Windows and macOS, installing the JDK in previous releases optionally installed a JRE. In
JDK 11, this is no longer an option.
 In this release, the JRE or Server JRE is no longer offered. Only the JDK is offered. Users can
use jlink to create smaller custom runtimes.
 JavaFX is no longer included in the JDK. It is now available as a separate download from
openjfx.io. JavaFX is a Java library used to build Rich Internet Applications. The applications
written using this library can run consistently across multiple platforms. The applications
developed using JavaFX can run on various devices such as Desktop Computers, Mobile
Phones, TVs, Tablets, etc.
 Java Mission Control, which was shipped in JDK 7, 8, 9, and 10, is no longer included with
the Oracle JDK. It is now a separate download.
 Previous releases were translated into English, Japanese, and Simplified Chinese as well as
French, German, Italian, Korean, Portuguese (Brazilian), Spanish, and Swedish. However, in
JDK 11 and later, French, German, Italian, Korean, Portuguese (Brazilian), Spanish, and
Swedish translations are no longer provided.
 Updated packaging format for Windows has changed from tar.gz to .zip, which is more
common in Windows OSs.

2. CREATING A SIMPLE JAVA PROGRAM:

 Open up an editor (notepad) and write the following code.

class Uet
{
public static void main(String abc[])
{
System.out.println("My first Java Program");
}
}

 Save the program in C:\program Files\java\jdk11\bin with the name Uet (same as the
name of class) and Extension (.java) i.e.; Uet.java.

 In order to compile and run the program do the following


Start -> Run ->(type) cmd ->(press Ok).

 Then in command prompt write the following.

Engr. Sidra Shafi Lab-01 5


4thSemester Lab-01: Running Java Programs

This is done to change the directory to the drive where bin of java is installed.

cd is a command used for changing directory.

 Now compile the program on command prompt as:

The command javac converts the java source code into byte code program.

 Assuming your program contains no errors, the compiler generates a byte code program
that is equivalent of your source code. The compiler stores the byte code program in a
file with the same name as source file, but with the extension .class.

 To execute the byte code program in the .class file with the java interpreter in the JDK,
you enter the command

 If there is no exception in the program the output is printed on the command prompt.

LAB TASKS
Task 1: Marks: 5

Run the following codes and see the result.

Run the following programs. Observe the output. Focus on comments to enhance the
understandability.

Program # 1
class Program1{
//your program begins with a call to main().

public static void main (String args[] )

System.out.println(“ Welcome to java world !”); //println() displays the string which is passed
to it.

Engr. Sidra Shafi Lab-01 6


4thSemester Lab-01: Running Java Programs

Program # 2
class Program2 {
public static void main(String args[]) {
int a,b,c; //this statement declares three variables a, b and c.
a=2;
b=3;
c=a+b;
System.out.println("Sum of two numbers = "+c); }
}

Task 2: Marks: 5

Run the following code by saving it firstly with name abc.java and then with bcd.java
and check the output.

class abc
{
public static void main(String args[])
{
System.out.println(“hello world!”);
}
}
class bcd
{
public static void main(String args[])
{
System.out.println(“This is my First Program!”);
}
}

******************

Engr. Sidra Shafi Lab-01 7

S-ar putea să vă placă și