introduction to computers - 2 nd exam-

20
Introduction to Computers - 2 nd exam- 授授授授 授授授

Upload: arsenio-marquez

Post on 30-Dec-2015

22 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Computers - 2 nd exam-. 授課教授:李錫智. 1. Double the Number  

TRANSCRIPT

Page 1: Introduction to Computers - 2 nd   exam-

Introduction to Computers- 2nd exam-

授課教授:李錫智

Page 2: Introduction to Computers - 2 nd   exam-

1. <html><head>

<title>Double the Number</title></head>

<body><p><input type="button" value="Double the number:" onclick=“ num =

parseFloat(document.getElementById('numberBox').value); document.getElementById('numberBox').value = 2 * num;" /></p> <p><input type="text" id="numberBox" size="10" value="2" /></p></body></html>

Page 3: Introduction to Computers - 2 nd   exam-

1. (a) If you click the button once, what is the displayed value?

Ans: 4

(b) If you click the button twice, what is the displayed value?Ans: 8

(c) If you click the button n times, what is the displayed value?Ans: 2n+1

Page 4: Introduction to Computers - 2 nd   exam-

2. Develop a web page with two buttons and two text boxes. One button, labeled by “get square”, enables the value entered from text box A to be squared and the result to be displayed in test box B. The other button, labeled by “get square root”, enables the value entered from text box B to be square-rooted and the result to be displayed in text box A. The size of each box is 4. No default values are associated with the boxes.(a) Show your program without using functions.(b) Show your program using two functions, one for

each button. The function for the first button is named as getx2 and the function for the second button is named as getsqrt.

Page 5: Introduction to Computers - 2 nd   exam-

2. (a) Without using functionsAns: <html><head>

<title>Test_2</title></head><body><p><input type="text" id="textbox_A" size="4" value="" /></p><input type="button" value="get square"

onclick="squ=document.getElementById('textbox_A').value;squ=parseFloat(squ);squ=squ*squ;document.getElementById('textbox_B').value=squ;"/>

&nbsp; &nbsp;

Page 6: Introduction to Computers - 2 nd   exam-

<input type="button" value="get square root" onclick=“ squroot= document.getElementById('textbox_B').value; squroot=parseFloat(squroot); squroot=Math.sqrt(squroot); document.getElementById('textbox_A').value=squroot;"/>

<p><input type="text" id="textbox_B" size="4" value="" /></p></body></html>

Page 7: Introduction to Computers - 2 nd   exam-

2. (b) using two functions (getx2、getsqrt)Ans: <html><head><title>Test_2</title><script type="text/javascript">function getx2(){

var squ;squ=document.getElementById('textbox_A').value;squ=parseFloat(squ);squ=squ*squ;document.getElementById('textbox_B').value=squ;

}

Page 8: Introduction to Computers - 2 nd   exam-

function getsqrt(){

var squroot;squroot=document.getElementById('textbox_B').value;squroot=parseFloat(squroot);squroot=Math.sqrt(squroot);document.getElementById('textbox_A').value=squroot;

}</script></head>

Page 9: Introduction to Computers - 2 nd   exam-

<body><p><input type="text" id="textbox_A" size="4" value="" /></p><input type="button" value="get square" onclick="getx2();"/>&nbsp; &nbsp;<input type="button" value="get square root"

onclick="getsqrt()"/><p><input type="text" id="textbox_B" size="4" value="" /></p></body></html>

Page 10: Introduction to Computers - 2 nd   exam-

3. <html><head>

<title>Swap Page</title><script type="text/javascript">

function Swap(){

temp = document.getElementById('box1').value;document.getElementById('box1').value=document.getElementById('box2').value;document.getElementById('box2').value = temp;

}</script></head>

Page 11: Introduction to Computers - 2 nd   exam-

<body><div style="text-align:center"><input type="text" id="box1" size="10" value="foobar" /><input type="text" id="box2" size="10" value="bizbaz" /><br />

<input type="button" value="Swap Contents" onclick="Swap();" /></div></body></html>

Page 12: Introduction to Computers - 2 nd   exam-

3. (a) Please show the page on the screen before any user input.Ans:

(b) What happens when the user hit the button after he/she enters 20 and 40, respectively, in text box1 and text box2?Ans:

Page 13: Introduction to Computers - 2 nd   exam-

4. Suppose that you are going to arrange the students in a class in sequence from youngest to oldest. You must organize a line that begins with the youngest person and continues in ascending order according to age. Describe an algorithm for completing this task.

Ans: 1. 假設此 sequence 有 n 個 students 排成一排,編號由 1~n2. 令 i=1 3. 先假定第 i 個 student 是最年輕的,往後比較出生年月 日,比第 i 個 student 年輕的便跟他交換位置,一直比 較到第 n 個 student ,則第 i 個位置就是最年輕的 student4. i=i+1 ,如果 i=n 則結束,否則回到 (3)

Page 14: Introduction to Computers - 2 nd   exam-

5. Suppose that you are going to merge two sequences of numbers, sequence A and sequence B, into sequence C. Both sequence A and sequence B were sorted in ascending order. It is required that sequence C be also sorted in ascending order. Describe an algorithm for completing this task.

Ans: 6. Sequence A 與 Sequence B 分別排成兩排,位置由 1 開始編號, i 用來

表示 sequence A 的位置, j 表示 sequence B 的位置, k 表示 sequence C 的位置 ( 以 C[ k ] 代表 )

7. 令 i=1 、 j=1 、 k=18. 比較 A 的第 i 個位置的值 ( 以 A[ i] 表示 ) 與 B 的第 j 個位置的值 ( 以

B[ j ] 表示 ) ,如果 A[ i ] B[ j ]≦ ,則將 A[ i ] 放入 C[ k ] ,並執行 i=i+1 與k=k+1 ,即往後移一個位置,同理如果 A[ i ]>B[ j ] ,則將 B[ j ] 放入 C[ k ] ,並執行 j=j+1 與 k=k+1

9. 重複 (3) ,直到 i 大於 sequence A 的最後一個位置,或是 j 大於sequence B 的最後一個位置

10. 將另一個 sequence 剩餘的 number 直接複製到 C[ k ] 的後面

Page 15: Introduction to Computers - 2 nd   exam-

6. <html><head><title> Picture Fun </title><script type="text/javascript">function ChangeImage(imgSource){ document.getElementById('faceImg').src = imgSource;}</script></head> <body><div style="text-align:center"><h2>How do you feel today?</h2><img id="faceImg" src="happy.gif" alt="face image" /><br /><br />

Page 16: Introduction to Computers - 2 nd   exam-

<input type="button" value="I feel happy" onclick="ChangeImage('happy.gif');" /> &nbsp; &nbsp;<input type="button" value="I feel sad" onclick="ChangeImage('sad.gif');" /> &nbsp; &nbsp;<input type="button" value="I feel sleepy" onclick="ChangeImage('sleepy.gif');" /> &nbsp; &nbsp;<input type="button" value="I feel angry" onclick="ChangeImage('angry.gif');" /></div></body></html>

Page 17: Introduction to Computers - 2 nd   exam-

6. (a) What happens if the user hits the “I feel happy” button?Ans: 如果 happy.gif 存在的話,它會顯現在 下圖紅框的

部分,否則只會顯示 face image (b) What happens if the user hits the “I feel sleepy” button?Ans: 如果 sleepy.gif 存在的話,它會顯現在 下圖紅框的

部分,否則只會顯示 face image

Page 18: Introduction to Computers - 2 nd   exam-

7. Suppose you want to compute the function value f(x) = x2 + 2x + 3. You write a web page for doing this. The value of x is entered via a text box named “box for x”. After the click of a button named “click for f(x)”, the value of x is retrieved from the text box, f(x) is computed, and the result is displayed in another text box named “box for f(x)”. It is required that a function call is used for the onclick part of the button, and the function should be named as funCalc. The size of each text box is 5 and their default value is 0. Please show your program.

Page 19: Introduction to Computers - 2 nd   exam-

Ans:<html><head><title>Test_7</title><script type="text/javascript">function funCalc(){

temp=document.getElementById('box for x').value;temp=parseFloat(temp);result=temp*temp+2*temp+3;document.getElementById('box for

f(x)').value=result;}</script></head>

Page 20: Introduction to Computers - 2 nd   exam-

<body><p><input type="text" id="box for x" size="5" value="0" /></p><input type="button" value="click for f(x)" onclick="funCalc()"/><p><input type="text" id="box for f(x)" size="5" value="0" /></p></body></html>