[go: up one dir, main page]

0% found this document useful (0 votes)
32 views6 pages

OOP Lab Manual 1

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

OOP Lab Manual 1

Uploaded by

Aamir Rasool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH

OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

PRACTICAL MANUALS
FOR ACADEMIC SESSION 2022

OBJECT ORIENTED PROGRAMMING


For
21 BS(AI) BATCH
Name: _____________________________________
Roll Number: ________________________________
Batch: ______________________________________
Department: _________________________________

DEPARTMENT OF ARTIFICIAL INTELLIGENCE

QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE &


TECHNOLOGY, NAWABSHAH

1
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Name: _____________________________ Roll No: __________________________________

Signature of Lab Tutor: ____________________ Date: ________________________________

LAB 1: INTRODUCTION TO JAVA AND FIRST JAVA PROGRAM

OBJECTIVES

 To study the features and working environment of Java language.


 To write, compile and run first Java program.

THEORY

Java Virtual Machine & Java Runtime Environment


Basic Concept
When you write a program in C++ it is known as source code. The C++ compiler converts this
source code into the machine code of underlying system (e.g. Windows) If you want to run that
code on Linux you need to recompile it with a Linux based compiler. Due to the difference in
compilers, sometimes you need to modify your code. Java has introduced the concept of WORA
(write once run anywhere). When you write a java program it is known as the source code of
java. The java compiler does not compile this source code for any underlying hardware system;
rather it compiles it for a software system known as JVM (This compiled code is known as byte
code). We have different JVMs for different systems (such as JVM for Windows, JVM for Linux
etc). When we run our program the JVM interprets (translates) the compiled program into the
language understood by the underlying system. So we write our code once and the JVM runs it
everywhere according to the underlying system. This concept is discussed in detail below

2
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH

Bytecode
Java programs (Source code) are compiled into a form called Java bytecodes. The Java compiler
reads Java language source (.java) files, translates the source into Java bytecodes, and places the
bytecodes into class (.class) files. The compiler generates one class file for each class contained
in java source file.

Java Virtual Machine (JVM)


The central part of java platform is java virtual machine. Java bytecode executes by special
software known as a "virtual machine". Most programming languages compile source code
directly into machine code, suitable for execution. The difference with Java is that it uses
bytecode - a special type of machine code. The JVM executes Java bytecodes, so Java bytecodes
can be thought of as the machine language of the JVM. JVM are available for almost all
operating systems. Java bytecode is executed by using any operating system’s JVM. Thus
achieve portability.
Java Runtime Environment (JRE)
The Java Virtual Machine is a part of a large system i.e. Java Runtime Environment (JRE).
Every operating system and CPU architecture requires different JRE. The JRE consists of set of
built-in classes, as well as a JVM. Without an available JRE for a given environment, it is
impossible to run Java software. Any computer that has the Java environment implemented will
handle your program as well as any other, and because the Java interpreter sits between your
program and the physical machine, it can prevent unauthorized actions in the program from
being executed.

Java Program Development and Execution Steps


Java program normally go through five phases. These are
1. Edit,
2. Compile,
3. Load,
4. Verify and
5. Execute
We look over all the above mentioned phases in a bit detail. First consider the following figure
that summarizes the all phases of a java program.

Phase 1: Edit
Phase 1 consists of editing a file. This is accomplished with an editor program. The programmer
types a java program using the editor like notepad, and make corrections if necessary. When the
programmer specifies that the file in the editor should be saved, the program is stored on a
secondary storage device such as a disk. Java program file name ends with a .java extension.

3
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
On Windows platform, notepad is a simple and commonly used editor for the beginners.
However java integrated development environments (IDEs) such as NetBeans, Borland JBuilder,
JCreator and IBM’s Ecllipse hava built-in editors that are smoothly integrated into the
programming environment.

Phase 2: Compile
In Phase 2, the programmer gives the command javac to compile the program. The java compiler
translates the java program into bytecodes, which is the language understood by the java
interpreter.
To compile a program called welcome.java, type javac welcome.java at the command window of
your system. If the program compiles correctly, a file called welcome.class is produced. This is
the file containing the bytecodes that will be interpreted during the execution phase.
Phase 3: Loading
In phase 3, the program must first be placed in memory before it can be executed. This is done
by the class loader, which takes the .class file (or files) containing the bytecodes and transfers it
to memory. The .class file can be loaded from a disk on your system or over a network (such as
your local university or company network or even the internet). Applications (Programs) are
loaded into memory and executed using the java interpreter via the command java. When
executing a Java application called welcome, the command Java welcome
Invokes the interpreter for the welcome application and causes the class loader to load
information used in the welcome program.
Phase 4: Verify
Before the bytecodes in an application are executed by the java interpreter, they are verified by
the bytecode verifier in Phase 4. This ensures that the bytecodes for class that are loaded form the
internet (referred to as downloaded classes) are valid and that they do not violate Java’s security
restrictions.
Java enforces strong security because java programs arriving over the network should not be able
to cause damage to your files and your system (as computer viruses might).
Phase 5: Execute
Finally in phase 5, the computer, under the control of its CPU, interprets the program one
bytecode at a time. Thus performing the actions specified by the program. Programs may not
work on the first try. Each of the preceding phases can fail because of various errors. This would
cause the java program to print an error message. The programmer would return to the edit
phase, make the necessary corrections and proceed through the remaining phases again to
determine the corrections work properly.

4
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
EXERCISE:

Q1. Describe the role of Java Virtual Machine (JVM)?

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Q2. Write the steps to write, compile and run a Java program.
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Task#01: Type the following Java program in Notepad, execute it and write the output.

1. Open any simple editor like Notepad or Notepad++.


2. Write the following code (to print a simple welcome message on the screen) and save the
file with the name “welcome.java”.

3. Open command prompt to compile and run the program using following commands:
a. Press Windows+R key cmd (press enter key)
b. Go to the bin directory inside java (cd C:\Program Files\Java\jdk-16.0.1\bin)
c. You may need to set path to java’s bin directory for example by writing (set
path=C:/Program Files/Java/jdk-16.0.1/bin)
d. Type (javac welcome.java) to compile the program
e. Type (java welcome) to run the program 

OUTPUT:
______________________________________________________________________________

5
DEPARTMENT OF ARTIFICIAL INTELLIGENCE, QUEST NAWABSHAH
OBJECT ORIENTED PROGRAMMING, 21 BS(AI) BATCH
______________________________________________________________________________
______________________________________________________________________________

Task#02: Write a java program to display your name, roll number, class and department
on separate lines.
OUTPUT:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

You might also like