112年 一般警察特考 三等 警察資訊管理人員 物件導向程式設計 試卷

pdf
103.25 KB
4 頁
windows10
侵權投訴
加載中. ..
PDF
112
公務人特種試警察人般警人員安全局國安全情報
112
退
一般警察人員考試
三等考試
警察資訊管理人員
物件導向程式設計
2小時 座號:
※注意:
使
使
代號:
30450
頁次:
4
1
針對以下 C++程式碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;
class Pet {
public:
virtual int eat(int f) = 0;
private:
string name;
};
class Dog: public Pet {
public:
Dog() {food =0; }
int eat(int f) {
food += f;
cout<<food<<endl;
return food;
}
private:
int food;
};
int main() {
Pet *d1 = new Pet();
Pet *d2 = new Dog();
Pet *d3 = new Dog();
d2.eat(5);
cout<<d1->eat(5)<<endl;
cout<<d3->eat(5)<<endl;
return 0;
}
請完成統一塑模語言(UML)類別圖(a)~(e)10 分)
請標示出錯誤行數程式碼,並說明錯誤原因。若將錯誤碼行數註解,
程式輸出為何?(15 分)
{ (a) }
Pet
- name: string
(b)
Dog
(d)
(e)
(c)
代號:
30450
頁次:
4
2
針對以下 Java 程式碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
interface Moving { public int go(int s); }
class Fly implements Moving { public int go(int s) { return s*5; } }
class Walk implements Moving { public int go(int s) { return s*2; } }
class Pet {
public Pet(Moving m, String mObj) {
moving = m;
movingObject = mObj;
distance = 0;
System.out.println(""+moving.go(5));
}
public void go(int d) { distance += moving.go(d); }
public String toString() { return (movingObject + " " + distance);}
private Moving moving;
private int distance;
private String movingObject;
};
class Bird extends Pet { public Bird(Moving m) { super(m, "wing"); } }
class Cat extends Pet { public Cat(Moving m) { super(m, "leg"); } }
public class Test {
public static void run(Pet p) { p.go(5); System.out.println(p); }
public static void main(String[] args) {
Moving m1 = new Fly();
Moving m2 = new Walk();
Pet p1 = new Bird(m1);
Pet p2 = new Cat(m2);
run(p1);
run(p2);
}
}
請完成統一塑模語言(UML)類別圖(a)~(f)10
此程式輸出為何?並請說明 Line 20 程式碼的意義與運作流程,包含
參數 p p.go()System.out.println(p)的說明。15 分)
Pet
CatBird
(a)
Moving
WalkFly
(b)
(c)
(d)
(f)
代號:
30450
頁次:
4
3
以下 C++程式實現部分 Map 的功能keys 存字元values 存該字元對應
之整數值。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;
class IMap{
public:
IMap(int s) {
mSize = 0;
keys = new char[s];
values = new int[s];
for (int i=0; i<s; i++) { keys[i]=&apos;&apos;; values[i]=0; }
}
int &operator[] (char c) {
for (int i=0; i<mSize; i++) if (keys[i]==c) return values[i];
keys[mSize]=c;
return values[mSize++];
}
~IMap() { delete keys; delete values; }
private:
char *keys;
int *values;
int mSize;
};
void test01() {
IMap x(10);
x[&apos;a&apos;] = 5;
cout<<x[&apos;a&apos;]<<endl;
cout<<x[&apos;b&apos;]<<endl;
}
int countChar(string s, char c) {
IMap x(20);
for (int i=0; i<s.length();i++)
x[s[i]] = (a) ;
return (b) ;
}
void test02() { cout<<countChar("ababbccdewbsu", &apos;b&apos;)<<endl; }
int main() {
test01();
test02();
return 0;
}
請說明執行 test01()的輸以及說 IMap 類別 int&operator[] (char c)
函式的設計邏輯。(15 分)
函式 countChar(s, c)是利 IMap 類別的 Map 功能,統計在字 s中,
字元 c出現次數請完成此函式(a), (b)使之能正常執行其功能(10 分)
代號:
30450
頁次:
4
4
以下 Java 程式模擬銀行帳戶存款與查詢餘額的多執行緒功能。Account
是帳戶類別可以存款與查詢餘額ATM 類別可以操作帳戶的存款功能
Test 類別輸出查詢之最後帳戶餘額
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.ArrayList;
class Account {
public void deposit(int m) { balance = balance + m; }
public int getBalance() { return balance; }
private int balance=0;
}
class ATM extends Thread {
public ATM(Account a, int m) {account = a; money = m;}
public void run() {
try {Thread.sleep(100);
} catch (InterruptedException e) { e.printStackTrace(); }
synchronized(Account.class) { account.deposit(money); }
//account.deposit(money);
}
private Account account;
private int money;
}
public class Test {
public static void main(String[] args) throws InterruptedException {
ArrayList<Thread> ts = new ArrayList<>();
Account account = new Account();
for(int i = 1 ; i <= 10 ; i++) {
Thread atm = new ATM(account, i);
atm.start();
ts.add(atm);
}
for(Thread t : ts) { t.join(); }
System.out.println("" + account.getBalance());
}
}
請說明此程式的執行緒之特性以及此程式輸出。10 分)
Line28 程式 Line13
Line14,28 解後
15 分)
收藏 ⬇️ 下載