進階應用程式設計 2014/12/07

95
進階程式設計 哲輝

Upload: samael-wang

Post on 30-Jul-2015

190 views

Category:

Engineering


9 download

TRANSCRIPT

Page 1: 進階應用程式設計 2014/12/07

進階程式設計⺩王哲輝

Page 2: 進階應用程式設計 2014/12/07

關於我

Page 3: 進階應用程式設計 2014/12/07

Hi, I’m Samael

Pr. Engineer, Connected Services, HTC Creative Labs

[email protected]

Previous

Page 4: 進階應用程式設計 2014/12/07

課程規劃

Page 5: 進階應用程式設計 2014/12/07

⼀一個⺫⽬目標

Page 6: 進階應用程式設計 2014/12/07

兩個主題

Page 7: 進階應用程式設計 2014/12/07

三個週次

Page 8: 進階應用程式設計 2014/12/07

⺫⽬目標

Page 9: 進階應用程式設計 2014/12/07

⺫⽬目標

沒有⼈人睡著

Page 10: 進階應用程式設計 2014/12/07

⺫⽬目標

沒有⼈人睡著

Page 11: 進階應用程式設計 2014/12/07

⺫⽬目標對 Java ⾏行動裝置程式設計

Page 12: 進階應用程式設計 2014/12/07

⺫⽬目標對 Java ⾏行動裝置程式設計

略懂~略懂~

Page 13: 進階應用程式設計 2014/12/07

主題

Java 物件與執⾏行緒

Android 基本概念

Page 14: 進階應用程式設計 2014/12/07

主題

Java 物件與執⾏行緒

Android 基本概念這部份⽤用 C/C++ 解釋

Page 15: 進階應用程式設計 2014/12/07

週次W1: Java 物件與執⾏行序

W2: Android 基本概念

W3: Android (續) + 專案展⽰示(暫定)

Page 16: 進階應用程式設計 2014/12/07

範例程式

https://github.com/freesamael/npu-java-course-2014-fall

Page 17: 進階應用程式設計 2014/12/07
Page 18: 進階應用程式設計 2014/12/07

學 Java 到底可以幹嘛?

Page 19: 進階應用程式設計 2014/12/07

藍鑽級 程式 設計師

Page 20: 進階應用程式設計 2014/12/07

藍鑽級 程式 設計師

Page 21: 進階應用程式設計 2014/12/07
Page 22: 進階應用程式設計 2014/12/07

Java 平台

Page 23: 進階應用程式設計 2014/12/07

Java EE Java SE Java ME

Android

Page 24: 進階應用程式設計 2014/12/07

Java EE

Page 25: 進階應用程式設計 2014/12/07

Java EE很夯的雲端

Page 26: 進階應用程式設計 2014/12/07

Java EE很夯的雲端

https://www.youtube.com/watch?v=vxf1skKXwM0

千⾔言萬語不如看廣告

Page 27: 進階應用程式設計 2014/12/07

Java SE

Page 28: 進階應用程式設計 2014/12/07

Java SE

Java

Page 29: 進階應用程式設計 2014/12/07

Java ME

Page 30: 進階應用程式設計 2014/12/07

Java ME

Series 40

Page 31: 進階應用程式設計 2014/12/07

Java ME⼀一代霸主

Series 40

Page 32: 進階應用程式設計 2014/12/07

Java ME⼀一代霸主

過氣英雄Series 40

Page 33: 進階應用程式設計 2014/12/07
Page 34: 進階應用程式設計 2014/12/07

Java ME

東⼭山 再起

Page 35: 進階應用程式設計 2014/12/07

Java ME

東⼭山 再起進廣告

https://www.youtube.com/watch?v=Kt5VulFqBm4

Page 36: 進階應用程式設計 2014/12/07

Android

Page 37: 進階應用程式設計 2014/12/07

Android

Page 38: 進階應用程式設計 2014/12/07
Page 39: 進階應用程式設計 2014/12/07

“Java 是⼀一種 物件導向程式語⾔言”

Page 40: 進階應用程式設計 2014/12/07

什麼是物件?

Page 41: 進階應用程式設計 2014/12/07

將真實世界的物件對應到程式語⾔言

典型 範例

Page 42: 進階應用程式設計 2014/12/07
Page 43: 進階應用程式設計 2014/12/07

以 C 實作資料結構#define FF (0)#define FR (1)#define RR (2)

struct Porche911 {int cost;int top_speed;double time_0_62;double braking_distance;double cornering;int layout;double speed;double position;

};

Page 44: 進階應用程式設計 2014/12/07

初始化

void init911(struct Porche911 *p911) {p911->cost = 172700;p911->top_speed = 311;p911->time_0_62 = 4.0;p911->braking_distance = 30.2;p911->cornering = 0.99;p911->layout = RR;

p911->speed = 0.0;p911->position = 0.0;

}

Page 45: 進階應用程式設計 2014/12/07

油⾨門 / 煞⾞車

void accelerate(struct Porche911 *p911, double depth, double duration) {

p911->speed = ...p911->position = ...

}

void brake(struct Porche911 *p911, double depth, double duration) {

p911->speed = ...p911->position = ...

}

Page 46: 進階應用程式設計 2014/12/07

C++ 的作法class Porche911 {public:

Porche911();void accelerate(double depth, double duration);void brake(double depth, double duration);

private:int cost;int top_speed;double time_0_62;double braking_distance;double cornering;int layout;double speed;double position;

};

Page 47: 進階應用程式設計 2014/12/07

流程⽐比較

int main(int argc, char *argv[]) {struct Porche911 my911;init911(&my911);accelerate(&my911, 0.8, 1.2);brake(&my911, 0.3, 0.5);return 0;

}

int main(int argc, char *argv[]) {Porche911 my911;my911.accelerate(0.8, 1.2);my911.brake(0.3, 0.5);return 0;

}

Page 48: 進階應用程式設計 2014/12/07

Object = data + code

Page 49: 進階應用程式設計 2014/12/07

Object = data + code

也就是封裝

Page 50: 進階應用程式設計 2014/12/07

什麼是 物件導向?

Page 51: 進階應用程式設計 2014/12/07

封裝、 繼承、多型

Page 52: 進階應用程式設計 2014/12/07

繼承

Page 53: 進階應用程式設計 2014/12/07
Page 54: 進階應用程式設計 2014/12/07
Page 55: 進階應用程式設計 2014/12/07
Page 56: 進階應用程式設計 2014/12/07

Generalization

Page 57: 進階應用程式設計 2014/12/07

Specialization

Page 58: 進階應用程式設計 2014/12/07

A Sedan is-a Vehicle A SUV is-a Vehicle A MPV is-a Vehicle

Page 59: 進階應用程式設計 2014/12/07

class Point2d {public:

int x;int y;

};

class Point3d : public Point2d {public:

int z;};

Page 60: 進階應用程式設計 2014/12/07

Q1 - 輸出?#include <cstdio>int main(int argc, char *argv[]) {

Point2d p2d;p2d.x = 100;p2d.y = 50;printf("Point2d: x=%d, y=%d\n", p2d.x, p2d.y);

Point3d p3d;p3d.x = 100;p3d.y = 50;p3d.z = 75;printf("Point3d: x=%d, y=%d, z=%d\n", p3d.x, p3d.y, p3d.z);

Point2d *p2dp = &p3d;p2dp->x = 50;p2dp->y = 100;printf("pointer of Point3d as Point2d: x=%d, y=%d\n",

p2dp->x, p2dp->y);

return 0;}

Page 61: 進階應用程式設計 2014/12/07

Q2 - 以 C 語⾔言改寫

class Point2d {public:

int x;int y;

};

class Point3d : public Point2d {public:

int z;};

Page 62: 進階應用程式設計 2014/12/07

struct Point2d {int x;int y;

};

struct Point3d {struct Point2d base;int z;

};

Page 63: 進階應用程式設計 2014/12/07

Q3 - 輸出?#include <stdio.h>int main(int argc, char *argv[]) {

struct Point2d p2d;p2d.x = 100;p2d.y = 50;printf("Point2d: x=%d, y=%d\n", p2d.x, p2d.y);

struct Point3d p3d;p3d.base.x = 100;p3d.base.y = 50;p3d.z = 75;printf("Point3d: x=%d, y=%d, z=%d\n", p3d.base.x, p3d.base.y, p3d.z);

struct Point2d *p2dp = (struct Point2d *)&p3d;p2dp->x = 50;p2dp->y = 100;printf("pointer of Point3d as Point2d: x=%d, y=%d\n",

p2dp->x, p2dp->y);

return 0;}

Page 64: 進階應用程式設計 2014/12/07

多型

Page 65: 進階應用程式設計 2014/12/07

class Engineer {const char *title;

public:Engineer(): title("Engineer") {}const char* getTitle() {

return title;}

};

class SrEngineer : public Engineer {const char *title;

public:SrEngineer(): title("Sr. Engineer") {}const char* getTitle() {

return title;}

};

class PrEngineer : public Engineer {const char *title;

public:PrEngineer(): title("Pr. Engineer") {}const char* getTitle() {

return title;}

};

Page 66: 進階應用程式設計 2014/12/07

Q4 - 輸出?

#include <cstdio>int main(int argc, char *argv[]) {

Engineer eng;printf("%s\n", eng.getTitle());SrEngineer sr;printf("%s\n", sr.getTitle());PrEngineer pr;printf("%s\n", pr.getTitle());

return 0;}

Page 67: 進階應用程式設計 2014/12/07

Q5 - 輸出?#include <cstdio>int main(int argc, char *argv[]) {

Engineer eng;SrEngineer sr;PrEngineer pr;

Engineer *engp = &eng;printf("%s\n", engp->getTitle());engp = &sr;printf("%s\n", engp->getTitle());engp = &pr;printf("%s\n", engp->getTitle());

return 0;}

Page 68: 進階應用程式設計 2014/12/07

class Engineer {const char *title;

public:Engineer(): title("Engineer") {}virtual const char* getTitle() {

return title;}

};

class SrEngineer : public Engineer {const char *title;

public:SrEngineer(): title("Sr. Engineer") {}const char* getTitle() {

return title;}

};

class PrEngineer : public Engineer {const char *title;

public:PrEngineer(): title("Pr. Engineer") {}const char* getTitle() {

return title;}

};

Page 69: 進階應用程式設計 2014/12/07

Q6 - 輸出?#include <cstdio>int main(int argc, char *argv[]) {

Engineer eng;SrEngineer sr;PrEngineer pr;

Engineer *engp = &eng;printf("%s\n", engp->getTitle());engp = &sr;printf("%s\n", engp->getTitle());engp = &pr;printf("%s\n", engp->getTitle());

return 0;}

Page 70: 進階應用程式設計 2014/12/07

Q7 - 以 C 語⾔言改寫class Engineer {

const char *title;public:

Engineer(): title("Engineer") {}const char* getTitle() {

return title;}

};

#include <cstdio>int main(int argc, char *argv[]) {

Engineer eng;printf("%s\n", eng.getTitle());

return 0;}

Page 71: 進階應用程式設計 2014/12/07

struct Engineer {const char *title;

};

void Engineer_init(struct Engineer *this) {this->title = "Engineer";

}

const char* Engineer_getTitle(struct Engineer *this) {return this->title;

}

#include <stdio.h>int main(int argc, char *argv[]) {

struct Engineer eng;Engineer_init(&eng);printf("%s\n", Engineer_getTitle(&eng));

return 0;}

Page 72: 進階應用程式設計 2014/12/07

這個也可以⽤用 C 語⾔言改寫嗎?class Engineer {

const char *title;public:

Engineer(): title("Engineer") {}virtual const char* getTitle() {

return title;}

};

#include <cstdio>int main(int argc, char *argv[]) {

Engineer eng;printf("%s\n", eng.getTitle());

return 0;}

Page 73: 進階應用程式設計 2014/12/07

中場休息 Q&A

Page 74: 進階應用程式設計 2014/12/07

Java 執⾏行緒

Page 75: 進階應用程式設計 2014/12/07

WHY?

Page 76: 進階應用程式設計 2014/12/07

GUI 範例

Page 77: 進階應用程式設計 2014/12/07

Start a Thread #1

public class HelloRunnable implements Runnable {

public void run() { System.out.println("Hello from a thread!"); }

public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); }

}

Page 78: 進階應用程式設計 2014/12/07

Start a Thread #2

public class HelloThread extends Thread {

public void run() { System.out.println("Hello from a thread!"); }

public static void main(String args[]) { Thread t = new HelloThread(); t.start(); }

}

Page 79: 進階應用程式設計 2014/12/07

Sleeppublic class HelloThread extends Thread {

public void run() { try { Thread.sleep(4000); } catch (InterruptedException e) {} System.out.println("Hello from a thread!"); }

public static void main(String args[]) { Thread t = new HelloThread(); t.start(); }

}

Page 80: 進階應用程式設計 2014/12/07

Joinpublic class HelloThread extends Thread {

public void run() { System.out.println("Hello from a thread!"); }

public static void main(String args[]) { Thread t = new HelloThread(); t.start(); try { t.join(); } catch (InterruptedException e) {} }

}

Page 81: 進階應用程式設計 2014/12/07

回顧 GUI 範例

Page 82: 進階應用程式設計 2014/12/07

Q8 - 輸出?

public class Counter { private int c = 0;

public void increment() { c++; }

public void decrement() { c--; }

public int value() { return c; }}

public static void main(String args[]) {final Counter c = new Counter();

Thread t1 = new Thread() {public void run() {

for (int i = 0; i < 50000; i++) c.increment();}

};

Thread t2 = new Thread() {public void run() { for (int i = 0; i < 50000; i++)

c.decrement();}

};

t1.start();t2.start();try {

t1.join();t2.join();

} catch (InterruptedException e) {}

System.out.println("value=" + c.value());}

Page 83: 進階應用程式設計 2014/12/07

臨界區段 (Critical Section)

Page 84: 進階應用程式設計 2014/12/07

Critical Section 要件

• Mutual Exclusion:不允許兩個以上的執⾏行緒同時在對應的 critical section 中執⾏行。

• Progress:若沒有執⾏行緒在對應的 critical section 中執⾏行,則控制的機制不能阻擋請求進⼊入 critical section 之執⾏行緒進⼊入critical section。

• Bounded Waiting:控制機制必須使等待進⼊入 critical section 之執⾏行緒在有限時間內得以進⼊入。

Page 85: 進階應用程式設計 2014/12/07

三種做法

• Semaphore

• Mutex (= binary semaphore)

• Monitor

• Message Passing

Page 86: 進階應用程式設計 2014/12/07

三種做法

• Semaphore

• Mutex (= binary semaphore)

• Monitor

• Message Passing

全部講下去就要花三週了...

Page 87: 進階應用程式設計 2014/12/07

Synchronizationpublic class SynchronizedCounter { private int c = 0;

public synchronized void increment() { c++; }

public synchronized void decrement() { c--; }

public synchronized int value() { return c; }}

Page 88: 進階應用程式設計 2014/12/07

Q9 - does it work?public class SynchronizedCounter { private int c = 0;

public synchronized void increment() { c+=2; decrement(); }

public synchronized void decrement() { c--; }

public synchronized int value() { return c; }}

Page 89: 進階應用程式設計 2014/12/07

Reentrant Synchronization

⼀一個執⾏行緒不能 acquire 其他執⾏行緒正在使⽤用的 lock,但是可以重複 acquire 已經取得的 lock。

Page 90: 進階應用程式設計 2014/12/07

Reentrant Synchronization

⼀一個執⾏行緒不能 acquire 其他執⾏行緒正在使⽤用的 lock,但是可以重複 acquire 已經取得的 lock。

換個說法:已經進⼊入 critical section 內的執⾏行緒可以持續執⾏行相同 critical section 的功能。

Page 91: 進階應用程式設計 2014/12/07

Deadlock static class Friend { private final String name; public Friend(String name) { this.name = name; } public String getName() { return this.name; } public synchronized void bow(Friend bower) { System.out.format("%s: %s" + " has bowed to me!%n", this.name, bower.getName()); bower.bowBack(this); } public synchronized void bowBack(Friend bower) { System.out.format("%s: %s" + " has bowed back to me!%n", this.name, bower.getName()); } }

Page 92: 進階應用程式設計 2014/12/07

Deadlock

public static void main(String[] args) { final Friend alphonse = new Friend("Alphonse"); final Friend gaston = new Friend("Gaston"); new Thread(new Runnable() { public void run() { alphonse.bow(gaston); } }).start(); new Thread(new Runnable() { public void run() { gaston.bow(alphonse); } }).start(); }

Page 93: 進階應用程式設計 2014/12/07

Java Monitor(Optional)

Page 94: 進階應用程式設計 2014/12/07

Producer Consumer Problem

(Optional)

Page 95: 進階應用程式設計 2014/12/07

作業撰寫⼀一 Java 程式,由命令列輸⼊入兩個 4x4 矩陣 A、B, 以 4 個背景執⾏行緒平均執⾏行 A x B 矩陣相乘之運算,

從命令列印出運算結果。

Deadline: 12/20

參考此網站驗證結果 http://www.bluebit.gr/matrix-calculator/

matrix_multiplication.aspx