INTE 201 Computer Programming 3
INTE 201 Computer Programming 3
73
a. Introduction
73
b. Learning Objectives/Outcomes
73
c. Presentation/Discussion of the Lesson
81
d. Activity
82
e. Rubrics of Activity
a. Introduction 83
b. Learning Objectives/Outcomes 83
c. Presentation/Discussion of the Lesson 83
d. Activity 87
e. Rubrics of Activity 88
Reference List 89
Appendices
8
COURSE SYLLABUS
UNIT I
Lesson 1
Introduction to Java Programming
INTRODUCTION
Java is a computer programming language. It enables programmers to write computer
instructions using English-based commands instead of having to write in numeric codes. It’s
known as a high-level language because it can be read and written easily by humans.
Like English, Java has a set of rules that determine how the instructions are written.
These rules are known as its syntax. Once a program has been written, the high-level
instructions are translated into numeric codes that computers can understand and execute.
LEARNING OBJECTIVES
1. Give a brief history of the Java Programming language
2. Discuss the key principles of Java
3. Discuss the different Java Technology Product Group
LECTURE DISCUSSION
Introduction to Java
Java is an Object-Oriented programming language developed by James Gosling in the early
1990s. The team initiated this project to develop a language for digital devices such as set-top
boxes, television, etc. Originally C++ was considered to be used in the project but the idea was
9
rejected for several reasons (For instance C++ required more memory). Gosling endeavoured to
alter and expand C++ however before long surrendered that for making another stage
called Green. James Gosling and his team called their project “Greentalk” and its file
extension was .gt and later became to known as “OAK”.
The name Oak was used by James Gosling after an oak tree that remained outside his office.
Also, Oak is an image of solidarity and picked as a national tree of numerous nations like the
U.S.A., France, Germany, Romania, etc. But they had to later rename it as “JAVA” as it was
already a trademark by Oak Technologies.
Gosling and his team did a brainstorm session and after the session, they came up with several
names such as JAVA, DNA, SILK, RUBY, etc. Java name was decided after much discussion
since it was so unique. The name Java originates from a sort of espresso bean, Java. Gosling
came up with this name while having a coffee near his office.
Java was created on the principles like Robust, Portable, Platform Independent, High
Performance, Multithread, etc. and was called one of the Ten Best Products of 1995 by
the TIME MAGAZINE.
• Ease of Use: The fundamentals of Java came from a programming language called
C++. Although C++ is a powerful language, it is complex in its syntax and inadequate for
some of Java's requirements. Java built on and improved the ideas of C++ to provide a
programming language that was powerful and simple to use.
• Reliability: Java needed to reduce the likelihood of fatal errors from programmer
mistakes. With this in mind, object-oriented programming was introduced. When data and
its manipulation were packaged together in one place, Java was robust.
• Security: Because Java was originally targeting mobile devices that would be
exchanging data over networks, it was built to include a high level of security. Java is
probably the most secure programming language to date.
• Platform Independence: Programs need to work regardless of the machines they're
being executed on. Java was written to be a portable and cross-platform language that
doesn't care about the operating system, hardware, or devices that it's running on.
What is JVM?
10
JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java
program.
When you run the Java program, Java compiler first compiles your Java code to bytecode.
Then, the JVM translates bytecode into native machine code (set of instructions that a
computer's CPU executes directly).
Java is a platform-independent language. It's because when you write Java code, it's ultimately
written for JVM but not your physical machine (computer). Since, JVM executes the Java byte
code which is platform independent, Java is platform-independent.
What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java class libraries, along
with Java Virtual Machine (JVM), and other components to run applications written in Java
programming. JRE is the superset of JVM.
What is JDK?
JDK (Java Development Kit) is a software development kit to develop applications in Java.
When you download JDK, JRE is also downloaded, and don't need to download it separately. In
addition to JRE, JDK also contains number of development tools (compilers, JavaDoc, Java
Debugger etc).
JDK is the development platform, while JRE is for execution. JVM is the foundation, or the heart
of the Java programming language, and ensures the program's Java source code will be
platform-agnostic. JVM is included in both JDK and JRE—Java programs won't run without it.
Activity:
11
1. One of the most powerful programming languages
2. The first name of Java
3. Considered as the father of Java Programming Language
4. It is an abstract machine that enables your computer to run a Java program.
5. It is a software package that provides Java class libraries, along with Java Virtual
Machine (JVM), and other components to run applications written in Java programming.
JRE is the superset of JVM.
6. It is a software development kit to develop applications in Java.
7. What are the 4 principles of Java Programming?
8. Explain the relationship between JVM, JRE, JDK
Rubrics of Activity
Lesson 2
Basic Elements of Java
INTRODUCTION
A Java program is made up of classes. A class can be thought of as a collection of variables
and subroutines also known as method. A subroutine is a collection of program instructions that
may be made up of the following essential program elements like literals, identifiers,
expressions, operators.
LEARNING OBJECTIVES
1. Describe the Java data types that are used for simple data like numbers and characters
2. Write Java statements to declare variables and define named constants
3. Write assignment statements and expressions containing variables and constants
4. Explain the rules of naming a variable names
12
5. Recognized the use of different java operators like assignment operators, arithmetic
operators
LECTURE DISCUSSION
JAVA VARIABLES AND (PRIMITIVE) DATA TYPES
In this lesson 1, you will learn about variables, how to create them, and different data types that
Java programming language supports for creating variables.
Java Variables
A variable is a location in memory (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier).
Here, speedLimit is a variable of int data type, and is assigned value 80. Meaning,
the speedLimit variable can store integer values. You will learn about Java data types in detail
later in the article.
In the example, we have assigned value to the variable during declaration. However, it's not
mandatory. You can declare variables without assigning the value, and later you can store the
value as you wish. For example,
int speedLimit;
speedLimit = 80;
The value of a variable can be changed in the program, hence the name 'variable'. For example,
Java is a statically-typed language. It means that all variables must be declared before they can
be used.
Also, you cannot change the data type of a variable in Java within the same scope. What is
variable scope? Don't worry about it for now. For now, just remember you cannot do
something like this.
int speedLimit = 80;
... .. ...
float speedLimit;
13
• Variables in Java are case-sensitive.
• A variable's name is a sequence of Unicode letters and digits. It can begin with a
letter, $ or _. However, it's convention to begin a variable name with a letter. Also,
variable name cannot use whitespace in Java.
int speed;
Here, speed is a variable, and the data type of the variable is int . The int data type determines
that the speed variable can only contain integers.In simple terms, a variable's data type
determines the values a variable can store. There are 8 data types predefined in Java
programming language, known as primitive data types.In addition to primitive data types, there
are also referenced data types in Java (you will learn about it in later chapters).
Data Types
boolean
• The boolean data type has two possible values, either true or false .
• Default value: false .
• They are usually used for true/false conditions.
Example:
classBooleanExample{
publicstaticvoid main(String[] args){
14
}
The byte data type can have values from -128 to 127 (8-bit signed two's complement integer).
It's used instead of int or other integer data types to save memory if it's certain that the value of
a variable will be within [-128, 127].
Default value: 0
Example:
classByteExample{
publicstaticvoid main(String[] args){
byte range;
range =124;
System.out.println(range);
short
• The short data type can have values from -32768 to 32767 (16-bit signed two's
complement integer).
• It's used instead of other integer data types to save memory if it's certain that the value
of the variable will be within [-32768, 32767].
• Default value: 0
Example:
classShortExample{
publicstaticvoid main(String[] args){
short temperature;
temperature =-200;
System.out.println(temperature);
}
}
When you run the program, the output will be:
-200
15
int
• The int data type can have values from -231 to 231-1 (32-bit signed two's complement
integer).
• If you are using Java 8 or later, you can use unsigned 32-bit integer with minimum value
of 0 and maximum value of 232-1.
• Default value: 0
Example:
classIntExample{
publicstaticvoid main(String[] args){
long
• The long data type can have values from -263 to 263-1 (64-bit signed two's complement
integer).
• If you are using Java 8 or later, you can use unsigned 64-bit integer with minimum value
of 0 and maximum value of 264-1.
• Default value: 0
•
Example:
classLongExample{
publicstaticvoid main(String[] args){
Notice, the use of L at the end of -42332200000 . This represents that it's an integral literal
of long type. You will learn about integral literals later in this article.
double
• The double data type is a double-precision 64-bit floating point.
• It should never be used for precise values such as currency.
• Default value: 0.0 (0.0d)
Example:
classDoubleExample{
publicstaticvoid main(String[] args){
16
double number =-42.3;
System.out.println(number);
}
}
float
• The float data type is a single-precision 32-bit floating point. Learn more about single
precision and double precision floating point if you are interested.
• It should never be used for precise values such as currency.
• Default value: 0.0 (0.0f)
•
Example:
class FloatExample{
publicstaticvoid main(String[] args){
Notice that, we have used -42.3f instead of -42.3 in the above program. It's because -42.3 is
a double literal. To tell compiler to treat -42.3 as float rather than double , you need to use f or F.
char
• It's a 16-bit Unicode character.
• The minimum value of char data type is '\u0000' (0). The maximum value of char data
type is '\uffff' .
• Default value: '\u0000'
Example:
classCharExample{
publicstaticvoid main(String[] args){
17
System.out.println(letter1);
}
}
When you run the program, the output will be:
9
A
When you print letter1, you will get 9 because letter1 is assigned character '9'.
When you print letter2, you get A because the ASCII value of 'A' is 65. It's because java
compiler treats character as integral type. Learn more about ASCII. Java also provides support
for character strings via java.lang.String class. Here's how you can create a String object in
Java:
myString = "Programming is awesome";
Java String is an important topic which you will learn in detail in later chapters. However, if you
are not a newbie in programming and want to learn it now, visit Java String.
Java literals
To understand literals, let's take an example to assign value to a variable.
Here,
A Literal is the source code representation of a fixed value. Values like 1.5 , 4 , true , '\u0050' that
appears directly in a program without requiring computation are literals.In the above
example, flag is a variable. Since, it's a boolean type variable, it may store either false or true . For
compiler to understand it, it requires computation. However, literals like -5 , 'a' , true represents
fixed value.
Integer Literals
• Integer literals are used to initialize variables of integer data
types byte , short , int and long .
• If an integer literal ends with l or L , it's of type long . Tip: it is better to use L instead of l .
18
Integer literals can be expressed in decimal, hexadecimal and binary number systems.
The numbers starting with prefix 0x represents hexadecimal. Similarly, numbers starting with
prefix 0b represents binary.
// decimal
int decNumber = 34;
// 0x represents hexadecimal
int hexNumber = 0x2F;
// 0b represents binary
Floating-point Literals
• Floating-point literals are used to initialize variables of data type float and double .
• If a floating-point literal ends with f or F , it's of type float. Otherwise, it's of type double. A
double type can optionally end with D or d . However, it's not necessary.
• They can also be expressed in scientific notation using E or e.
classDoubleExample{
publicstaticvoid main(String[] args){
// 3.445*10^2
double myDoubleScientific =3.445e2;
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myDoubleScientific);
}
}
classDoubleExample{
publicstaticvoid main(String[] args){
19
char newLine ='\n';
String myString ="Java 8";
System.out.println(myChar);
System.out.println(newLine);
System.out.println(myString);
}
}
When you run the program, the output will be:
g
Java 8
Java Operators
In this article, you'll learn everything about different types of operators in Java programming
language, their syntax and how to use them with examples.
Operators are special symbols (characters) that carry out operations on operands (variables and
values). For example, + is an operator that performs addition. In Java variables article, you
learned to declare variables and assign values to variables. Now, you will learn to use operators
to manipulate variables.
Assignment Operator
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
The assignment operator assigns the value on its right to the variable on its left. Here, 5 is
assigned to the variable age using = operator.There are other assignment operators too.
However, to keep things simple, we will learn other assignment operators later in this article.
class AssignmentOperator{
public staticvoid main(String[] args){
// Assigning 5 to number1
number1 =5;
System.out.println(number1);
20
5
5
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication etc.
Java Arithmetic Operators
Operator Meaning
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator
class ArithmeticOperator{
public staticvoid main(String[] args){
21
}
}
When you run the program, the output will be:
number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1 % number2 = 2.0
In above example, all operands used are variables. However, it's not necessary at all. Operands
used in arithmetic operators can be literals as well. For example,
Unary Operators
Operator Meaning
+ Unary plus (not necessary to use since numbers are positive without using it)
22
Operator Meaning
System.out.println("+number = "++number);
// number is equal to 5.2 here.
System.out.println("-number = "+-number);
// number is equal to 5.2 here.
System.out.println("!flag = "+!flag);
// flag is still false.
}
}
When you run the program, the output will be:
+number = 5.2
-number = -5.2
number = 6.2
number = 5.2
!flag = true
You can also use ++ and -- operator as both prefix and postfix in Java. The ++ operator
increases value by 1 and -- operator decreases value by 1.
int myInt = 5;
++myInt // myInt becomes 6
myInt++ // myInt becomes 7
--myInt // myInt becomes 6
myInt-- // myInt becomes 5
Simple enough till now. However, there is a crucial difference while using increment and
decrement operator as prefix and postfix. Consider this example,
23
Example 5: Unary Operator
classUnaryOperator{
publicstaticvoid main(String[] args){
System.out.println(number++);
System.out.println(number);
System.out.println(++number);
System.out.println(number);
}
}
When you run the program, the output will be:
5.2
6.2
7.2
7.2
24
Java Equality and Relational Operators
Equality and relational operators are used in decision making and loops (which will be
discussed later). For now, check this simple example.
Here, we have used > operator to check if number1 is greater than number2 or not.
Since, number2 is greater than number1, the expression number1 > number2 is evaluated
to false .
Hence, the block of code inside else is executed and the block of code inside if is skipped.
If you didn't understand the above code, don't worry. You will learn it in detail in Java
if...else article.
For now, just remember that the equality and relational operators compares two operands and
is evaluated to either true or false .
In addition to relational operators, there is also a type comparison operator instance of which
compares an object to a specified type. For example,
Instance of Operator
Here's an example of instanceof operator.
class instanceofOperator {
publicstaticvoid main(String[] args){
25
}
}
When you run the program, the output will be true . It's because test is the instance
of String class.
You will learn more about instanceof operator works once you understand Java Classes and
Objects.
Logical Operators
The logical operators || (conditional-OR) and && (conditional-AND) operates on boolean
expressions. Here's how they work.
Operator Description
26
Ternary Operator
The conditional operator or ternary operator ?: is shorthand for if-then-else statement. The
syntax of conditional operator is:
Operator Description
~ Bitwise Complement
^ Bitwise exclusive OR
| Bitwise inclusive OR
27
These operators are not commonly used. Visit this page to learn more about bitwise and bit shift
operators.
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x/5
^= x ^= 5 x=x^5
|= x |= 5 x=x|5
Now you know about Java operators, it's time to learn precedence of Java operators; the order
in which the operators in an expression are evaluated when two operators share a common
operand.
Activity:
Instruction: On the space provided at the right column, write VALID if the given literal is valid.
Otherwise, write the valid form.
1. 1.525 6. 2100.00
2. 60miles/hour 7. 0.07
3. 80% 9. -20
28