1 / 20

Introduction to Computers - 2 nd exam-

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);

Download Presentation

Introduction to Computers - 2 nd exam-

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. (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

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

  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;"/> <p> <input type="text" id="textbox_B" size="4" value="" /> </p> </body> </html>

  7. 2. (b) using two functions (getx2、getsqrt) Ans: <html> <head> <title>Test_2</title> <script type="text/javascript"> function getx2() { varsqu; squ=document.getElementById('textbox_A').value; squ=parseFloat(squ); squ=squ*squ; document.getElementById('textbox_B').value=squ; }

  8. function getsqrt() • { • varsquroot; • squroot=document.getElementById('textbox_B').value; • squroot=parseFloat(squroot); • squroot=Math.sqrt(squroot); • document.getElementById('textbox_A').value=squroot; • } • </script> • </head>

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

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

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

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

  15. <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 />

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

  17. (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. 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.

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

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

More Related