2009年11月13日 星期五

JAVA筆記-vararg變長參數,利用省略號傳多個參數

利用省略號...傳多個參數,也是把參數自動轉成陣列
public int calc(int...c){
  int sum = 0;
  for(int i:c)
    sum+=i;
  return sum ;
}

/*使用時可傳多個參數*/
int a = new calc(1,2);
int b = new calc(1,2,3,4);
int c = new calc(a,b);
##ShowAll##

JAVA筆記-關於static成員

static只能用在類別裡,
不能使用在方法之內,
方法內也不能再定義方法

類別內,static不可呼叫non-static,只可呼叫static,
non-static則都可,
因static不需要被建立就可執行,
亦即static成員中根本沒有隱含的this參考指標指向物件,
但non-static卻未被建立,必須先new出來才可用
public class Test {
  void a() {
    b(); //正確
    d(); //正確
  }
  void b() {}
  static void c() {
    b(); //錯誤,不可直接呼叫b()
    d(); //正確,可直接呼叫d()
  }
  static void d(){
    new Test().b(); //正確,可直接呼叫b()
  }
}

JAVA筆記-Object類別提供的方法

##ShowAll##任何物件包含自定義物件,都繼承Object類別
Object提供的方法:
clone()
equals()
finalize()
hashCode()
notify()
notifyAll()
toString()
wait()

JAVA筆記-物件導向的兩個精神與三個特徵

##ShowAll##

精神
1.抽象化(Abstraction)
2.繼承(Inheritance)

特徵
1.繼承(Inheritance)
2.封裝(Encapsulation)
3.多型(Polymorphism)

2009年11月12日 星期四

JAVA筆記-A Multi-Threaded Server

這支程式好有挑戰性,不但要非常熟悉Thread的運用,
還要解決臨界區間和鎖的問題,
看了好多Multi-Threaded相關的程式碼,
對於設計整個演算法我也思考了好多種變形,
寫完這支程式完後會對分散式計算多點興趣了

補充:關於這個程式碼的不正確性
write client的會做寫入資料的操作,但對於這個操作,
我其實是通知server要寫入什麼資料,
再由server進行實際要寫入的資料,這會加重server的loading,
應該要把寫入資料的操作,寫在write client才好,當然,程式仍可執行

程式下載:Multi-Threaded Server

A Multi-Threaded Server
You are to implement a multi-threaded server that provides shared read/write access to an integer array of size 10.
The server will maintain an array of 10 integers. It will accept two client operations:
read - will return the values of the integers in the array Processes array
write num[10] - will update the values of the integers in the array with the integers in num
Server Threads
main thread
• Receive requests for read or write service
• Create a new thread to service each client request, then loop back to handle the next request.
read/write threads
• Communicate with associated clients. They will need the socket after the main thread has accepted the client.
• Handle concurrency control. That is, once created, it is up to the new read or write thread to determine if it is "safe" to perform the operation.
• Should allow multiple concurrent readers, but exclusive access for writers.
• When a writer finishes writing and there are both readers and writers waiting, the finishing writer should allow the first waiting writer to execute before anyallow the first waiting writer to execute before any waiting readers. If there are no waiting writers, the finished writer should allow all waiting readers to execute concurrently.
• Each read/write thread should perform a busy loop incrementing a local variable from 0 to 2,000,000 before actually doing the reading or the writing of the shared array. Make sure that you put this loop inside the critical section of the thread. This simulates longer service and will therefore introduce more contention for the resource.
The Clients
• You will create a set of clients to exercise the server. „
• You should implement both the writer and reader clients.„
• Clients should loop making their requests several times - Clients should loop making their requests several times -enough to get contention in the server. „
• Clients should print status messages to the screen with an identifier indicating which client that the message came from.„
• On testing, you should create enough clients to fully demonstrate the concurrency control technique that you have implemented.
• With proper concurrency control, the readers should always get an array with all elements having been written by one writer. Your status messages should check this and indicate its validity.

Note: Neither client needs user input. The read client simply does a loop (say 30 times) issuing a read request to the server and printing the results. The write client does a loop (say also 30 times) issuing write requests to the server You(say also 30 times) issuing write requests to the server. You can make the write client to use the loop counter to be the value it writes so that the first time it writes all 1's, the second time all 2's, etc. Then, you should see that the read client gets back 10 uniform values each time (say all 1's the first read, all 2's for the next 3 times, all 10's the next time). The values depend on the interleaving. All we can say is that a single read loop iteration should get the same 10 values back. You can also try putting busy loop or sleep delays in both reader and writer clients if you want to see how it impacts the interleaving and the result.

JAVA筆記-物件導向萬年曆程式

這支程式,是完全用物件導向的觀念來撰寫的
可能題目也不難的關係,所以我自覺物件化寫得很好
以前寫JAVA都像是小孩子寫C一樣,大學真沒學好

程式下載:Calendar


題目:
Implement a calendar class and a calendar_month include the following functionality:
(1) calendar :
     Specify a year, such as 1989
     Specify a print pattern. Pattern a is a 2*6 format, and pattern b is 3*4 format.
(2) calendar_month :
     Assign a year.
     Assign a month.
     Compute dates and fill the dates of each week in the month of the year.
(3) Constraints :
     All the dates are stored in calendar_month.
     All the dates are printed by a method in calendar_month.

實作方法:
CalendarFrame : 使用者介面(輸入年、月、格式)
Calendar : 計算年曆、輸出年曆
Calendar_Month : 計算月曆、輸出月曆


操作介面:


顯示結果:

SML筆記-兩LIST相加

補充:ML比Prolog人性化了一點,
          但就連Google也很難查詢到中文教學
          乖乖看英文吧!

ML語言是functional language,
很不直覺的程式,思考邏輯好難建立起來喔!
而且連本中文教學書都沒有可以讓我學習
臨時抱佛腳,沒從基礎打起,寫得很頭大

使用軟體:SML of New Jersey

題目:
Define a function sumlists: int list * int list -> int list which takes in input two lists of integers and gives as result the list of the sums of the elements in corresponding position in the input lists. The shortest list has to be seen as extended with 0’s.
Examples:
(1) Sumlists([],[]) = []
(2) Sumlists([1,2],[3,4]) = [4,6]
(3) Sumlists([1],[3,4,2]) = [4,4,2]
(4) Sumlists([1,6],[3]) = [4,6]

答案:
fun merge(xs,ys) =
if null(ys) then xs
else hd(xs)+hd(ys)::merge(tl(xs), tl(ys));

fun sumlists(x, y) =
if length(x) >= length(y) then merge(x, y)
else if length(x) < length(y) then merge(y, x) else [];


更簡短的答案:
fun sumlists([],[]) = []
| sumlists(x::xs,y::ys) = (x+y)::sumlists(xs,ys)
| sumlists([],y::ys) = (y)::sumlists([],ys)
| sumlists(x::xs,[]) = (x)::sumlists(xs,[])
;


測試結果: