08Chapters 1 - 8_Java Basics Revision
08Chapters 1 - 8_Java Basics Revision
June 2022
www.adu.edu.ae
Objectives – I
To describe objects and classes, and use classes to model
objects
To use UML graphical notation to describe classes and objects.
• A Simple Java Program.
– Interpreter / Compiler
– Programming Errors
• Elementary Programming.
– Trace a program execution
– Reading Input From Console
– Variables and Data types
• Selection
– Relational Operator
– Logical Operator
– Conditional Statement
2 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
Objectives – II
• Loops
– For Loop
– While loop
– Do-while loop
• Methods.
– Defining Methods / Parameters
– Method Invocations
– Method Overload
– Scope of variables
• Arrays .
– Single Dimensional Array
– Multidimensional Array
Object Oriented
Programming In Java
Basics
4 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
OO Programming Concepts
Object-oriented programming (OOP) involves
programming using objects.
• An object represents an entity in the real world that can be
distinctly identified.
For example, a student, a desk, a circle, a button, and even a loan
can all be viewed as objects.
Data Fields:
radius is _______
Methods:
getArea
A compiler:
• translates the entire source code into a machine-code file (all at once), and
• the machine-code file is then executed, as shown in the following figure.
• Eclipse
• NetBeans (To Be Used in the Labs)
• Let’s search on internet “What is IDE?”
and discuss.
Start the NetBeans to see the components.
The NetBeans practice session will be held in the lab!
Understanding Basic
Components Of A Java
Program
15 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
A Simple Java Program
Listing 1.1
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
• Syntax Errors
– Detected by the compiler
• Runtime Errors
– Causes the program to abort
• Logic Errors
– Produces incorrect result
• Constants:
– Capitalize all letters in constants,
and use underscores to connect
words. For example, the constant
PI and MAX_VALUE
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
VARIABLES
• int x = 1;
• double d = 1.4;
Positive range:
4.9E-324 to 1.7976931348623157E+308
Method Description
+ Addition 34 + 1 35
% Remainder 20 % 3 2
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
CONDITIONAL
STATEMENTS
49 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
Logical Operators
|| or logical disjunction
true false !(age > 18) is false, because (age > 18) is true.
false false false (age <= 18) && (weight < 140) is false, because (age >
false
true false (age > 18) && (weight > 140) is false, because (weight
false true true (age > 34) || (weight <= 140) is true, because (age > 34)
true
true false (age > 14) || (weight >= 150) is false, because
false false false (age > 34) ^ (weight > 140) is true, because (age > 34) is false
false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is false
true
true false (age > 14) ^ (weight > 140) is true, because (age > 14) is
false
true true
54 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
One-way if Statements
if (radius >= 0) {
area = radius * radius * PI;
if (boolean-expression) { System.out.println("The area"
statement(s); + " for the circle of radius "
}
+ radius + " is " + area);
}
if i > 0 { if (i > 0) {
System.out.println("i is positive"); System.out.println("i is positive");
} }
(a) Wrong (b) Correct
if (i > 0) { if (i > 0)
System.out.println("i is positive"); Equivalent System.out.println("i is positive");
}
(a) (b)
(a) (b)
REPETITIVE STATEMENTS
System.out.println("Welc
ome to Java!");
count++;
}
do {
// Loop body;
Statement(s);
} while (loop-continuation-
condition);
67 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
for Loops
for (initial-action; loop- int i;
continuation-condition;
action-after-each-iteration) { for (i = 0; i < 100; i++) {
// loop body; System.out.println(
Statement(s); "Welcome to Java!");
} }
System.out.println(
"Welcome to Java!");
}
Logic
Error
A for loop in (a) in the following figure can generally be converted into the
following while loop in (b) except in certain special cases
METHODS
return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
Method Header
Black Box
Method body
Example:
double[] myList;
Example:
myList = new double[10];
arrayRefVar.length
For example,
myList.length returns 10
arrayRefVar[index];
111 Dr M Nasir Mumtaz Bhutta www.adu.ac.ae
Using Indexed Variables
After an array is created, an indexed
variable can be used in the same way
as a regular variable. For example, the
following code adds the value in
myList[0] and myList[1] to myList[2].
values[i] = i + values[i-1]; 2 0
} 3 0
values[0] = values[1] + 4 0
values[4];
}
}
»Questions ?