c01 JSE Intro Synthax OOP MultiParadigm VMGit
c01 JSE Intro Synthax OOP MultiParadigm VMGit
Java SE – Programming
presentation
Java Programming – Software App Development
Assoc. Prof. Cristian Toma Ph.D.
1 2 3
JVM and Java
Fundamentals
Intro
Java OOP
analogy with
C++
Multi-paradigm
programming
intro & Git
sources from
Linux VM
1
JVM – Java Virtual Machine, Java SE – loops, methods, array
What is the difference of stack based vs. registered based JVM and what are
features of JIT compiler?
Hello World sample program in Oracle VM Box – MacOS, Linux, Windows and
RaspberryPi
Command line compiling with JDK 9.0 | 8.0 – all lectures samples are command line
based – and Eclipse OXYGEN projects in Linux Ubuntu 16 LTS. The sources are
full in Git and alternatively is a Docker image for the lectures in the command line.
Linux Ubuntu 16 VM download:
http://acs.ase.ro/java ~ 15 GB HDD, 2 CPU cores, 4 GB RAM / use Oracle VM
Box File->Import OVA file - All seminars will be in /home/stud/javase/labs directory
and the lectures are in /home/stud/javase/lectures : user=stud / pass=stud
1.1 Java Object Oriented Programming Summary
What is a class?
http://support.novell.com/techcenter/articles/ana19970701.html
http://www.media-art-online.org/java/help/how-it-works.html
1.1 Java Virtual Machine Summary
Result
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson
Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/01slide.ppt If runtime errors or incorrect result
1.1 Java Trace a Program Execution
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/01slide.ppt
1.1 Java Trace a Program Execution
Execute statement
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/01slide.ppt
1.1 Java Trace a Program Execution
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/01slide.ppt
JSE – Java Standard Edition
1.1 Java SE JDK and JRE
JDK = JRE + Development / Compiling /
Debugging tools
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Comparison Operators
Operator Name
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
if (radius >= 0) {
One-way if Statements area = radius * radius * PI;
System.out.println("The area"
if (boolean-expression) { + " for the circle of radius "
statement(s); + radius + " is " + area);
}
}
false false
Boolean (radius >= 0)
Expression
true true
(A) (B)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Note
if i > 0 { if (i > 0) {
System .out.pr intln( " i is po sitive "); Sys tem.out .printl n( "i is positi ve");
} }
(a) W ron g (b) C orre ct
i f (i > 0) { if (i > 0)
System .out.pr intln(" i is po sitive" ); Equ ivalent Syste m.out. println ("i is positiv e");
}
(a) (b)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
true false
Boolean
Expression
Statement(s) for the true case Statement(s) for the false case
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
if...else Example
if (radius >= 0) {
area = radius * radius * 3.14159;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Note
The else clause matches the most recent if clause in the
same block.
int i = 1; int i = 1;
int j = 2; int j = 2;
int k = 3; int k = 3;
Equivalent
if (i > j) if (i > j)
if (i > k) if (i > k)
System.out.println("A"); System.out.println("A");
else else
System.out.println("B"); System.out.println("B");
(a) (b)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Note, cont.
Nothing is printed from the preceding statement. To
force the else clause to match the first if clause, you
must add a pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
This statement prints B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Common Errors
Adding a semicolon at the end of an if clause is a common
mistake.
Wrong
if (radius >= 0);
{
area = radius*radius*PI;
System.out.println(
"The area for the circle of radius " +
radius + " is " + area);
}
This mistake is hard to find, because it is not a compilation error
or a runtime error, it is a logic error.
This error often occurs when you use the next-line block style.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
TIP
if (number % 2 == 0) Equivalent
even = true; boolean even
else = number % 2 == 0;
even = false;
(a) (b)
CAUTION
Equivalent if (even)
if (even == true)
System.out.println( System.out.println(
"It is even."); "It is even.");
(a) (b)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
if (status == 0) { // Compute tax for single filers
import java.util.Scanner; if (income <= 8350)
tax = income * 0.10;
else if (income <= 33950)
public class ComputeTax {
tax = 8350 * 0.10 + (income - 8350) * 0.15;
public static void main(String[] else if (income <= 82250)
args) { tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
// Create a Scanner (income - 33950) * 0.25;
Scanner input = new else if (income <= 171550)
Scanner(System.in); tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
// Prompt the user to enter filing (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
status else if (income <= 372950)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
System.out.print( (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
"(0-single filer, 1-married (income - 171550) * 0.35;
jointly,\n" + else
"2-married separately, 3-head of tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
household)\n" + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
"Enter the filing status: "); (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
}
int status = input.nextInt();
else if (status == 1) { // Compute tax for married file jointly
// Prompt the user to enter // Left as exercise
taxable income }
System.out.print("Enter the else if (status == 2) { // Compute tax for married separately
taxable income: "); // Left as exercise
double income = }
input.nextDouble(); else if (status == 3) { // Compute tax for head of household
// Compute tax // Left as exercise
}
double tax = 0; else {
System.out.println("Error: invalid status");
System.exit(0);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Logical Operators
Operator Name
! not
&& and
|| or
^ exclusive or
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
true false !(age > 18) is false, because (age > 18) is true.
false true !(gender != 'F') is true, because (grade != 'F') is false.
p !p Example
true false !(1 > 2) is true, because (1 > 2) is false.
false true !(1 > 0) is false, because (1 > 0) is true.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
false false false (age > 18) && (gender == 'F') is true, because (age
false true false > 18) and (gender == 'F') are both true.
true false false (age > 18) && (gender != 'F') is false, because
(gender != 'F') is false.
true true true
p1 p2 p1 && p2 Example
false false false (3 > 2) && (5 >= 5) is true, because (3 >
false true false 2) and (5 >= 5) are both true.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
false false false (age > 34) || (gender == 'F') is true, because (gender
false true true == 'F') is true.
true false true (age > 34) || (gender == 'M') is false, because (age >
true true true 34) and (gender == 'M') are both false.
p1 p2 p1 || p2 Example
false false false (2 > 3) || (5 > 5) is false, because (2 > 3)
false true true and (5 > 5) are both false.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
false false false (age > 34) ^ (gender == 'F') is true, because (age
false true true > 34) is false but (gender == 'F') is true.
true false true (age > 34) || (gender == 'M') is false, because (age
> 34) and (gender == 'M') are both false.
true true false
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Examples
System.out.println("Is " + number + " divisible by 2 and 3? " +
((number % 2 == 0) && (number % 3 == 0)));
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch Statements
switch (status) {
case 0: compute taxes for single filers;
break;
case 1: compute taxes for married file jointly;
break;
case 2: compute taxes for married file separately;
break;
case 3: compute taxes for head of household;
break;
default: System.out.println("Errors: invalid status");
System.exit(0);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
status is 1
Compute tax for married file jointly break
status is 2
Compute tax for married file separatly break
status is 3
Compute tax for head of household break
default
Default actions
Next Statement
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
ch is 'a':
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
case 'b': System.out.println(ch);
case 'c': System.out.println(ch);
}
Next statement;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'b': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Next statement;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Conditional Operator
if (x > 0)
y = 1;
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
(boolean-expression) ? expression1 : expression2
Ternary operator
Binary operator
Unary operator
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Conditional Operator
(boolean-expression) ? exp1 : exp2
if (num % 2 == 0)
System.out.println(num + “is even”);
else
System.out.println(num + “is odd”);
System.out.println(
(num % 2 == 0)? num + “is even” :
num + “is odd”);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Formatting Output
Use the printf statement.
System.out.printf(format, items);
Where format is a string that may consist of substrings and
format specifiers. A format specifier specifies how an item
should be displayed. An item may be a numeric value,
character, boolean value, or a string. Each specifier begins
with a percent sign.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Frequently-Used Specifiers
Specifier Output Example
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.460000
%e a number in standard scientific notation 4.556000e+01
%s a string "Java is cool“
%02x a hex value 2F
int count = 5;
items
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
Operator Precedence
§ var++, var--
§ +, - (Unary plus and minus), ++var,--var
§ (type) Casting
§ ! (Not)
§ *, /, % (Multiplication, division, and remainder)
§ +, - (Binary addition and subtraction)
§ <, <=, >, >= (Comparison)
§ ==, !=; (Equality)
§ ^ (Exclusive OR)
§ && (Conditional AND) Short-circuit AND
§ || (Conditional OR) Short-circuit OR
§ =, +=, -=, *=, /=, %= (Assignment operator)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Operator Associativity
When two operators with the same precedence
are evaluated, the associativity of the operators
determines the order of evaluation. All binary
operators except assignment operators are left-
associative.
a – b + c – d is equivalent to ((a – b) + c) – d
Assignment operators are right-associative.
Therefore, the expression
a = b += c = 5 is equivalent to a = (b += (c = 5))
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.2 Java Selections – Boolean Operators, if-else, switch statements
Example
Applying the operator precedence and associativity rule, the
expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows:
3 + 4 * 4 > 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 > 5 * 7 – 1
(2) multiplication
3 + 16 > 5 * 7 – 1
(3) multiplication
3 + 16 > 35 – 1
(4) addition
19 > 35 – 1
(5) subtraction
19 > 34
(6) greater than
false
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/03slide.ppt
1.3 Java Loops – for, while, … statements
int count = 0;
while (count < 100) {
System.out.println("Welcome to Java");
count++;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Caution
Don’t use floating-point values for equality checking in a loop control. Since
floating-point values are approximations for some values, using them could result in
imprecise counter values and inaccurate results. Consider the following code for
computing 1 + 0.9 + 0.8 + ... + 0.1:
Variable item starts with 1 and is reduced by 0.1 every time the loop body is
executed. The loop should terminate when item becomes 0. However, there is no
guarantee that item will be exactly 0, because the floating-point arithmetic is
approximated. This loop seems OK on the surface, but it is actually an infinite loop.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
do-while Loop
Statement(s)
(loop body)
true Loop
Continuation
Condition?
do {
// Loop body; false
Statement(s);
} while (loop-continuation-condition);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Initial-Action i=0
for Loops
(A) (B)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Note
The initial-action in a for loop can be a list of zero or more
comma-separated expressions. The action-after-each-
iteration in a for loop can be a list of zero or more comma-
separated statements. Therefore, the following two for
loops are correct. They are rarely used in practice,
however.
for (int i = 1; i < 100; System.out.println(i++));
Note
If the loop-continuation-condition in a for loop is omitted,
it is implicitly true. Thus the statement given below in (a),
which is an infinite loop, is correct. Nevertheless, it is
better to use the equivalent loop in (b) to avoid confusion:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.3 Java Loops – for, while, … statements
Caution
Adding a semicolon at the end of the for clause before
the loop body is a common mistake, as shown below:
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:
Recommendations
Use the one that is most intuitive and comfortable for
you. In general, a for loop may be used if the number of
repetitions is known, as, for example, when you need to
print a message 100 times. A while loop may be used if
the number of repetitions is not known, as in the case of
reading the numbers until the input is 0. A do-while loop
can be used to replace a while loop if the loop body has
to be executed before testing the continuation condition.
ASSIGNMENT 02 - Problem: Write a program that uses nested for
loops to print a multiplication table – nested loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807:
http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/04slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Problem
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Problem
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Solution
public static int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
Defining Methods
A method is a collection of statements that are
grouped together to perform an operation.
Define a method Invoke a method
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Calling Methods
pass the value of i
pass the value of j
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
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;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
CAUTION
A return statement is required for a value-returning method. The method
shown below in (a) is logically correct, but it has a compilation error because
the Java compiler thinks it possible that this method does not return any value.
To fix this problem, delete if (n < 0) in (a), so that the compiler will
see a return statement to be reached regardless of how the if
statement is evaluated.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Call Stacks
Space required for Space required for Space required for Space required for
the main method the main method the main method the main method
k: k: k: k: 5 Stack is empty
j: 2 j: 2 j: 2 j: 2
i: 5 i: 5 i: 5 i: 5
(a) The main (b) The max (c) The max method (d) The max method is (e) The main
method is invoked. method is invoked. is being executed. finished and the return method is finished.
value is sent to k.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
}
return result;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
}
return result;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
} Space required for the
main method
k:
public static int max(int num1, int num2) { j: 2
int result; i: 5
return result;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
} Space required for the
main method
k:
public static int max(int num1, int num2) { j: 2
int result; i: 5
return result;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k); num2: 2
} num1: 5
Space required for the
public static int max(int num1, int num2) { main method
int result;
k:
if (num1 > num2) j: 2
result = num1; i: 5
else
result = num2;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i + result:
" and " + j + " is " + k); num2: 2
} num1: 5
Space required for the
public static int max(int num1, int num2) { main method
int result;
k:
if (num1 > num2) j: 2
result = num1; i: 5
else
result = num2;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i + result:
" and " + j + " is " + k); num2: 2
} num1: 5
Space required for the
public static int max(int num1, int num2) { main method
int result;
k:
if (num1 > num2) j: 2
result = num1; i: 5
else
result = num2;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
} Space required for the
main method
k:5
public static int max(int num1, int num2) { j: 2
int result; i: 5
return result;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Passing Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
The main method The swap method The swap method The main method
is invoked is invoked is finished is finished
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Overloading methods - Ambiguous Invocation
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
Method Abstraction
A programmer can think of the method body as a black box that contains the
detailed implementation for the method.
Optional arguments Optional return
for Input value
Method Header
Black Box
Method body
Trigonometric Methods
• sin(double a) Examples:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Exponent Methods
• exp(double a) Examples:
Returns e raised to the power of
a. Math.exp(1) returns 2.71
• log(double a) Math.log(2.71) returns 1.0
Returns the natural logarithm of a. Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
• log10(double a)
Math.pow(3.5, 2.5) returns
Returns the 10-based logarithm of 22.91765
a.
Math.sqrt(4) returns 2.0
• pow(double a, double b) Math.sqrt(10.5) returns 3.24
Returns a raised to the power of
b.
• sqrt(double a)
Returns the square root of a.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
1.4 Java Methods – defining, signature, formal / actual parameters
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/05slide.ppt
Section Conclusion
Fact: Java is natural
In few samples it is simple to remember:
selections, loops, methods – it is like in
C/C++ syntax.
2
Java OOP
What is a class?
Interface – “contract between objects of the class that implements the interface and
another objects of another classes that interact with objects from the
implementation class” – has the following features:
§ static fields
§ Static and non-static methods prototypes – declaration but NOT
implementation
§ The implementation class use the “implements” keyword
§ It is possible to have interface as type – declare objects with type interface
but you must call the constructor methods from a real Java class
2. Summary of Java SE - OOP
Abstract Class – “a class that have at least one method abstract” – has
the following features:
§ At least one abstract method – keyword “abstract”
§ May contain standard static and non-static fully implemented methods
§ You may declare objects from an abstract class but you can NOT
instantiate them, you should use constructor method from a real Java
class
DEMO
Compile in command prompt & Eclipse – Shallow Copy vs. Deep Copy Example.
Section Conclusions
Object Oriented Programming in Java – class,
object, object’s deep vs. shallow copy, interface,
interface as type, abstract class, inheritance,
polymorphism.
Copyright: https://en.m.wikipedia.org/wiki/Programming_paradigm
3.2 JVM Languages
This list of JVM Languages comprises notable computer programming languages that
are used to produce computer software that runs on the Java virtual machine (JVM).
Some of these languages are interpreted by a Java program, and some are compiled
to Java bytecode and JIT-compiled during execution as regular Java programs to
improve performance.
The JVM was initially designed to support only the programming language Java.
However, as time passed, ever more languages were adapted or designed to run on
the Java platform.
JVM based High-profile programming languages
Apart from the Java language, the most common or well-known other JVM languages
are:
• Clojure, a functional Lisp dialect
• *** Apache Groovy, a dynamic programming and scripting language (Jenkins)
• JRuby, an implementation of Ruby
• Jython, an implementation of Python
• *** Kotlin, a statically-typed language from JetBrains, the developers of IntelliJ IDEA
(Android Mobile Apps)
• *** Scala, a statically-typed object-oriented and functional programming language
(Cloud – IaaS / PaaS / CaaS over IaaS, HA/HPC scalable computing, Big Data,
DataMining, Collective Intelligence….)
Copyright: https://en.wikipedia.org/wik i/List_of_JVM_languages
3.2 JVM Languages
JVM implementations of existing languages
Language Java implementations Language Java implementations
Ada JGNAT JIProlog
Prolog
Arden Syntax Arden2ByteCode TuProlog
COBOL Micro Focus Visual COBOL Python Jython
ColdFusion Adobe ColdFusion
R Renjin
Markup Railo
Language (CF Lucee Rexx NetRexx
ML) Open BlueDragon JRuby
Common Lisp Armed Bear Common Lisp Ruby
Mirah
Cypher Neo4j
Bigloo
Rhino Kawa
JavaScript Scheme
Nashorn
SISC
Mercury Mercury (Java grade) JScheme
Oberon Component Pascal
Tcl Jacl
MIDletPascal
Pascal Visual Basic Jabaco
Oxygene
Perl 6 Rakudo Perl 6
PHP Quercus
Copyright: https://en.wikipedia.org/wik i/List_of_JVM_languages
3.3 Repositories: Git from Linux VM
Mandatory steps to get the lectures and the labs: (Optional full Git presentation
@ISM – Cyber-Security Master – www.ism.ase.ro by Cristian Toma & Casian Andrei @ Oracle):
1. Download & install the Linux Ubuntu 16.04 LTS VM OVA appliance from Google drive:
2. In Ubuntu Linux VM download the Git repo with the sources for the lecture & labs:
https://github.com/critoma/javase | http://acs.ase.ro/java
Optional – set your e-mail and Git global username if you want to contribute:
git config --global user.email [email protected]
git config --global user.name "critoma"
Mandatory – clone or pull the latest Git repo updates OR Download-ZIP (web):
git clone https://github.com/critoma/javase.git
git pull
?
Questions & Answers!
What’s
Thanks!Your Message?