c02 JSE Intro Syntax Arrays OOP Jlib DevOps Docker
c02 JSE Intro Syntax Arrays OOP Jlib DevOps Docker
Java SE – Programming
presentation
Java Programming – Software App Development
Assoc. Prof. Cristian Toma Ph.D.
1 2 3
Java Standard
Arrays
Java OOP
analogy with
C++ (cont.)
Advanced SW
Eng. & DevOps
Topics
1
Java Standard Edition – Uni- & Multi- dimensional Arrays
Java Arrays
1.1 Java Arrays – defining and usage
Opening Problem
ASSIGNMENT 03 - Read one hundred numbers, compute their
average, and find out how many numbers are above the average.
double[] myList = new double[10];
myList[7] 45.45
myList[8] 99.993
myList[9] 11123
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Declaring Array Variables
• datatype[] arrayRefVar;
Example: double[] myList;
• datatype arrayRefVar[]; // This style is allowed, but not preferred
Example: double myList[];
Creating Arrays
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Printing arrays
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Random shuffling
for (i nt i = 0; i < myList .length ; i++) { m y List
// G enerat e an in dex j r andomly i [0]
int index = ( int) (Math.r andom() [1 ]
* myList .length );
. sw a p
// S wap my List[i] with m yList[j ] .
doub le tem p = myL ist[i]; .
myLi st[i] = myLis t[index ]; [in de x]
myLi st[ind ex] = t emp;
A ran d om in de x
}
Shifting Elements
double temp = myList[0]; // Retain the first element
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Enhanced for Loop (for-each loop)
JDK 1.5 introduced a new for loop that enables you to traverse the complete array
sequentially without using an index variable. For example, the following code
displays all elements in the array myList:
You still have to use an index variable if you wish to traverse the array in a
different order or change the elements in the array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Copying Arrays
Often, in a program, you need to duplicate an array or a part of an
array. In such cases you could attempt to use the assignment
statement (=), as follows:
list2 = list1;
Before the assignment After the assignment
list2 = list1; list2 = list1;
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
Example:
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Passing Arrays to Methods
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
Anonymous array
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – defining and usage
Pass By Value
Java uses pass by value to pass arguments to a method. There
are important differences between passing a value of variables
of primitive data types and passing arrays.
return result;
} 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/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Declare result and create array
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 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/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 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/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (= 0) is less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 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/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) { Assign list[0] to result[5]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.5 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 1 and j = 4
public static int[] reverse(int[] list) { Assign list[1] to result[4]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 2 and
public static int[] reverse(int[] list) { j becomes 3
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=2) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 2 and j = 3
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 3 and
public static int[] reverse(int[] list) { j becomes 2
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=3) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 3 and j = 2
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 4 and
public static int[] reverse(int[] list) { j becomes 1
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=4) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 4 and j = 1
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 5 and
public static int[] reverse(int[] list) { j becomes 0
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=5) is still less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 5 and j = 0
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
After this, i becomes 6 and
public static int[] reverse(int[] list) { j becomes -1
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (=6) < 6 is false. So exit the
public static int[] reverse(int[] list) { loop.
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Trace the reverse Method
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Return result
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
list2
result 6 5 4 3 2 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
Searching Arrays
Searching is the process of looking for a specific element in an array;
for example, discovering whether a certain score is included in a list of
scores. Searching is a common task in computer programming. There
are many algorithms and data structures devoted to searching. In this
section, two commonly used approaches are discussed, linear search
and binary search.
public class LinearSearch {
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i]) [0] [1] [2] …
return i; list
return -1;
} key Compare key with list[i] for i = 0, 1, …
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
Linear Search
The linear search approach compares the key
element, key, sequentially with each element
in the array list. The method continues to do
so until the key matches an element in the list
or the list is exhausted without a match being
found. If a match is made, the linear search
returns the index of the element in the array
that matches the key. If no match is found, the
search returns -1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
Linear Search Animation
Key List
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
From Idea to Solution – Linear Search
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i])
return i;
return -1;
}
Binary Search
For binary search to work, the elements in the
array must already be ordered (SORTED).
Without loss of generality, assume that the array
is in ascending order.
e.g., 2 4 7 10 11 45 50 59 60 66 69 70 79
Binary Search
Key List
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
8 1 2 3 4 6 7 8 9
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
key < 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
key > 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key < 66 list 59 60 66 69 70 79
[7] [8]
key < 59 list 59 60
low high
-insertion point - 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
return -1 - low;
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
The Arrays.binarySearch Method
Since binary search is frequently used in programming, Java provides several
overloaded binarySearch methods for searching a key in an array of int, double,
char, short, long, and float in the java.util.Arrays class. For example, the following
code searches the keys in an array of numbers and an array of characters.
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
System.out.println("Index is " +
java.util.Arrays.binarySearch(list, 11));
Return is 4
char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'};
System.out.println("Index is " +
java.util.Arrays.binarySearch(chars, 't'));
Return is –4 (insertion point is
3, so return is -3-1)
For the binarySearch method to work, the array must be pre-sorted in increasing
order.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays
Sorting Arrays
Sorting, like searching, is also a common task in
computer programming. Many different algorithms
have been developed for sorting. Mainly are used
two simple, intuitive sorting algorithms: selection
sort and insertion sort.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort
Selection sort finds the largest number in the list and places it last. It then finds the largest number
remaining and places it next to last, and so on until the list contains only a single number. Figure
shows how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort.
swap
...
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Selection Sort – Wrap it in a Method
/** The method for sorting the numbers */
public static void selectionSort(double[] list) {
for (int i = 0; i < list.length; i++) {
// Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin; Invoke it
}
} selectionSort(yourList)
}
60
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.1 Java Arrays – Sort
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/06slide.ppt
1.2 Java Arrays – Multidimensional Arrays
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays
x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4
x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays
Ragged Arrays
Each row in a two-dimensional array is itself an array. So,
the rows can have different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
matrix.length is 5
{3, 4, 5}, matrix[0].length is 5
matrix[1].length is 4
{4, 5}, matrix[2].length is 3
{5} matrix[3].length is 2
matrix[4].length is 1
};
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807: http://akyokus.com/courses/Fall2011/ise204/Lectures/Liang8e/07slide.ppt
1.2 Java Arrays – Multidimensional Arrays
Ragged Arrays
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 2 3 4 5
{3, 4, 5},
{4, 5}, 3 4 5
{5}
}; 4 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/07slide.ppt
Section Conclusion
Fact: Java is natural
In few samples it is simple to remember:
selections, loops, methods, arrays – it is like
in C/C++ syntax.
2
Java OOP
What is a class?
How can be solved in Java multiple dependencies or inclusions of the same class in the compilation
phase? How were solved these problems in C/C++?
How should be created a Java library and how should be used – command line vs. IDE?
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
• As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever
modules we want. So it is very easy to scale down the Java Application to Small devices.
• Ease of Testing and Maintainability.
• Supports better Performance.
• As public is not just public, it supports very Strong Encapsulation. (Don’t worry its a big
concept. we will explore it with some useful examples soon).
• We cannot access Internal Non-Critical APIs anymore.
• Modules can hide unwanted and internal details very safely, we can get better Security.
• Application is too small because we can use only what ever modules we want.
• Its easy to support Less Coupling between components.
• Its easy to support Single Responsibility Principle (SRP).
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Compare JDK 8 and JDK 9
We know what a JDK software contains. After installing JDK 8 software, we can see couple of
directories like bin, jre, lib etc in Java Home folder. However Oracle Corp / OpenJDK has
changed this folder structure a bit differently as shown below.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules - Compare JDK 8 and JDK 9 Directory Structure:
3.1 JDK 9 and 11+ Modules
Java 9 Modules – SDK Distro:
In a Java 8 or earlier applications, Top level component a Package. It groups a set related of types into a group. It also contains a set of resources.
Java 9 Applications does not have much difference with this. It just introduced a new component called “Module”, which is used to group a set of related
Packages into a group. And one more new component that Module Descriptor (“module-info.java”). That’s it.
Like Java 8 applications have Packages as a Top level components, Java 9 applications have Module as Top Level components.
NOTE: -Each Java 9 Module have one and only one Module and one Module Descriptor. Unlike Java 8 Packages, We cannot create multiple modules into a
single Module. In brief we can say a Java 9 Module contains the following main components:
• One Module + Module Name
• Module Descriptor
• Set of Packages
• Set of Types and Resources
Here Resources may be module-info.java (Module Descriptor) or any other properties or XML.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
The two main goals of Java 9 Module System:
• Reliable Configuration
In Java 9 Module System, a Module is a First class citizen.
• Strong Encapsulation
In Simple Terms, Java has these many First class citizens:
• In OOP (Object Oriented Programming) = Object
• In FP (Functional Programming) = Function or Lambda
Java 9 Module Basics • In Module System (Modular Programming) = Module
We should remember the following important points about Java 9 Module:
• Each module has a unique Name.
• Each module has some description in a source file.
• A Module description is expressed in a source file called “module-info.java”.
• As “module-info.java” file describes a Module, it is also known as “Module Descriptor”.
• A Module Descriptor is a Java file. It is not an XML, Text file or Properties file.
• By convention, we should use same name “module-info.java” for Module Descriptor.
• By convention, Module Descriptor file is placed in the top level directory of a Module.
• Each Module can have any number of Packages and Types.
• As of now, year 2017, JDK 9 have 97 modules.
• We can create our own modules.
• One Module can dependent on any number of modules.
• Each Module should have one and only one Module Descriptor (“module-info.java”).
Using Java SE 9, we develop Modules that means we can do Modular Programming in Java.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 JDK 9 and 11+ Modules
Java 9 Modules:
Follow these steps one by one to develop and test the “HelloWorld” Module.
• Create Module name folder, for example “com.hello”.
• Create Module Packages, for example “com.hello”.
• Create our Java component, for example “HelloWorld.java”.
• Create Module Descriptor, for example “module-info.java”.
• Define Module Description in Module Descriptor, for example “exports
com.hello;” of “module-info.java” in “com.hello” module.
• Create Module Jars if required.
• Test the modules.
• These steps are common for almost all modules development.
Copyright: https://www.journaldev.com/13106/java-9-modules
3.1 GraalVM
GraalVM is an extension of the Java virtual machine to support more languages and execution
modes. The Graal project includes a new high performance Java compiler, itself called Graal, which
can be used in a just-in-time configuration on the HotSpot VM, or in an ahead-of-time configuration on
Copyright: https://www.graalvm.org/ the SubstrateVM.
3.1 GraalVM
Copyright: https://www.graalvm.org/
3.1 GraalVM
Copyright: https://www.graalvm.org/
3.2 Build Automation: Apache ANT
Optional steps to build the lectures with Gradle – all lectures are independent by any
Build
Automation system (e.g. Gradle, MVN, ANT, etc.) and the labs are projects in Eclipse IDE:
Apache ANT is a Java based build tool from Apache Software Foundation. Apache Ant's
build files are written in XML and they take advantage of being open standard, portable
and easy to understand.
Features:
• Ant is the most complete Java build and deployment tool available.
• Ant is platform neutral and can handle platform specific properties such as file
separators.
• Ant can be used to perform platform specific tasks such as modifying the modified
time of a file using 'touch' command.
• Ant scripts are written using plain XML. If you are already familiar with XML, you can
learn Ant pretty quickly.
• Ant is good at automating complicated repetitive tasks.
• Ant comes with a big list of predefined tasks.
• Ant provides an interface to develop custom tasks.
• Ant can be easily invoked from the command line and it can integrate with free and
commercial IDEs. Copyright: https://www.tutorialspoint.com/ant/ant_introduction.htm
3.2 Build Automation: Apache ANT
ant compile
ant jar
ant run Copyright: https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
3.2 Build Automation: Apache MVN - Maven
Optional steps to build the lectures with Gradle – all lectures are independent by any Build
Automation system (e.g. Gradle, MVN, ANT, etc.) and the labs are projects in Eclipse IDE:
Apache Maven (MVN) is a software project management and comprehension tool. Based on
the concept of a project object model (POM), Maven can manage a project's build, reporting
and documentation from a central piece of information. Using maven we can build and
manage any Java based project.
Features:
• Maven uses Convention over Configuration, which means developers are not required to
create build process themselves. Developers do not have to mention each and every
configuration detail. Maven provides sensible default behavior for projects. When a
Maven project is created, Maven creates default project structure. Developer is only
required to place files accordingly and he/she need not to define any configuration in
pom.xml.
• Simple project setup that follows best practices.
• Consistent usage across all projects.
• Dependency management including automatic updating.
Copyright: https://www.tutorialspoint.com/maven/maven_overview.htm
3.2 Build Automation: Apache MVN - Maven
Apache Maven (MVN) Features:
• A large and growing repository of libraries.
• Extensible, with the ability to easily write plugins in Java or scripting languages.
• Instant access to new features with little or no extra configuration.
• Model-based builds − Maven is able to build any number of projects into predefined
output types such as jar, war, metadata.
• Coherent site of project information − Using the same metadata as per the build process,
maven is able to generate a website and a PDF including complete documentation.
• Release management and distribution publication − Without additional configuration,
maven will integrate with your source control system such as CVS and manages the
release of a project.
• Backward Compatibility − You can easily port the multiple modules of a project into
Maven 3 from older versions of Maven. It can support the older versions also.
• Automatic parent versioning − No need to specify the parent in the sub module for
maintenance.
• Parallel builds − It analyzes the project dependency graph and enables you to build
schedule modules in parallel. Using this, you can achieve the performance improvements
of 20-50%.
• Better Error and Integrity Reporting − Maven improved error reporting, and it provides
you with a link to the Maven wiki page where you will get full description of the error.
Copyright: https://www.tutorialspoint.com/maven/maven_overview.htm | https://www.tutorialspoint.com/maven/maven_creating_project.htm
3.2 Build Automation: Apache MVN - Maven
Features:
• It supports Maven and Ivy (ANT) repositories for retrieving project dependencies,
allowing reuse of the artifacts of existing build systems.
https://gradle.org/install/
https://plugins.gradle.org/
https://github.com/jabedhasan21/java-hello-world-with-gradle/blob/master/README.md
https://www.tutorialspoint.com/gradle/index.htm
3.3.1. Gradle Intro and Basics
Projects and tasks in Gradle:
• A project using Gradle describes its build via a build.gradle file. build.gradle file is located in the
root folder of the project. build.gradle file is based on a Domain Specific Language (DSL) written
in Groovy. task hello {
doLast {
println 'Hello Gradle'
• build.gradle build file defines a project and its tasks. }
println 'Project name: ' + rootProject.name
• Gradle daemon: avoid starting the Java virtual machine for every build. Usage:
org.gradle.daemon=true in gradle.properties in the ${HOME}/.gradle; Executing
gradle with the --daemon parameter on the command line or gradle --stop to stop it.
• Gradle ships with a number of plug-ins (java), and custom plug-ins can be developed.
• Adding a plug-in in build.gradle file: apply plugin: 'pluginname' (Eg. apply plugin:
‘java’).
build.gradle
repositories {
jcenter()
}
dependencies {
compile fileTree(dir: 'libs',
include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
3.3.5. Gradle tasks
• Default Gradle tasks: tasks for introspection of Gradle itself (eg. task tasks).
• Custom Gradle tasks: tasks created by developers (eg. task hello). Running the gradle
tasks task, the hello task will be listed under Other tasks. Tasks without a group are
considered as private tasks. Groups can be applied with the group property and a
description can be applied by using the description property .
task hello {
group 'greetings'
description 'The hello task greets Gradle by
saying "Hello Gradle" and the root project name'
doLast {
println 'Hello Gradle'
println 'Project name: ' + rootProject.name
}
}
3.3.6. Building Java projects
• Java plug-in: provides tasks to compile Java source code, run unit tests, create Javadoc and
create a JAR file.
sourceSets {
• Default project layout: main {
java {
- Java source code: src/main/java. srcDir 'src'
}
- Java tests: src/test/java. }
test {
• Different project structure: sourceSets. java {
srcDir 'test'
}
}
}
export JAVA_HOME=/opt/software/java/jdks/jdk1.8.0_161
export CLASSPATH=.:$JAVA_HOME/jre/lib
export CATALINA_HOME=/opt/software/apache-tomcat-9.0.4
export
PATH=.:$JAVA_HOME/bin:$CATALINA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/
bin:/sbin:/bin:/usr/games
cd $CATALINA_HOME
bin/startup.sh
http://127.0.0.1:8080
http://127.0.0.1:8080/jenkins/
bin/shutdown.sh
3.4 Jenkins/Hudson – DevOps Build Automation
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers
Docker image:
https://hub.docker.com | https://hub.docker.com/r/critoma/ubuntu-java-node-py-dev/
Install Docker in Linux (similar steps used for Windows):
https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#install-from-a-package
https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/
sudo dpkg -i /path/to/package.deb
sudo docker run hello-world
docker search critoma
docker pull critoma/ubuntu-java-node-py-dev
docker run -it critoma/ubuntu-java-node-py-dev
Resources:
https://www.tutorialspoint.com/docker/docker_architecture.htm
https://www.tutorialspoint.com/docker/docker_containers_and_shells.htm
https://docs.docker.com/install/linux/docker-ce/ubuntu/
https://docs.docker.com/engine/installation/linux/linux-postinstall/
https://docs.docker.com/get-started/
https://docs.docker.com/get-started/part2/
https://docs.docker.com/get-started/part6/
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes#purging-all-unused-or-dangling-images,-
containers,-volumes,-and-networks
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
https://github.com/fgrehm/docker-eclipse
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
https://stackoverflow.com/questions/19234831/where-are-docker-images-stored-on-the-host-machine
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers
https://hub.docker.com/r/critoma/ubuntu-java-node-py-dev/
3.5 Deployment: Std. OS vs. VMs vs. Docker Containers