Tuesday, May 5, 2015

Bash Shell Learning Notes

Bash: Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.
Lynda online tutorial: http://www.lynda.com/Bash-tutorials/Up-Running-Bash-Scripting/142989-2.html

Push to backend run
bash filename.sh > log.txt 2>&1

Linux command:
pwd: current working directory
ls 
ls directory
ls -l directory: info
man ls: help info
rmdir directory : delete
clear
cp file1 file2: copy 1->2
rm: delete file
cat file: see what's inside
more file: view page by page
head file: first couple line
tail file: last couple line

chmod mode file: change mode
eg: chmod 000 *_015*
#Permissionrwx
7read, write and execute111
6read and write110
5read and execute101
4read only100
3write and execute011
2write only010
1execute only001
0none000


Brace Expansion: {} 
touch file{1,2,3} :create multiple files
touch file_{1...100}: create 100 files 

touch {apple,banana}_{01..03}{a..b} : note no space inside
echo {1..10..2}: 1,3,5,7,9
rm *: delete all
rm -I * : delete all with prompt once

ls -l | wc -l : count files
ls | more : list view page by page

Changing where things go with pipes and redirection
cp -v * ../otherfolder 1>../success.txt 2>../error.txt
copy all files from current folder to another folder, -v: output what is being done; 1 successful copy, 2 error message, & all message

/dev/null: nowhere, eg: ls > /dev/null

Manipulating output with grep, awk, and cut

Grep: Search
Awk:  Awk is both a programming language and text processor that can be used to manipulate text data in very useful ways.
Cut : Select certain items with deliminator

grep search_term file: search term in file
grep -i : None case sensitive
grep  search_term file | awk {'print $12'}: search for lines, and only return the 12th item in line

linux command | grep 'search term': search in an linux command output, eg: ping -c l example.com | grep 'bytes from'

Cut: Eg: ping -c 1 example.com | grep 'bytes from' | cut -d = -f 4 : 4th thing counting by deliminator '='.  Counting method: 1st=2nf=3rd=4th


BASH Script
#!/bin/bash
# comment
commands

Two ways executing the bash file:
1. bash my.sh
2. chmod +x my.sh
    ./my.sh

Displaying text with echo

echo statement : need \ before special char, eg \(
echo 'statement' : take everything as text
echo "statement" : middle way, $ as special, everything else as text, \$ to take $ as text 

greeting='hello'
echo $greeting, world \(planet\)!
echo '$greeting, world (planet)!'
echo "$greeting, world (planet)!"
Output:
hello, world (planet)!
$greeting, world (planet)!
hello, world (planet)!

Variables

Define variable
Variable name should start with letter
a=Hello: No space
b="Hello World": if space, then put in quote

Use variable
$a: put $ before variable

Adding attributes to variables
declare -i d=123 #d is an integer
-i: integer
-r: read only
-l: lower case
-u: upper case

Built-in variables
$HOME : home dir
$PWD : current dir
$MACHTYPE : machine type
$HOSTNAME: system name
$BASH_VERSION: version of Bash
$SECONDS : time in seconds the bash session has run
$0 : file name

Command substitution

d=$PWD or d=$(pwd)
a=$(long command)

Working with numbers

((expression))
val=$((expression))

Operators
* / %       #(times, divide, remainder)
+ -         #(add, subtract)
** #Exponentiation

Example:
#!bin/bash
a=2
b=1
e=$((a+b))
echo "$a plus $b = $e"
((e++))
echo $e
((e--))
echo $e
((e+=5))
echo $e
((e*=2))
echo $e

Float Number (bc command)
f=$((1/3)) #0
g=$(echo 1/3 | bc -l) #.333

Comparing values

[[expression]]
1:FALSE
0:TRUE

For Char:
< > <= >=   #(the obvious comparison operators)
== !=       #(equal to, not equal to)&&          #(logical and)
 
Examples
[["cat" == "dog"]] #1, False
[[20 > 100]]  #0, True, as it compares it as string

For int numbers:
-lt #less than
-gt 
-le #less than or equal to
-ge
-eq #equal
-ne
 
For Logic:
&& #and
|| #or
! #not

For string
-z #is null?
-n #is not null?

Working with strings

Concatenation $a$b
a="hello"
b="world"
c=$a$b #helloworld
echo $c 

Length of string: ${#a}

Substring: 
${a:3} #substring starting from 3rd char 'lo' (index starts from 0)
${a:3:1} #start at 3, ask for 1 char after that
${c: -4} #4chars from end of the string 
${c: -4:3} #first 3 letters of the last 4 letters 

Search and Replace
1. search and replace once
${str/search_term/replace_term}
eg. 
fruit='apple banana banana cherry'
echo ${fruit/banana/durian} #first banana gets replaced

2. search and replace for all
${str//search_term/replace_term}
eg.
fruit='apple banana banana cherry'
echo ${fruit//banana/durian} #all banana gets replaced

3. Matching 
wildcard: *
eg.
fruit='apple banana banana cherry'
echo ${fruit//b*/durian} # 'banana banana cherry' gets replaced by 'durian'

4. search and replace once only if it is in certain location
${str/#search_term/replace_term} # only if search_term at the beginning of the string
${str/%search_term/replace_term} # only if search_term at the end of the string

Coloring and styling text
Echo with escaping
eg: echo -e '\033[34;42mColor Text\033[0m'
format: echo -e '\033[text style ANSI; foreground color; background colorm Text \033[clear format using 0 m'
Begin escaping: \
End escaping: m
Style: Foreground/Background color number code

Date and printf
today: date
format: date+"%d-%m-%Y"
          date+"%H-%M-%S"
printf "Name:\t%s\nID:\t%04d\n" "Scott" "12"
eg. 
today=$(date+"%d-%m-%Y')
time=$(date+"%H-%M-%S")
printf -v d "Current User:\t%s\nDate:\t\t%s @ %s\n" $USER $today $time
echo "$d"

Array (0 based)
a=()
b=("apple" "banana" "cherry")
echo ${b[2]}
b[5]="kiwi"
b+=("mango")
echo ${b[@]} #whole array

declare -A myarray
myarray[color]=blue
myarray["office"]="HQ West"

echo ${myarray["office"]} is ${myarray[color]}

Reading and Writing text files
Write
echo "Some test" > file.txt #Write by replacing
echo "Some test" >> file.txt #Write by adding
>file.txt  #delete content in the file
Read
while read f; do
echo $f
done<file.txt

Putting command in file and execute 
ftp -n < ftp.txt

Here documents
cat << EndOfText
This is a 
multiline
text string
EndOfText

cat << EndOfText >file.txt
This is a 
multiline
text string
EndOfText

<<- eliminate tab at the beginning of the each line

Control Structures
If...Else...
(1) if((expression))

(2) if expression
     then
          echo "True"
     elif expression2; then
          echo "False"
     else
          echo "Null"
     fi

a=5
if [ $a -gt 4 ]
then
        echo $a is greater than 4!
else
        echo $a is not greater than 4.
fi
#Note:  [ $a -gt 4 ] space needed

Case
a="dog"
case $a in
     cat) echo "Feline";;
     dog|puppy) echo "Canine";;
     *) echo "No match!";;
esac



Loop
while
i=0 
while [ $i -le 10 ]; do
     echo i:$i
     ((i+=1))
done

until
counterpart of while loop, once qualify -> exit

for
#M1
for i in 1 2 3
do 
          echo $i
done
#M2: 1~100, counting by 2
for i in {1..100..2}
#M3
for((i=1; i<=10;i++))
#M4 array
arr=("apple" "banana" "cherry")
for i in ${arr[@]}
#M5
declare -A arr
arr["name"]="Scott"
arr["id"]="1234"
for i in "${!arr[@]}"
do
          echo "$i: ${arr[$i]}"
done
#M6 command
for i in $(ls)


Loop
Example:
startdate='20140929'
for i in $(seq 1 1 7)
do
dati=$(date -d "$startdate $i days" +'%Y%m%d')
echo "$dati"
done

Function
#Note: must be a space between function name and {
function greet {
     echo "Hi $1!"
}
echo "And now, a greeting!"
greet Scott

function numberthings {
     i=1
     for f in $@; do
          echo $i: $f
          ((i+=1))
     done
}

numberthings $(ls)
numberthings pine birch spruce


Arguments
#manual argument: 
echo $1
echo $2
#put all argument as an array
for i in $@
do 
          echo $i
done

echo There are $# arguments.

Flags (Arguments by matching char, not by order)
while getopts u:p: option; do
     case $option in
          u) user=$OPTARG;;
          p) pass=$OPTARG;;
     esac
done
     echo "User: $user / Pass: $pass"

./my.sh -u scott -p secret

#u:p: there will be two flags
#u:p:ab a,b optional to be here
#:u:p:ab Other key in wildcard ?) 


Getting input during execution
Method 1
echo "What is your name?"
read name

echo "What is your password?"
read -s pass

read -p "What's your favorite animal?" animal

echo name: $name, pass: $pass, animal: $animal

Method 2
select animal in "cat" "dog" "bird" "fish"
do
     echo "You selected $animal!"
     break
done

select option in "cat" "dog" "quit"
do
     case $option in
          cat) echo "Cats like to sleep.";;
          dog) echo "Dogs like to play catch.";;
          quit) break;;
          *) echo "I'm not sure what that is.";;
     esac
done

Ensuring a response of number of arguments
if [$# -lt 3]; then
          cat <<- EOM
          This command requires three arguments: a1, a2, a3.
     EOM
else
          #the program goes here
          echo "Username: $1"
          echo "UserID: $2"
          echo "Favorite Number: $3"
fi

Setting default value
read -p "Favorite animal? [cat]" a
#if a is empty
while [[-z "$a"]]; do
          a="cat"
done
echo "$a was selected."  

Checking input format
read -p "What year? [nnnn]" a
while [[! $a=~[0-9]{4}]]; do
     read -p "A year, please! [nnnn]" a
done

echo "Select year: $a"



#Project Guessing Game

#Input a number
echo "Input A Secret Number: " 
read -s a
while [[ ! $a -lt 100 && $a -gt 0 ]]; do
     echo "A number in 0-100, please! " 
     read -s a
done

#Or a random Number 
rand=$RANDOM
a = ${rand:0:1}

function guess {
     if [ $1 -eq $2 ]; then
     echo "Bingo! The number is $1!"
     echo "You are the best!"
     echo "You are amazing!"
     echo "You are YOU DIAN NI HAI!"
     elif [ $1 -lt $2 ]; then
     echo "$1 is smaller than the number."
     else 
     echo "$1 is greater than the number."
     fi
}
read -p "Guess a number from 0 to 100: " b
guess $b $a
while [[ ! $a -eq $b ]]; do 
read -p "Guess again: " b
guess $b $a
done


Next step
man bash
tldp.org/LDP/abs/html/gotchas.html



Wednesday, April 29, 2015

JAVA Coursera Course Notes



JAVA vs. C++
  • 无直接指针操作 
  • 自动内存管理
  • 数据类型长度固定
  • 不用头文件
  • 不包含结构和联合
  • 不支持宏
  • 不用多重继承
  • 无类外全局变量
  • GOTO 

应用程序的类型:
Application: 应用程序,独立的程序,需要执行器运行
Applet: 小应用程序,由appleViewer或web浏览器运行


编译:使用JDK中的javac
Example: javac Hello.java

运行:java工具
Example: java Hello

文件名要与public class的名字一致
一个文件至多有一个public class

设定path和classpath
path: javac和java的路径
classpath: 所引用的class的路径

#check current path
set path
#set path
set path=.;c:\jdk\bin;...
#set classpath
set classpath

在java命令行上使用 classpath
javac -cp libxx.jar
java -cp libxx.jar

使用package时的编译
设置生成文件路径
java -d classes dir\*.java

java -cp classes edu.pku.tds.PackageTest


Applet的编辑,编译和运行

applet标签:<applet code="HelloWorldApplet.class" width=200 height=40 background=white>
运行
appleViewer HelloWorldApplet.html
or
download java(JRE) from java.com, and then enable java in browser

alternative to applet: flash, silverlight, javascript

jar打包
(1)编译 javac A.java
(2) 打包 jar cvfm A.jar A.man A.class
(3)运行 java -jar A.jar

javadoc生成文档
javadoc -d directory xxx.java

javap查看类信息
javap class_name
javap反汇编 (显示java虚拟机指令)
javap -c class_name

comments
/*
*
 */

输入/输出
Text Interface: 使用Scanner class
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.print("Please input one number");
int a = scanner.nextInt();
System.out.printf("%d squared is %d \n",a,a*a);
}
}

nextInt()
nextDouble()
next() /*(for char)*/


In and Out
java.io class
System.in.read()
System.out.print(), println, printf

Input Error: try{}catch

try{
c=(char) System.in.read();
}catch(IOException e){}

Read one char
c=(char) System.in.read();

Read one line
BufferedReader in = new BufferedReader(new inputStreamReader(System.in));
s=in.readLine();

Convert to Int, or Double
n=Integer.parseInt(s);
d=Double.parseDouble(s);

图形界面的输入和输出
AppGraphInOut.java
add(xxx) 加入对象
btn.addActionListener 处理事件
actionPerformed() 具体处理事件

Java 8 简写 e->{...}
lambda 表达式

Applet输入和输出
在init()中
add(xxx) 加入对象
btn.addActionListerner 处理事件

actionPerformed() 具体处理事件


java编程工具

Eclipse code template
Alt+/




L3

数据


2
primitive types: double, char, int
reference types: class, interface,
存储:
primitive types stack
reference types: heap

reference type is similar to pointer, contain address

赋值
double d2=d; copy value
Person p2=p; copy reference (pointing to the same class)

基本数据类型 4/8
整数型 byte,short,int,long
浮点数型 float,double
逻辑型 Boolean
字符型 char

Boolean: true/false,不可以用0/1代替
Char: char c=’A’ unicode编码,每个字符占两个字节
Int:

long b=3.14L

Java没有无符号数
Float: 3.14,3.14e2 (*10^2),3.14E2
Float a=3.14f

 

大小写敏感
class 首字母大写,其他首字母小写

运算符
取余, 个位 a%10
^ 异或
&& 短路short-circuit: 第一个操作false就不判断第二个操作
|| short-circuit or: if the first statement is true, then don’t evaluate the second statement
<< 左移a<<b b位,<<1=*2
>>带符号右移,int32位取模,long64位取模
>>>无符号右移

Example:


数据转换 (cast)
int i =(int)l;


流程控制语句
注释:
// 单行注释
/*...*/ multiple line comments
/**...*/ doc comment












If-else
if(condition)
{statement;}
else
{statement;}

Switch
switch(exp){
case const1:
  statement1;
  break;
case const2:
      statement2;
       break;
default:
statment_default;
break;
}

****** LOOP ******

FOR
int result=0;
for(int i=1; i<=100; i++){
result+=i;
}

WHILE
int i=1
while(i<=100){
result+=i;
i++;
}

DO/WHILE (at least execute once)
do{
result+=i;
i++;
}while(i<=100);


BREAK/CONTINUE
break;

label1: for(...){
label2: while(...){
break label1;
}
}

continue; //get into next iteration, skip the remaining statement in current iteration

continue label1;

ARRAY
int[] a;
double []b;
Mydate []c;

int []a=new int[3]; //allocate memory
a[0]=3;
a[1]=3;
a[2]=5

int[] a={3,3,5};
int[] a=new int[]{3,3,5};

MyDate[] dates={
new MyDate(22,7,1987),
new MyDate(1,1,2000),
new MyDate(4,30,2015)
};

length of array:
a.length
for(int i=0;i<a.length;i++){}

Enhanced for loop, in:
for(int age : ages){
System.out.println(age);
}

Copy:
//copy source from 0 to source.Length to dest
Array.Copy(source,0,dest,0,source.Length)

Multi-dimension array (array of array):
int [][] a={{1,2},{3,4,0,9},{5,6,7}}
int [][] t = new int[3][];
t[0]=new int[2];
t[1]=new int[4];
t[2]=new int[3];


L4

CLASS
class contains field (variable) and method (function)

Constructor
constructor is a special method
constructor has the same name as class, no return type
one class has one or more constructors
if no constructor is defined, system will provide a default constructor

Use object
Example:
Person p = new Person();
System.out.println(p.name);
p.sayHello();

Overloading
same name for multiple methods,with different signature
overloading can be used to implement polymorphism

'this' (same as python self)
Within method, field is the same as this.field
used to distinguish local variable from field
e.g.
//age, name local variable, this.age and this.name are fields
Person(int age, String name){
 this.age=age;
 this.name=name;
}

In constructor, can be used to construct another constructor
e.g.
Person()
{
  this(0,"") // must be put as the first statement
  ...
}

Inheritance
child class: subclass
parent class: superclass

class Student extends Person{
...
}

method override
define a method with the same method name as parent method

method overload
define a method with the same method with different argument compared with parent method

super
use fields and methods in parent class (that was override by the same name in current class)
compared with 'this' for current class

Example
void sayHello(){
   super.sayHello();
   System.out.println("My school is" + school);
}

use super to inherit constructor
Student(String name, int age, String school){
  super(name,age); // must put at the first place
  this.school=school;
}

Superclass <-> Subclass
Person p1 = new Student();
Student s = (Student) p1; //casting, OK

Person p2 = new Person();
Student s2 = (Student) p2; // casting, OK with complier, not OK with run

Package

package pkg1.pkg2....
classes in the same package can visit each other

import
import pkg1.pkg2.classname;
import pkg1.pkg2.*;
simplify the reference classname (otherwise need pkg1.pkg2.classname)

Class Path
put a class file into a directory
javac -d [directory]
To run a program, run the class file with main()
java pk.TestPkg

CLASSPATH
(1) java -classpath [directory]; . pk.TestPkg
(2) set environment variable
set classpath=[directory];

Modifiers




Modifier for class
public or null
public: everywhere
default: within package

fields are usually private

setter and getter for methods to modify and check values of private fields
private int age;
public void setAge(){}
public void getAge(){}


Other Keywords
static
final
abstract


static:
(1) belongs to the class, not to specific instance
(2) global variable
eg. System.in and System.out
class Person{
static long totalNum; //same for all persons
int age;
String Name;
}

import static
import static java.lang.System.*;
out.println(); //equivalent to System.out.println();

final:
final class: not inheritable - no subclass allowed
final method: cannot be override by subclass
final fields or local variable:
- the value can be set at most once, cannot change the value later ;
- set value at definition place, or in constructor
static final: constant, read-only, default value 0

abstract:
abstract class: cannot define instance of the class
abstract method: has the method name, but no body; only declare, no realization; placeholder for realization in subclass (by override)


Interface
define interface: all methods are automatically public abstract
implement interface (keyword 'implements'): can be inherited by multiple subclasses, and don't consider the inheritance relations
used to indicate common behavior or methods of multiple classes

eg.
interface Collection{
void add(Object obj);
void delete(Object obj);
Object find(Object obj);
int size();
}

//implements
class FIFOQueue implements Collection{
public void add(Object obj){
...
}
public void delete(Object obj){
...
}
public int currentCount{
...
}
}

constant in interface
type NAME = value; //automatically to be public,static,final

method in interface in Java 8
-static method
-default method (default implements, no need to implements in subclass)


Syntax Summary

Class Definition










L5

Variables
- primitive type
- reference type 

1.field
- in class
- has initial default value
- can be set as public, private, static, final

2.local variable
- in method
- does not have initial default value
- can be set as final; cannot be set as public, private, static

Variable pass
- primitive type: value pass
- reference type: address pass

Polymorphism
1. Polymorphism during compiling 
- overload
2. Polymorphism during running
- override
- dynamic binding --- virtual method invoking

upcasting
use subclass as a superclass
Person p = new Student();
virtural method invoking 
--system chose the actual type of instance to choose method
--all methods (except final) can do automatically dynamic binding

instanceof: check dynamic variable, boolean output
eg. if(x instanceof Integer){...}

common methods are virtual methods
static, private, final methods are not virtual methods
-static: only belongs to class, not related with instances; static method depends on the type that declared, not depend on the instance type
-private: not applicable to subclass, thus not virtual
-final: cannot be override, virtual does not exist

Object Construction and Initialization
Constructor
-abstract class also has constructor
-this: current class' constructor; super: super class' constructor; must come at the 1st statement, this/super only one can apply
-If no this or super in constructor, then default super() is used (superclass' no argument constructor), in this case, either there is no constructor is defined in superclass, such that there is default constructor a(); or there is a constructor without argument defined a(){...}
-multiple constructors can be defined

Initialization
p = new Person(){{age=18; name="Li Ming";}};
Inside class:
1. Instance Initializer
{statement;}
2. Static Initializer
-related to class, not related to instances; used at the first time the class is used, before instance initializer
static{statement;}

Initialization order:
step 1 super() -> step 2 field initialization (outside class constructor) -> step 3 other statements in class constructor

*final method in superclass constructor to avoid incorrect order of using virtual method

Garbage collection
finalize()
try(Scanner scanner = new Scanner(...) ){...} automatically call close() after the statement, more controllable

Internal Class / Inner Class
A class defined in another class
Used inside: same as common class
Used outside: 
OutClass a = new OutClass(...);
OutClass.InterClass b = a.new InterClass(...)
In InterClass, to use fields or methods with the same name in OutClass and InterClass:
OutClass field: OutClass.this.field
InterClass field: this.field

Modifier:
public, protected, (default), private
final, abstract

static InterClass (called nested class): it's an common class actually, not related to OuterClass instance
When define instance, no need to add OutClassInstance before new
Cannot call non-static fields or methods in OutClass

Local Class - a special Internal Class
defined in a method
Like local variables, no public, private, protected, static
can come with final or abstract
can call OutClass components
cannot call the local variables (temporary) in method, unless final local variables

Anonymous Class - a special Internal Class
- no class name, use superclass or interface name, no class, extends or implements
- an instance is created when class definition, 'new' before class definition
- used only once
- use superclass constructor
eg.
new Object(){
public String toString(){...}
}

Lambda expression
(parameters) -> result
e.g.
(String s) -> s.length()
x->x*x
()->{System.out.println("aaa");}

It's a function
It's an instance of anonymous class
It's an abbreviation of interface or interface function

E.g.
//a function needs a interface function f input
interface Fun{double fun(double x);}
static double Integral(Fun f, double a, double b, double eps){...}
//lambda as f input
d=Integral(x->Math.sin(x),0,1,EPS);
d=Integral(x->x*x,0,1,EPS);


primitive type -> Object (reference type)
int->Integer
Integer I = new Integer(10);

Boxing / Unboxing
Boxing -> reference type
Integer I=10;
Unboxing -> primitive type
int i=I;

Enum
It's a special class.
e.g.
enum Light{Red,Yellow,Green};
Light light = Light.Red

Annotation
not comments
e.g.
@Override
@Deprecated
@SuppressWarnings
User defined annotation
public @interface Author{
String name();
}

Reference and Pointer
reference is actually a pointer
function pointer: lambda expression
linked list:
class Node{
Object data;
Node next; //reference, pointer
}

Equal or Not Equal
Primitive: value equality
Reference: reference equality
Boxing reference: value equality for -128~+127, otherwise reference equality
Comparing contents in reference type:  equals, hashCode()
Strings:
-- don't use ==, use equals
-- String literal (constant) can be compared with ==

String hello="Hello", lo="lo";
System.out.println(hello=="Hello"); // true
System.out.println(hello==Other.hello); // true

System.out.println(hello=="Hel"+"lo"); // true
System.out.println(hello=="Hel"+lo); // false
System.out.println(hello==("Hel"+lo).intern()); // true
System.out.println(hello==new String ("Hello")); // false



L6 Exception
Basic format:
try{
 statement;
}catch(){
  statement for exception;
}

or

try{
 statement;
}catch(err1){
  statement for exception;
}catch(err2){
statement for exception;
}finally{
statement for exception;//run without condition; even return before finally
}

throw: throw error

Throwable superclass -> error subclass; exception subclass

Exception class
Constructor
public Exception();
public Exception(String message);
Exception(String message, Throwable cause);

Method
-getMessage()
-getCause()
-printStackTrace()

Exception Types
-Runtime Exception: not necessary to handle
-Checked Exception: need handle (catch/throws)
throws: put after method signature, claiming this method may throw exception
Example of throws:
public static void readFile()throws IOException{
...
}

Another format of try
try(type variable=new type()){
...
}
-automatically add finally{variable.close();}

User Defined Exception
...


Assert
assert statement: "output message"; //output if statement is False

Testing (JUnit)
Eclipse
Project->New->Junit Test Case
Project->Run as -> Junit Test

@Test marks testing method
fail(message); // program error
assertEqauls(param1, param2); //param1==param2
assertNull(param);//param should be null

Errors
-Syntax error

-Runtime error
try,catch

-Logic error
debug, unit test

Debug
-breakpoint
right click line left to add break point
Ctrl+shift+B

-trace
F5: Stepwise run
F6: Stepover run (skip getting inside method)
F7: Jump out of method
Ctrl+R: Run to the point

-watch
realtime watch: point to the variable to get value
fast watch: right click, Inspector
add watch: right click, watch


L7 Basic Library, Data Structure, and Algorithm
Basic Class Library
java.lang: language related
java.util: utility
java.io
java.awt, javax.swing: GUI
java.net: web
java.sql

JAVA API Doc
JDK source code

Object class
The direct or indirect superclass of all classes
Methods in Object class
(1) equals(): whether the contents is the same
Example:
Integer one = new Integer (1);
Integer anotherOne = new Integer (1);
if(one==anotherOne) //false
if(one.equals(anotherOne)) //true

(2)getClass(): a final method, return the class type in run time

(3)toString(): return the string type of the object
usually used to output: System.out.println(person);
or used to + string: "current person is" + person
adjust output info by overriding

(4)finalize()
clear object

(5) notify(), notifyAll(), wait()
multi-thread

Wrappers of basic type (basic type -> reference type)
Character, Byte, Short, Integer, Long, Float, Double, Boolean
Methods:
valueOf(String), toString()
xxxxValue(): get the value, eg. intValue()
Cannot modify the value inside

Boxing and unboxing
Integer I = 5; // Boxing, equivalent to  I=Integer.valueOf(5)
int i=I; //Unboxing, equivalent to i=I.intValue()


Math Class
public final static double PI;
public static double log(double a);
public static double random(); // random number in (0,1)
public static double sqrt();

System Class
System.getProperty(String name): get the system property value
System.getProperties(): get a object containing all system property value


String
Two types of String
- String: immutable, cannot change the value; inefficient in += : new string created
- String Buffer, StringBuilder: able to modify after construction
String s="";  //s+=x;
StringBuffer sb=new StringBuffer(); //sb.append(x);

Methods in String
-concat,replace,substring,toLowerCase,toUpperCase
-endsWith, startsWith, indexOf
-equals,equalsIgnoreCase

StringBuffer
Constructor
-StringBuffer()
-StringBuffer(int capacity)
-StringBuffer(String initialString)
Methods
-append, insert, reverse, setCharAt, setLength

StringToken: split string
-StringTokenizer(string, separater)

Date
Calender
-getTime() -> Date
Date
-getTime() -> long

Calender
-getInstance()
-get(DAY_OF_MONTH)
-set()
-setTime(date), getTime()

Date
-new Date(), new Date(System.currentTimeMillis())
-setTime(long), getTime()
SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
-format, parse

Java8: time api: java.time.*, java.time.format.*

Collection API and List
Interface
-subinterface List
-subinterface Set
Map
-key-value pair

Interface
-add
-remove
-set
-get
...

List interface
- Realized class: ArrayList, LinkedList, Vector (old,similar to array list)
- Iterator
eg. Iterator iterator = iterable.iterator();
while(iterator.hasNext()) doSomething(iterator.next());
Enhanced for
- for (Element e : list) doSomething(e);

Stack and Queue
Stack
-Last In First Out, LIFO
Methods
-push(Object item)
-pop()
-empty()
Eg.
Stack<String> stk = new Stack<>();

Queue
-First In First Out, FIFO
-LinkedList
Methods



Old Classes
Old -> Now
-Vector -> ArrayList
-Stack -> LinkedList
-Hashtable -> HashMap
-Enumeration -> Iterator

Set
-Realized classes: HashSet, TreeSet
-unique element: hashCode() not equal, if equal, then evaluate equals or == false

Map
-Realized classes: HashMap, TreeMap
-entrySet(): key-value set, keySet(), values()
-map.put("key","value");
-map.get("key");

Sort and Search
Arrays
-Arrays.asList(10,7,6,5,9) get a list
-sort(), binarySearch()
-eg. Arrays.<String>sort(s);
-eg. Arrays.<String>binarySearch(s,s[2])

Compare
-Comparable: java.lang.Comparable
-define a java.lang.Comparator
  eg. public int compare(T o1, T o2)
- more general, use Lambda
eg. Collections.sort(school,(p1,p2)->p1.age-p2.age);

Collections
-sort,binarySearch,reverse

Generic
Same processing method for different classes
eg.
Vector<String> v = new Vector<String> ();
v.addElement("one");
String s = v.elementAt(0);

Self-Defined Generic
- Class
class TNode<T>{
private T value
private ArrayList<TNode<T>> children = new ArrayList<>();
}

- Method
class BeanUtil{
public static <T> T getInstance(String clzName){

}

Type control
-? any type
reverse(List<?> list)
-extends: all subclass of E
Set.addAll(Collection<? extends E> col)
-super: all superclass os T
Collections.fill(List<? super T> list, T obj)


Algorithms
-Exhaust Algorithm 遍历
for(; ;){if();}
-Iterative Algorithm 迭代
while(){x=f(x);}
-Recursive Algorithm 递归
f(n){f(n-1);}
-Back-track Algorithm 回溯
x++; if(...) x--;
eg. Queen 8



L8 Threads
One process can contain multiple threads
-These threads share CPU - parallel or sequential (time slots)
-Share memory - eg. multiple threads visit the same object

Java supports multi-threading
java.lang -> Thread

Thread class
-run() method

eg.
-inheriting Thread
class MyThread extends Thread{
public void run() {
...
}
}
-or Use Thread constructor to pass Runnable object
class MyTask implements Runnable{
public void run() {
...
}
}
Thread thread = new Thread(mytask);

//Run the thread
thread.start();

-or anonymous class
- or Lambda expression

Hold thread for a while
Thread.sleep(1000);  // 1000 milisecond

Set priority
setPriority(int priority)

Regular thread - not Daemon
-if regular thread is still running, program will not end
Daemon thread
-backend thread
-thread.setDeamon(true)

Multi-threading 
Lock
monitor, lock, mutex, synchronized
-lock on some code blocks
synchronized(object){...}
-lock on methods
eg. public synchronized void push(char c){...}

Wait
wait()
-release object lock
notify(), or notifyAll()
-notify other waiting threads it is ready

java.util.concurrent library
-automatically lock and unlock
AtomicInteger
aint.getAndIncrement();
CopyOnWriteArrayList
CopyOnWriteArraySet
ConcurrentHashMap
ArrayBlockingQueue





L9 I/O and Stream

字节流(byte):InputStream, OutputStream
字符流(char):Reader, Writer

InputStream:
- read() method, byte-wise
OutStream:
-write() method
Reader: read()
Writer: write()

Node Stream
- Node example: file
Common node streams:

 Processing Stream
- Further processing of node stream or other processing stream
- Buffer, construct object, etc
Common Processing streams:


Standard Input/Output
System.in: InputStream type
System.out: PrintStream type
System.err: PrintStream type

Common Contents
-binary
-text
-object

Encoding format
-UTF-8
-ASCII
-GB2312
-default (depending on os)

New package java.nio.file.Files
-readAllLines() method

Object I/O
ObjectInputStream
ObjectOutputStream
DataInputStream
DataOutputStream

Serialize: output
Deserialize: input


Network Stream
URLGetContent.java
URL Class
-input stream: url.openStream()

Files and Directories
File Class
-treat directory as file
eg.
File f;
f= new File("Test.java");

Methods in File Class
-getName()
-getPath()
-exists()
-length()
-mkdir()
-list()
...

RandomAccessFile Class
-seed() method

Regular Expressions
-Match strings
-[charactor]{times}position:    [0-9]{2,4}\b
-[0-9]+ any number from 0 to 9, '+': 1 or more digits in one word

Pattern Class for applying Regular Expressions
-split
-matches

Matcher class
-find and replace: find(), appendReplacement()

URLCrawler


L10

GUI (Graphical User Interface)
AWT and swing
java.awt
javax.swing

AWT component
-Container, non-container

3 Steps to create GUI
(1) Create Component
(2) Set Layout
(3) Add Event handler

 Use JFrame
set as closable
setDefaultCloseOperation(EXIT_ON_CLOSE);

Common Layouts:
-FlowLayout
-BorderLayout
-GridLayout

Event
-Action
-getsource()
Event Listener
-Program responding to action

Register event listener
addxxxxListenser
Implement event listener
implements xxxListener
extends xxxAdapter




L11

Network programming
Socket Class for client side
client to server
ServerSocket Class for server side

Media programming
Graphics and its subclass Graphics2D class
object: getGraphics(), Convas, JComponent object

Media Audio and Video
Java Advanced Imaging (JAI); Java 3D; Java Media Framework (JMF)

Database Programming
DBMS (relational database management system):
-Mysql
-Oracle
-Microsoft SQL Server
-DB2

Tool: Navicat
Eclipse tool: DB Viewer, Quantum DB

Connection class to connect to DB


L12

Get class info
Class clz;
clz.getPackage();
clz.getDeclaredFields();
clz.getName();
clz.getDeclaredMethods();