Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers - 2 nd exam- 授課教授:李錫智. 1. Double the Number <input type="button" value="Double the number:" onclick=“ num = parseFloat(document.getElementById('numberBox').value);

Similar presentations


Presentation on theme: "Introduction to Computers - 2 nd exam- 授課教授:李錫智. 1. Double the Number <input type="button" value="Double the number:" onclick=“ num = parseFloat(document.getElementById('numberBox').value);"— Presentation transcript:

1 Introduction to Computers - 2 nd exam- 授課教授:李錫智

2 1. Double the Number <input type="button" value="Double the number:" onclick=“ num = parseFloat(document.getElementById('numberBox').value); document.getElementById('numberBox').value = 2 * num;" />

3 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: 2 n+1

4 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.

5 2. (a) Without using functions Ans: Test_2 <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;"/>

6 <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;"/>

7 2. (b) using two functions (getx2 、 getsqrt) Ans: Test_2 function getx2() { var squ; squ=document.getElementById('textbox_A').value; squ=parseFloat(squ); squ=squ*squ; document.getElementById('textbox_B').value=squ; }

8 function getsqrt() { var squroot; squroot=document.getElementById('textbox_B').value; squroot=parseFloat(squroot); squroot=Math.sqrt(squroot); document.getElementById('textbox_A').value=squroot; }

9

10 3. Swap Page function Swap() { temp = document.getElementById('box1').value; document.getElementById('box1').value= document.getElementById('box2').value; document.getElementById('box2').value = temp; }

11 <input type="button" value="Swap Contents" onclick="Swap();" />

12 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:

13 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~n 2. 令 i=1 3. 先假定第 i 個 student 是最年輕的,往後比較出生年月 日,比第 i 個 student 年輕的便跟他交換位置,一直比 較到第 n 個 student ,則第 i 個位置就是最年輕的 student 4. i=i+1 ,如果 i=n 則結束,否則回到 (3)

14 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: 1.Sequence A 與 Sequence B 分別排成兩排,位置由 1 開始編號, i 用來表示 sequence A 的位置, j 表示 sequence B 的位置, k 表示 sequence C 的位置 ( 以 C[ k ] 代表 ) 2. 令 i=1 、 j=1 、 k=1 3. 比較 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 4. 重複 (3) ,直到 i 大於 sequence A 的最後一個位置,或是 j 大於 sequence B 的最後一個位置 5. 將另一個 sequence 剩餘的 number 直接複製到 C[ k ] 的後面

15 6. Picture Fun function ChangeImage(imgSource) { document.getElementById('faceImg').src = imgSource; } How do you feel today?

16 <input type="button" value="I feel happy" onclick="ChangeImage('happy.gif');" /> <input type="button" value="I feel sad" onclick="ChangeImage('sad.gif');" /> <input type="button" value="I feel sleepy" onclick="ChangeImage('sleepy.gif');" /> <input type="button" value="I feel angry" onclick="ChangeImage('angry.gif');" />

17 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

18 7.Suppose you want to compute the function value f(x) = x 2 + 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.

19 Ans: Test_7 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; }

20 <input type="button" value="click for f(x)" onclick="funCalc()"/>


Download ppt "Introduction to Computers - 2 nd exam- 授課教授:李錫智. 1. Double the Number <input type="button" value="Double the number:" onclick=“ num = parseFloat(document.getElementById('numberBox').value);"

Similar presentations


Ads by Google