2011年6月7日 星期二

2011 c2期中考後的小考-題目

第一題 (30%)
請使用 java+awt套件 繪製出三角形、梯形、菱形,圖形尺寸自訂。
並利用number變數控制展示圖形,如 number=1 時展示三角形,number=2 時展示梯形、number=3 時展示菱形。
參考資料:[url]http://blog.kmol.info/?p=385[/url]

第二題 (30%)
請將下列PHP程式碼由函式形式改為類別形式。
參考資料:[url]http://blog.kmol.info/?p=376[/url]

程式碼:
1
2
3
4
5
6
7
8
9
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
<?php
$m=10;
//$m=$_GET["m"];
//$color=$_GET["color"];
$color="red";
printallH($m);
for($i=0;$i<$m-2;$i+=1)
{
printheadtailH($m);
}
printallH($m);
function printallH($m)
{
// 函式目的,由 $m 變數控制列印 H 的個數,全部列印相同的 H
for($i=1;$i<=$m;$i+=1)
{
echo "H";
}
// 印完後,跳行
echo "<br>";
}
function printheadtailH($m)
{
global $color;
// 列印只有頭尾是 H ,其餘是空白
echo "H";
for($i=1;$i<=$m-2;$i+=1)
{
// 採用 css 控制列印字元顏色,將 orange 改為 white,可以列出白色字元
echo "<span style=\"color:".$color."\">H</span>";
//echo "<span style="color:white">H</span>";
}
echo "H";
echo "<br>";
}
?>

第三題 (20%)
請將下列 java 程式碼編譯,並改成 CMSimple Plugins 且在 CMSimple 底下執行,可與第四題一起展示。
Hint:注意package drawstar,須放置在drawstar目錄底下,可自由更改。
Hint:注意檔案名稱必須為Main.java。
參考資料:[url]http://blog.kmol.info/?p=437[/url]

程式碼:
1
2
3
4
5
6
7
8
9
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package drawstar;
import java.applet.Applet;
import java.awt.*;
// for Frame Exit
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main extends Applet {
Stroke drawingStroke = new BasicStroke(2);
// static 變數才可以在各方法中共用
static String setorigx = null;
static String setorigy = null;
static String setr = null;
static String setangle = null;
static float origx,origy,r,angle;
public void init() {
// getParameter 取得字串值
setorigx = getParameter("origx");
setorigy = getParameter("origy");
setr = getParameter("r");
setangle = getParameter("angle");
if(setorigx!=null && setorigy!= null && setr!=null && setangle!= null ) {
// 轉成整數使用 Integer.parseInt()
// 轉為浮點數使用 Float.parseFloat()
origx = Integer.parseInt(setorigx);
origy = Integer.parseInt(setorigy);
r = Integer.parseInt(setr);
angle = Integer.parseInt(setangle);
} else {
// 若 peakNumber 取不到值, 則內定為 5
origx=150;
origy=150;
r=30;
angle=180;
}
}
public void paint(Graphics g) {
int i,j;
float x,y;
double temp1,temp2,temp3,temp4;
double degree=3.14159/180.0;
double[] ptx=new double[6];
double[] pty=new double[6];
double[] pttx=new double[6];
double[] ptty=new double[6];
Graphics2D graph = (Graphics2D)g;
graph.setStroke(drawingStroke);
graph.setPaint(Color.black);
for(i=0;i<6;i++) {
ptx[i]=origx+r*Math.sin(72.*i*degree+angle*degree);
pty[i]=origy+r*Math.cos(72.*i*degree+angle*degree);
temp1= Math.cos(54.*degree-72.*degree*i-angle*degree);
temp2= Math.sin(54.*degree-72.*degree*i-angle*degree);
temp3= Math.sin(18.*degree);
temp4= Math.cos(36.*degree);
pttx[i]=origx+r*temp3*temp1/temp4;
ptty[i]=origy+r*temp3*temp2/temp4;
}
for(i=0;i<5;i++) {
graph.drawLine((int)ptx[i], (int)pty[i], (int)pttx[i], (int)ptty[i]);
j=i+1;
graph.drawLine((int)pttx[i], (int)ptty[i], (int)ptx[j], (int)pty[j]);
}
}
public void stop() {
}
public void printC(Graphics g, int m, int x, int y){
Graphics2D graph = (Graphics2D)g;
int i;
for(i=0;i<m;i++) {
graph.drawString("!", x+i*10, y);
}
}
public static void main(String[] args) {
Applet applet = new Main();
Frame frm = new Frame("我的主方法(函式)框架");
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frm.setSize(100, 100);
frm.setVisible(true);
// 將 applet 加入 Frame 中
frm.add(applet);
frm.setSize(300, 300);
frm.setVisible(true);
applet.init();
}
}

第四題 (20%)
利用第三題編譯好之java程式,透過 CMSimple Plugins 控制其參數,並在 CMSimple 底下執行,可與第三題一起展示。
Hint:控制變數之名稱分別為 origx(x軸位置)、origy(y軸位置)、r(半徑)、angle(旋轉角度)。
參考資料:[url]http://blog.kmol.info/?p=437[/url]

沒有留言:

張貼留言