0% found this document useful (0 votes)
4 views

Lab_01-OOP

The document outlines a lab report for a Java programming course at Sukkur Institute of Business Administration, detailing objectives, installation of JDK, and basic Java programming concepts. It includes a rubric for grading, instructions for setting up the Java environment, and exercises for students to practice their skills. Key topics covered include Java features, data types, variable types, input/output operations, and type casting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab_01-OOP

The document outlines a lab report for a Java programming course at Sukkur Institute of Business Administration, detailing objectives, installation of JDK, and basic Java programming concepts. It includes a rubric for grading, instructions for setting up the Java environment, and exercises for students to practice their skills. Key topics covered include Java features, data types, variable types, input/output operations, and type casting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sukkur Institute of Business Administration University

Department of Computer Science


BS – II (CS/SE/AI) Spring 2024
Object Oriented Programming
Lab # 01: To become familiar with Downloading, installing
& setting up JDK to write, compile, execute and debug Java
programs
Instructor: Engr. Zainab Umair Kamangar

Lab Report Rubrics Total


(Add the points in each column, then add across the bottom row to find the Marks
total score)
S.No Criterion 0.5 0.25 0.125
1 Accuracy  Desired output  Minor  Critical
mistakes mistakes
2 Timing  Submitted within  1 day late  More than
the given time 1 day late

Note: Submit this lab hand-out in the next lab with attached solved activities and exercises

Submission Profile
Name: Submission date (dd/mm/yy):
Enrollment ID: Receiving authority name and signature:
Comments:
__________________________________________________________________________
Instructor Signature

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 1


Objectives
After performing this lab, students will be able to
• What is JAVA?
• Features of Java.
• JAVA Basics.
• Downloading, and Installing JDK and setting path.
• Writing HelloWorld.java in Text Editor
• Learn to write, compile, and execute simple Java programs using Notepad
• JAVA variables & data types.
• Input & Output
• Java Variable Type Conversion & Type Casting

What is JAVA?
Java was developed by Sun Microsystems in 1995. It is a programming language as well
as platform. Java is among the most popular programming languages out there, mainly
because of how versatile and compatible it is. Java is general purpose programming language
as it is used for software development, mobile applications, web servers, and client-side web
applications. It is the native language of the Android operating system, which operates on
Android phones and tablets.

Versions of Java
There are many java versions that has been released.
JDK Alpha and Beta (1995)
JAVA 1.0 (23rd Jan, 1996)
JAVA 1.1 (19th Feb, 1997)
JAVA 1.2 (8th Dec, 1998)
JAVA 1.3 (8th May, 2000)
JAVA 1.4 (6th Feb, 2002)
JAVA 5.0 (30th Sep, 2004)
JAVA 6 (11th Dec, 2006)
JAVA 7 (28th July, 2011)
JAVA 8 (18th March, 2014)
JAVA 9 (21th Sep, 2017)
JAVA 10 (20th March, 2018)
JAVA 11 (25th Sep 2018)
JAVA 12 (19th March 2019)
JAVA 13 (17th Sep 2019)
JAVA 14 (17th March 2020)
JAVA 15 (15th Sep 2020)
JAVA 16 (16th March 2021)

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 2


JAVA 17 (14th Sep 2021)
JAVA 18 (22nd March 2022)
JAVA 19 (20th Sep 2022)
JAVA 20 (21st March 2023)
JAVA 21 (19th Sep 2023)

Platform: Any hardware or software environment in which a program runs, is known as a


platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

Java Platforms
According to Oracle, there are four platforms of the Java programming language
Java Platform, Standard Edition (Java SE)
Java Platform, Enterprise Edition (Java EE)
Java Platform, Micro Edition (Java ME)
JavaFX/OpenJFX

Features of JAVA
The following are Java's main features:
• Object Oriented − In Java, everything is an object, creating objects that contain data
and methods.
• Architecture-neutral − Traditionally, we would have to recompile a program for
every system that it was going to run on because all systems have a different idea of
what their machine code should look like. Java compiler generates an architecture-
neutral object file format called as bytecode, which makes the compiled code
executable on many machines but with the presence of Java runtime system.
• Platform Independent − Unlike many other programming languages including C and
C++, when Java is compiled, it is not compiled into platform specific machine, rather
into byte code. This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.
• Portable − Bytecode can be run by any system in which Java is installed. This is
because when java is installed, Java virtual machine is also installed that is specific to
that system. It is this machine's responsibility to convert the bytecode into the final
instructions of that particular machine.

By making it the system's responsibility to do this final conversion, Java has created a write
once, run anywhere language where anyone can hand you a Java program and you can run it
on your machine
“Write once, run everywhere”

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 3


JAVA Basics

JDK
• It stands for Java Development Kit, is a software development environment used for
developing Java applications and applets.
• It compiles and executes new and already built applications.
• It is a collection of development tools as well as Java Runtime Environment (JRE), an
interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation
generator (Javadoc) and other tools needed in Java development.

JRE
• It stands for Java Runtime Environment. The Java Runtime Environment provides the
minimum requirements for executing a Java application.
• It consists of the Java Virtual Machine (JVM), interpreter, JIT, core classes,
and supporting files.

JVM
• It stands for Virtual Machine (JVM)
• It is responsible for executing bytecode where interpreter provides machine code for
the current machine and has JIT as well.

JIT
• It stands for Just-in-time Compiler, is the part of the Java Virtual Machine (JVM) that is
used to speed up the execution time.
• JIT interprets parts of the bytecode that have similar functionality at the same time and
hence reduces the amount of time needed for full interpretation.

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 4


DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 5
Installing JDK and setting path
To develop Java applications on our computers, we require a JDK. Visit the link below to
download the JDK setup.
https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.exe
Double click downloaded JDK and click YES to allow JDK to make changes to system and then
click NEXT.

Installation location by default is C:\Program Files\Java\jdk-17\

Installation in progress

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 6


JDK successfully installed, click CLOSE

We need to set Java path so that whenever java program will be compiled or executed it can
locate JDK easily. Path can be set in two ways i.e. temporarily and permanently.

To set java path temporarily, open command prompt and run command “set path=C:\Program
Files\Java\jdk-21\bin”. Once the command prompt is closed, java path needs to be set again.

To set java path permanently, this path “C:\Program Files\Java\jdk-21 \bin” should be added
as per following steps.
Go to Control Panel > System and Security > System, and then click Advanced system settings
to view System properties window. In system properties window press advanced tab and
click on Environment variables to add new path. Finally click ok and you are all set.

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 7


To check if the Java compiler is installed, open command prompt and run command
“javac -version”. If the command returns something such as “javac 21.0.1”, it means JDK
having Java compiler is installed successfully.

To check version information of JDK, JRE and JVM, open command prompt and run command
“java -version”.

Writing HelloWorld.java in Text Editor


Follow the below given steps:
i. Run notepad and enter below given code. Save this file with Class name and end it with
“.java” extension. Save it on desktop for now!

ii. Go to search bar in taskbar, write cmd and Open Command Prompt then write the
following commands;
a. cd desktop //for going to desktop
b. javac HelloWorld.java
c. java HelloWorld
The Java programming language compiler (javac) takes your source file and translates the
code into instructions known as bytecodes. “Java ClassName” will enable Java virtual
machine to run your application/code.

Variables and data types in JAVA


In Java, there are three types of variables:
• Local Variables
• Instance Variables
• Static Variables

Local Variables
Local Variables are declared inside the body of a method.
Scope: Variables declared inside a method have method level scope and cannot be accessed
outside the method.

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 8


Instance Variables
Instance variables are defined without the ‘static’ keyword. They are defined outside a
method within a class. Access modifiers can be given for instance variables. They are Object-
specific.
Scope: Dependent on the access modifier.

Class/Static Variables
It is declared with the keyword ‘static’, outside the method within a class. Static variables are
created when the program starts and destroyed when the program stops. There would only
be one copy of each static variable per class, regardless of how many objects are created.
Scope: Visibility is like instance variables. However, most static variables are declared public
since they must be available for users of the class.

Example: Types of Variables in Java

class Variab {
int InsVarExam = 29; //instance variable
static int IsStatVar = 15; //static variable
void method() {
int IsLocalVar = 90; //local variable
}
}

Data Types in Java


Data types classify the different values to be stored in the variable. In java, there are two
types of data types:
• Primitive Data Types
• Non-primitive Data Types

Non primitive as arrays, strings


Primitive Data Types
Primitive Data Types are predefined and available within the Java language. Primitive values
do not share state with other primitive values.
There are 8 primitive types: byte, short, int, long, char, float, double, and boolean
Integer data types
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 9


Floating Data Type
float (4 bytes)
double (8 bytes)

Textual Data Type


char (2 bytes)
Logical
boolean (1 bit) (true/false)

Input & Output

Output in Java Syntax:


System.out.println("Hello World");

Input in Java Syntax:


// import library
import java.util.Scanner;
// Creating scanner object
Scanner ip = new Scanner(System.in); //system.in represents that the input is given via keyboard
Taking input from user
int ipFrmUser = ip.nextInt(); // Integer Input
double ipDbUser = ip.nextDouble(); // Double Input

Java Variable Type Conversion & Type Casting

A variable of one type can receive the value of another


type. Here there are 2 cases.
Case 1: A variable of smaller capacity is assigned to
another variable of bigger capacity.

This process is Automatic, and non-explicit is known


as type conversion

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 10


Case 2: Variable of larger capacity is be assigned to another variable of smaller capacity.

In such cases, you have to explicitly specify the type cast operator. This process is known
as type casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule is
enforced by the compiler, it makes the programmer aware that the conversion he is about to
do may cause some loss in data and prevents accidental losses.

Example: To Understand Type Casting

class Demo {
public static void main(String args[]) {
byte x;
int a = 270;
double b = 128.128;
System.out.println("int converted to byte");
x = (byte) a;
System.out.println("a and x " + a + " " + x);
System.out.println("double converted to int");
a = (int) b;
System.out.println("b and a " + b + " " + a);
System.out.println("\ndouble converted to byte");
x = (byte)b;
System.out.println("b and x " + b + " " + x);
}
}

Output:
int converted to byte
a and x 270 14
double converted to int
b and a 128.128 128

double converted to byte


b and x 128.128 -128

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 11


Exercises
Question 1: (JAVA Environment Installation & Error Messages)
Set up a Java development environment. In the main ( ) method of your program try to
compile the following invalid Java code snippets. Record the error messages you receive.
What do you think each error message indicates?

System.out.printn(“Hello World”)

System.out.printn(Hello World)

System.out.println”Hello World”;

println(“Hello World);

To generate one final error message, remove one of the brackets from the end of your
program. Now what message do you receive?

Question: 2 (Input)
Write a program that takes user input and displays it.

Question: 3 (Basic Arithmetic Operation)


Write a program that takes two double values from the user and performs basic arithmetic
operations (addition, subtraction, multiplication, division).

Question: 4 (Type casting)


Perform division using two int variables and store the result in float variable and print the
results.

Question: 5 Can you cast string into int?

Question: 6 Why JAVA when there are other OOP languages?

DEPARTMENT OF COMPUTER SCIENCE, SUKKUR IBA UNIVERSITY 12

You might also like