140 likes | 226 Views
Introduction to Computers 第二 次考試. 授課教授 : 李錫智. 第一題. [15] For the following program: <html> <head> <title> Picture Fun </title> <script type="text/ javascript "> function ChangeImage ( imgSource ) { document.getElementById (' faceImg '). src = imgSource ; } </script> </head>
E N D
Introduction to Computers第二次考試 授課教授:李錫智
第一題 [15] For the following program: <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 /> <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');" /> </div> </body> </html> (a) What image is shown if the user does not hit any button? (b) What happens if the user hits the “I feel happy” button? (c) What happens if the user hits the “I feel sleepy” button? Ans: (a) 顯示happy face (b) 顯示happy face (c) 顯示 sleepy face
第二題 [20] For the following program: <html> <head> <title>Square the Number</title> </head> <body> <p> <input type="button" value="Add the number:" onclick="num = document.getElementById('numberBox').value; document.getElementById('numberBox').value = num + 10;" /> </p> <p> <input type="text" id="numberBox" size="20" value="2" /> </p> </body> </html>
(a) Suppose you want to input a number x and get an output number x + 10 (For example, you input 25 and get 35). Can the program do it for you? Why not? (b) Please fix the program to make it work correctly. Ans: (a) 不行,因為document.getElementById('numberBox').value本身為字串,需用parseFloat轉為浮點數才可執行加法運算。 (b) <input type="button" value="Square the number:" onclick="num = parseFloat(document.getElementById('numberBox').value); document.getElementById('numberBox').value = num+10;" />
第三題 [10] Suppose that you are going to arrange the students in a class in sequence from oldest to youngest. You must organize a line that begins with the oldest person and continues in descending order according to age. Describe an algorithm for completing this task. Ans: Step1:先將所有同學排成一列。假設共有n個同學,年紀最大到最小的同學是由左排到右。 Stpe2:從右邊第一個同學往左開始比較年齡,第一個跟第二個比,比完之後第二個在跟第三個比,依此類推,如果右邊的同學比左邊的同學年紀還大則交換兩個人的位置,如果左邊同學比右邊同學還大則維持不變。 Step3:找到年紀最大的同學之後,重複step2,直到共執行n-1次step2為止。
第四題 [20] Develop a web page with two buttons and two text boxes. One button, labeled by “get double”, enables the value entered from text box A to be doubled and the result to be displayed in test box B. The other button, labeled by “get triple”, enables the value entered from text box B to be tripled 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. When the buttons are hit, the functions are called to do the desired things. The function for the first button is named as get2x and the function for the second button is named as get3x.
Ans: (a) <html> <body> <input type="button" id="button1" value="get double" onclick="document.getElementById('textB').value=2*parseFloat(document.getElementById('textA').value)" /> <input type="button" id="button1" value="get triple" onclick="document.getElementById('textA').value=3*parseFloat(document.getElementById('textB').value)" /> <br /> <input type="text" id="textA" size="4" /> <input type="text" id="textB" size="4" /> </body> </html>
Ans: (b) <html> <head> <script type="text/javascript"> function get2x() { document.getElementById('textB').value=2*parseFloat(document.getElementById('textA').value); } function get3x() { document.getElementById('textA').value=3*parseFloat(document.getElementById('textB').value); } </script></head><body> <input type="button" id="button1" value="get double" onclick="get2x()" /> <input type="button" id="button1" value="get triple" onclick="get3x()" /> <br /> <input type="text" id="textA" size="4" /> <input type="text" id="textB" size="4" /> </body></html>
第五題 [10] For the following program: <html> <head> <title>Swap Page</title> <script type="text/javascript"> function Swap() { // INSERT YOUR STATEMENTS HERE } </script></head><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 the Contents" onclick="Swap();" /> </div></body></html>
Suppose this program is to swap the contents in the two boxes after the user hits the button. Please fill in the statements you think they are required in the function Swap() as indicated above. Note that at least three statements are required to make the program work correctly. var temp; temp=document.getElementById('box1').value; document.getElementById('box1').value=document.getElementById('box2').value; document.getElementById('box2').value=temp; Ans:
第六題 [10] 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 descending order. It is required that sequence C be also sorted in descending order. Describe an algorithm for completing this task. Ans: 假設sequence A和sequence B都是由左到右從最大排到最小,先從sequence A的最大與sequence B每一個比較,如果sequence B到第k個才比sequence A的最大還小,則將sequence B的1~k-1個從左排到右,然後sequence A的最大排第k個,接著將sequence A的第2個與sequence B的第k個比較,依此類推。
第七題 [15] Suppose you want to compute the function value f(x) = 2x3 + x + 5. 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.
Ans: <html> <head> <script type="text/javascript"> function funCalc() { varx,ans; x=parseFloat(document.getElementById('text1').value); ans=2*Math.pow(x,3)+x+5; document.getElementById('text2').value=ans; } </script> </head> <body> <input type="text" name="box for x" id="text1" size="5" value="0" /> <br /> <input type="button" name="click for f(x)" id="button" value="click for f(x)" onclick="funCalc()" /> <br /> <input type="text" name="box for f(x)" id="text2" size="5" value="0" /> </body> </html>