2009年6月25日 星期四

Homework 11 5/25 User test

舊版的電子系網頁,感覺有較多的多媒體呈現,但甫進入只有看到幾個簡單的項目,使用者要尋找自己需要的資訊還必須從少數的大項目去思考應該要點哪一個連結,若是不熟悉舊版的網站,很容易在網站上迷路。

而新版的電子系網站架構類似我們常在使用的網誌,雖然沒有太大範圍的多媒體展示,但最新的資訊,教授的公告以及系上的公告都在網頁邊欄清楚得顯示,主頁文章也直接讓我們看到重點,可以提升不少尋找資訊的效率。

我覺得另外最重要的一點是,新版網站的讀取似乎比舊版網頁輕鬆了一些,電腦的流量負擔可以得到減輕。

LAB30 6/15 Hanoi Tower

The pseudocode for Hanoi Tower is as follows:

solve(N, Src, Aux, Dst)
if N is 0 return
solve(N-1, Src, Dst, Aux)
Move N from Src to Dst
solve(N-1, Aux, Src, Dst)


Write the Java program based on the pseudocode in the above.

程式畫面 Recursive Method


程式畫面 Demo program

Homework 12 6/1 Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.

Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.


程式畫面 Sorting method


由double[].length取得陣列長度
在將兩個for迴圈組成的排序法放入static method之中。


程式畫面 Demo program

LAB27 6/1 Array

Study Display 6.1, and then write a program that can sort numbers in ascending order.

程式畫面 Array sorting


預設一個整數陣列array,具有五個陣列元素。

使用兩個for迴圈的排序法進行排序。

LAB26 5/25 Static Method II on Complex multiply

Define a Complex class with a static method for computing complex addition. Use (2+3i)*(4+5i) in your test.


程式畫面 Class


Complex 乘積的實部是由原本兩實部相乘再加上原本兩虛部相乘的總合
虛部則為虛部和實部的乘積總合。

故2+3i * 4+5i = 2*4+3i*5i + 2*5i+4*3i
= 8+15(-1) + 10i +12i
= -7 + 22i

程式畫面 Demo

LAB 25 5/25 Magic Parking Tower

A parking tower is out of order someday.
If you park a Benz, you will end up with a Torben.
Write a program to simulate this scenario.
First create a class called CarParked which has a static method called outOfOrder.
Name an object called yourCar, which happens to be a Benz.

Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by

CarParked.outOfOrder(yourCar).

程式畫面 Class






程式畫面 Demo

LAB24 5/25 Static Mathod on Complex addition

Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.


程式畫面 Class definition


Static method add使用兩個Complex物件當引數,
回傳一個Complex物件。


程式畫面 Demo

LAB 23 5/25 Math methods

Compute the following mathematical functions.

Math.round(3.2)
Math.round(3.6)
Math.floor(3.2)
Math.floor(3.6)
Math.ceil(3.2)
Math.ceil(3.6)

程式畫面


可知round,floor,ceil為Class Math中的static method,不需要new一個新物件,
就可以叫用。

各method在程式畫面中有加註。

LAB22 5/25 Finding the max of three numbers by static method

Write a static method that computes the maximum of three float numbers.

程式畫面 Class define


定義Static method findMax
接受3個整數引數,回傳1個整數引數
利用兩個數比較後取大者,再與另外一個數比較,
得到最大值存入max,最後回傳max。


程式畫面 Demo program


輸入三個整數,以class name呼叫static method findMax,將三個整數傳入method。

回傳值存入max,即為最大值。

Homework 10 5/4 Temperature

Project 7 of Chapter 4

寫一個Class Temperature
包含溫標、溫度兩種變數。
四個Constructure包含沒有引數、僅溫標、僅溫度以及具備溫標及溫度等引數。
三個mutator的overloading 包含引入溫標、引入溫度以及引入溫標及溫度兩種。
兩種access分別為accessC和accessF,若本身溫標不同,能夠轉換後輸出。

三種比較方法,分別為greater()、less()以及equals(),
分別判斷是否大於、小於和等於另外一個Temperatrue物件,並且溫標不同時,能夠轉換後比較。

一個toString方法,回傳合適的字串格式資料。

最後進行0.0度C=32.0度F,-40.0度C=-40.0度F,100.0度C=212.0度F三組等式是否
成立的判斷。

程式畫面 Class-1


程式圖片 Class-2


程式圖片 Class-3


程式圖片 Demo program -1


程式圖片 Demo program -2


程式圖片 程式輸出結果

2009年6月24日 星期三

LAB21 5/4 Constructor

Write constructors in the lab Fraction Addition.

Constructor 建構子,是一種能夠在創見物件時進行一連串初始動作的定義。

程式畫面 Class


這裡的建構子(Constructor)使我們能夠在創建物件時就可以輸入數值。


程式畫面 Demo

LAB20 5/4 Method overloading

Method 的多載
依據Class definition 3,修改程式使其接受三種setDate

date1.setDate(1,2,2008);
date2.setDate("February",2, 2008);
date3.setDate(2008);

JAVA接受Method的多載,亦即Method的名稱相同,
但透過引數的分別去做不同的動作

程式畫面 Class(多載部分)




Method overloading的部分以紅框標示



程式畫面 Demo

Homework 9 4/27 Class Fraction

Project2 of Chapter 4

定義一個Class 名為Fraction

用來代表一個分數,必須要有修改分子分母、回傳分子分母(以Double型別)
以及約分功能。

程式畫面 Class Fraction


各項功能說明寫在Comment裡。
另外mutator允許使用引數修改,或者鍵盤輸入修改。


程式畫面 Demo

LAB19 4/27 ADT

Define a Complex class and write an object oriented program to compute (2+3i)+(4+5i) in Java.
The methods should include an access and a mutator.


程式畫面 Class




建立兩個private變數,re和im,分別代表實部(real)和虛部(imaginary)。
建立了
public void mutator(int r,int i)
{
re = r;
im = i;
}
使用兩個整數引數,可以對實虛部係數做修改。

建立了
public int re_access()
{
return re;
}
以及

public int im_access()
{
return im;
}

可分別回傳實部與虛部的係數,且型別為int。

最後是
public Complex add(Complex c)
{
int sum_re,sum_im;
sum_re = this.re + c.re;
sum_im = this.im + c.im;
Complex sum = new Complex(sum_re,sum_im);
return sum;
}

以class Complex 的物件為引數,回傳一個class Complex 的物件,該物件
為兩複數相加的總合。

其中在toString中
區分了im>0 im<0 和im = 0的狀況,以傳回正確的字串。

程式畫面 Demo

LAB18 4/27 Class Definition3

Do Display 4.7 (3rd, 2nd ed.) or 4.5 (1st ed.). Then use Display 4.8 to call 4.7.

Question
In Display 4.7, if the method setDate has the parameter as setDate(int month, int day, int year), what kind of changes should be made in its body of codes?


程式畫面 Display 4.7
上圖是Display4.7


程式畫面 修改Display 4.7的部分

Display 4.8使用到setDate(int,int,int)
這個方法,所以必須在Display 4.7的class 定義中,加入setDate方法的定義。
也就是藍色方框框住的部分。

程式畫面 Display 4.8

Homework 8 4/13 Fraction multiply test

Write a program to implement a method that can multiply 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator.

The multiplication of 2 fractions should be equal to a fraction.

Use 1/2 * 1/3 as the test.

Hints:
Fraction f1, f2;
f1.multiply(f2);

due date: 4/27/2009

程式畫面 Class





程式畫面 Demo

LAB17 4/13 Fraction equality test

Write a program to implement a method that can check
whether 2 fractions are equal.

You will implement a class called Fraction consisting of a numerator and a denominator.

The equality test of 2 fractions should return a boolean value.

Use the following as the tests.
* 1/2, 2/4
* 5/6, 6/7

Hints:
Fraction f1, f2;
f1.equals(f2);


程式畫面 Class


作法類似於分數相加,
判斷分數相等的方法為將兩分數分子通分,
通分後相等則可判定兩分數相等。
傳回true,否則傳回false。

程式畫面 Demo

LAB16 4/13 Fraction addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of
2 fractions should be equal to a fraction.
Use 1/2+1/3 as the test.

Hints:
Fraction f1, f2;
f1.add(f2);


程式畫面 Class




程式畫面 Demo

Homework 7 3/30 Counter

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test

counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();


程式畫面 Class



class定義中有一個私變數count,負責存放計算的非負整數。
另外還有應題目要求設置了4個public methods
其中在遞減1的部分加了限制,當count為零之後即不再作遞減的動作,以符合非負整數的條件。

程式畫面 Demo code



使用switch提供選擇想要的功能,照題目要求,程式輸出如下。


程式畫面 輸出

Homework 6 3/23 Incomes Average

Write a program to calculate average income by gender based on the following data, where F stands for female and M for male.

F 62,000
M 25,000
F 38,000
F 43,000
M 65,000
M 120,000
F 80,000
M 30,100

You should be able to allow users to type in a whole line such as F 80,000 followed by next line M 30,100.

程式畫面 code部分



定義f_sum與m_sum分別存放薪資的加總值

而m_count與f_count則存放資料筆數。

string gender用來讀取每筆資料的"F","M"關鍵字,
由if判別式分別將程式導向所屬的陳述式。

m_count與f_count在各自的性別中得到新的資料,即進行遞增。
m_sum和f_sum則加上新的薪資數值。

定義了一個boolean flag做為控制迴圈的旗標,預設為true
,若沒有得到正確的輸入,則flag設為false,離開迴圈。

平均值在輸出螢幕時一併計算。


程式畫面 輸出

LAB13 3/23 Cosine

Write a Java program to calculate the triangular function as follows:
Cos(x)=1 - x 2 /2!+ x 4/4!- x 6/ 6!...

程式畫面 程式碼部分


程式和exponential類似
同樣是在for 迴圈內控制分子、分母,並累加成sum
但偶數次方與偶數階乘,使得for迴圈參數、與分子分母變數的初值要另外設定。
同樣的首項的"1"是預設初值的。


程式畫面 輸出結果



由於輸出結果篇幅過長
故另外輸出一張圖片

Homework 5 3/16 Flow of Control

1. Project 7 of Chap. 3
輸出exponential的近似值
公式為:
1+x+x^2/2!+x^3/3!+.....+x^n/n!

程式畫面(程式碼部分)


輸出結果部分:由於篇幅過長,僅以文字呈現
請輸入x:
2
請輸入欲計算的項數n
10
2.0/1.0+
4.0/2.0+
8.0/6.0+
16.0/24.0+
32.0/120.0+
64.0/720.0+
128.0/5040.0+
256.0/40320.0+
512.0/362880.0+
1024.0/3628800.0+
=7.388994708994708
請輸入x:
2
請輸入欲計算的項數n
50
2.0/1.0+
4.0/2.0+
8.0/6.0+
16.0/24.0+
32.0/120.0+
64.0/720.0+
128.0/5040.0+
256.0/40320.0+
512.0/362880.0+
1024.0/3628800.0+
2048.0/3.99168E7+
4096.0/4.790016E8+
8192.0/6.2270208E9+
16384.0/8.71782912E10+
32768.0/1.307674368E12+
65536.0/2.0922789888E13+
131072.0/3.55687428096E14+
262144.0/6.402373705728E15+
524288.0/1.21645100408832E17+
1048576.0/2.43290200817664E18+
2097152.0/5.109094217170944E19+
4194304.0/1.1240007277776077E21+
8388608.0/2.585201673888498E22+
1.6777216E7/6.204484017332394E23+
3.3554432E7/1.5511210043330986E25+
6.7108864E7/4.0329146112660565E26+
1.34217728E8/1.0888869450418352E28+
2.68435456E8/3.0488834461171384E29+
5.36870912E8/8.841761993739701E30+
1.073741824E9/2.6525285981219103E32+
2.147483648E9/8.222838654177922E33+
4.294967296E9/2.631308369336935E35+
8.589934592E9/8.683317618811886E36+
1.7179869184E10/2.9523279903960412E38+
3.4359738368E10/1.0333147966386144E40+
6.8719476736E10/3.719933267899012E41+
1.37438953472E11/1.3763753091226343E43+
2.74877906944E11/5.23022617466601E44+
5.49755813888E11/2.0397882081197442E46+
1.099511627776E12/8.159152832478977E47+
2.199023255552E12/3.3452526613163803E49+
4.398046511104E12/1.4050061177528798E51+
8.796093022208E12/6.041526306337383E52+
1.7592186044416E13/2.6582715747884485E54+
3.5184372088832E13/1.1962222086548019E56+
7.0368744177664E13/5.5026221598120885E57+
1.40737488355328E14/2.5862324151116818E59+
2.81474976710656E14/1.2413915592536073E61+
5.62949953421312E14/6.082818640342675E62+
1.125899906842624E15/3.0414093201713376E64+
=7.389056098930649
請輸入x:
2
請輸入欲計算的項數n
100
2.0/1.0+
4.0/2.0+
8.0/6.0+
16.0/24.0+
32.0/120.0+
64.0/720.0+
128.0/5040.0+
256.0/40320.0+
512.0/362880.0+
1024.0/3628800.0+
2048.0/3.99168E7+
4096.0/4.790016E8+
8192.0/6.2270208E9+
16384.0/8.71782912E10+
32768.0/1.307674368E12+
65536.0/2.0922789888E13+
131072.0/3.55687428096E14+
262144.0/6.402373705728E15+
524288.0/1.21645100408832E17+
1048576.0/2.43290200817664E18+
2097152.0/5.109094217170944E19+
4194304.0/1.1240007277776077E21+
8388608.0/2.585201673888498E22+
1.6777216E7/6.204484017332394E23+
3.3554432E7/1.5511210043330986E25+
6.7108864E7/4.0329146112660565E26+
1.34217728E8/1.0888869450418352E28+
2.68435456E8/3.0488834461171384E29+
5.36870912E8/8.841761993739701E30+
1.073741824E9/2.6525285981219103E32+
2.147483648E9/8.222838654177922E33+
4.294967296E9/2.631308369336935E35+
8.589934592E9/8.683317618811886E36+
1.7179869184E10/2.9523279903960412E38+
3.4359738368E10/1.0333147966386144E40+
6.8719476736E10/3.719933267899012E41+
1.37438953472E11/1.3763753091226343E43+
2.74877906944E11/5.23022617466601E44+
5.49755813888E11/2.0397882081197442E46+
1.099511627776E12/8.159152832478977E47+
2.199023255552E12/3.3452526613163803E49+
4.398046511104E12/1.4050061177528798E51+
8.796093022208E12/6.041526306337383E52+
1.7592186044416E13/2.6582715747884485E54+
3.5184372088832E13/1.1962222086548019E56+
7.0368744177664E13/5.5026221598120885E57+
1.40737488355328E14/2.5862324151116818E59+
2.81474976710656E14/1.2413915592536073E61+
5.62949953421312E14/6.082818640342675E62+
1.125899906842624E15/3.0414093201713376E64+
2.251799813685248E15/1.5511187532873822E66+
4.503599627370496E15/8.065817517094388E67+
9.007199254740992E15/4.2748832840600255E69+
1.8014398509481984E16/2.308436973392414E71+
3.6028797018963968E16/1.2696403353658276E73+
7.2057594037927936E16/7.109985878048635E74+
1.44115188075855872E17/4.052691950487722E76+
2.8823037615171174E17/2.350561331282879E78+
5.7646075230342349E17/1.3868311854568986E80+
1.15292150460684698E18/8.320987112741392E81+
2.305843009213694E18/5.075802138772248E83+
4.6116860184273879E18/3.146997326038794E85+
9.223372036854776E18/1.98260831540444E87+
1.8446744073709552E19/1.2688693218588417E89+
3.6893488147419103E19/8.247650592082472E90+
7.378697629483821E19/5.443449390774431E92+
1.4757395258967641E20/3.647111091818868E94+
2.9514790517935283E20/2.4800355424368305E96+
5.9029581035870565E20/1.711224524281413E98+
1.1805916207174113E21/1.197857166996989E100+
2.3611832414348226E21/8.504785885678622E101+
4.722366482869645E21/6.123445837688608E103+
9.44473296573929E21/4.4701154615126834E105+
1.888946593147858E22/3.3078854415193856E107+
3.777893186295716E22/2.480914081139539E109+
7.555786372591432E22/1.8854947016660498E111+
1.5111572745182865E23/1.4518309202828584E113+
3.022314549036573E23/1.1324281178206295E115+
6.044629098073146E23/8.946182130782973E116+
1.2089258196146292E24/7.156945704626378E118+
2.4178516392292583E24/5.797126020747366E120+
4.8357032784585167E24/4.75364333701284E122+
9.671406556917033E24/3.945523969720657E124+
1.9342813113834067E25/3.314240134565352E126+
3.8685626227668134E25/2.8171041143805494E128+
7.737125245533627E25/2.4227095383672724E130+
1.5474250491067253E26/2.107757298379527E132+
3.0948500982134507E26/1.8548264225739836E134+
6.1897001964269014E26/1.6507955160908452E136+
1.2379400392853803E27/1.4857159644817607E138+
2.4758800785707605E27/1.3520015276784023E140+
4.951760157141521E27/1.24384140546413E142+
9.903520314283042E27/1.1567725070816409E144+
1.9807040628566084E28/1.0873661566567424E146+
3.961408125713217E28/1.0329978488239052E148+
7.922816251426434E28/9.916779348709491E149+
1.5845632502852868E29/9.619275968248206E151+
3.1691265005705735E29/9.426890448883242E153+
6.338253001141147E29/9.33262154439441E155+
1.2676506002282294E30/9.33262154439441E157+
=7.389056098930649

說明:
為求for迴圈內項目的一致性,
我們將總和的第一項"1"內定為sum的初值,
使得接下來各項的分子可固定為x^n次方
而分母則為n!
近似值即由每個迴圈的結果累加而成,
並且在迴圈尾段將分母成上x,分母乘上迴圈中的i值
供下一次計算使用。

同樣地在外部增加一個重複三次的迴圈,以方便作三個n值的計算。

2.Write a program to generate the following table of arithmetic expressions

1*1=1 1*2=2 1*3=3 ... 1*9=9
2*1=2 2*2=4 2*3=6 ... 2*9=18
...
9*1=9 9*2=18 9*3=27 ... 9*9=81

程式畫面


輸出九九乘法表,
以兩個for迴圈,控制乘數和被乘數的值,分別由1遞增到9即可。

Homework 4 3/9 Console I/O

1. Do Project 4 in Chapter 2

計算通勤開銷



2. Do Project 5 in Chapter 2

計算年折舊金額



3. Do Project 6, in Chapter 2

計算代糖的安全攝取量



4. Do Project 7, in Chapter 2

計算販賣機找零程式