Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to computers Exam 2 授課教授:李錫智. Question_1 For the following program: Suppose the user enters 43 、 57 in the text boxes numberBox1 and numberBox2,

Similar presentations


Presentation on theme: "Introduction to computers Exam 2 授課教授:李錫智. Question_1 For the following program: Suppose the user enters 43 、 57 in the text boxes numberBox1 and numberBox2,"— Presentation transcript:

1 Introduction to computers Exam 2 授課教授:李錫智

2 Question_1 For the following program: Suppose the user enters 43 、 57 in the text boxes numberBox1 and numberBox2, respectively. What value will then be shown in the text box numberBox3 after the user hits the button? Add the Number <input type="button" value="Add the number:" onclick=" num1 = document.getElementById('numberBox1').value; num2 = document.getElementById('numberBox2').value; document.getElementById('numberBox3').value = num1 + num2;" />

3 Solution of Q1 Ans:4357 Because that the user type the numbers in TextBox, value of the TextBox is ‘Strings’ type. When we plus two of them, it’s equal to ‘strings_1’ + ‘strings_2’.

4 Question_2 Please design a web page that can estimate the BMI (Body Mass Index) of the user. The user enters his/her height (in meter) and weight (in kilogram) in two text boxes. After the user hits the button, the calculated BMI (BMI = weight/ height2) is shown in the third text box. A. [10%] Please develop your program without function calls. Show the resulting program. B. [10%] Please develop your program. It is required that a function call be used for the onclick part of the button. Show the resulting program.

5 Solution of Q2-A

6 Solution of Q2-B

7 Question_3 Can the program work? If it works, what does the webpage look like on the screen? If it does not work, please point out the syntax error(s) and correct the error(s). Personal Information Name =prompt("Please enter your name",""); Age =prompt("Please enter your age",""); Gender =prompt("Please enter your gender",""); document.write(" Student Name: "+ Name + " "); document.write(" Age: "+Age+" "); document.write(" Gender: " + Gender+" "); document.write(" ");

8 Solution of Q3 Ans: It’s not work. “…” and ‘…’ are a nested quotas. document.write(" Student Name: "+ Name + " "); That will be cause syntax error. We should correct it by using following descript: document.write(" Student Name: "+Name+" ");

9 Question_4 There are three classes. Five students are in Class A, and their heights are [162 183 176 179 166] cm. Six students are in Class B, and their heights are [190 152 171 177 156 184] cm. Five students are in Class C, and their height are [160 170 189 155 167] cm. Suppose that you are going to pick the five tallest students of all these classes to organize a basketball team. All students in the team should be sorted in ascending order. Please describe an algorithm for completing this task step by step.

10 Solution of Q4 Ans: 1. Class A, Class B 與 Class C 分別排成三排,位置由 1 開始編號,並且按照身高由高到矮進 行排序, i 用來表示 Class A 同學的位置, j 用來表示 Class B 同學的位置, k 用來表示 Class C 同學的位置, n 用來表示 team 的空間, ( 以 D[ n ] 代表,) 2. 令 i=1 、 j=1 、 k=1 、 n=5 3. 比較 A 的第 i 個位置的值 ( 以 A[ i] 表示 ) 、 B 的第 j 個位置的值 ( 以 B[ j ] 表示 ) 與 C 的第 k 個位置 的值 ( 以 C[ k ] 表示 ) ,比較 A[ i ] 、 B[ j ] 、 C[ k ] 最大的放入 D[n] ,假設 A[i] 最大,執行 i=i+1 、 n=n-1 , (B[j] 、 C[k] 最大同理 ) 。 4. 重複 (3) ,直到 n<1 則停止。

11 Question_5 Suppose there is a line of books sorted by book numbers in ascending order. George wants to find a book by its book number. A. [5%] Please show an algorithm of order O(n) to do the job. Please explain why it is O(n). B. [5%] Please show an algorithm of order O(log(n)) to do the job. Please explain why it is O(log(n)).

12 Solution of Q5 Ans: Q5-A:Compare the specific index number with each book in sequence until we find the book. If there are N books in this sequence, we will compare N times at most. Cost time is proportional to the amount of data, so it is O(n). Q5-B:Divide the sequence from the median into two parts and find which new sequence contains the specific book. Repeat this operating until only one book left in sequence, and it’s the answer. Every time the operating can eliminate half numbers in sequence, it means that if we want to find a specific book among N books, we should have to operate log 2 (N) times. Cost time is proportional to log 2 (N), so it is O(log(n)).

13 Question_6 For the following program: The box after answer= will show “undefined” even that you enter a number in the box following x=. Please explain why, and show how to fix this bug by only adjusting the function. function squareValue(x) { x = x*x; } x= <input type="button" value="click here for x^2" onclick=" x = parseFloat(document.getElementById('value_x').value); x = squareValue(x); document.getElementById('answerBox').value = x; " /> answer=

14 Solution of Q6 Ans: We call the function, squareValue(x), but there are neither to assign global variable nor to return value after calculating. The value of ‘x’ is assigned to ‘nothing’ so that the answer can’t be shown expertly. We just adjust the function that add ‘return x’ additionally. function squareValue(x){ x = x*x; return x; }

15 Question_7 For the following program: What will the boxes Box_1, Box_2, and Box_3 show on this webpage? function funcCalc(x) { x = 2*x; document.getElementById('Box_2').value = x; } Box1 is x = 10; document.getElementById('Box_1').value = x; Box2 is funcCalc(x); Box3 is document.getElementById('Box_3').value = x;

16 Solution of Q7 Ans: 10, 20,10 The first time we assign x = 10 so that the value of Box_1 will be 10. (Be careful that when we declare ‘x’ in, the type of ‘x’ is global variables!) The second time we call the function funcCalc(x), the value of Box_2 will be 20. (Be careful that the ‘x’ will be local variables in function().) The third time we get ‘x’ again, we have two types of ‘x’, but local variables only be read in function. We just get value of ‘x’ in type of global variable x = 10, so the value of Box_3 will be 10.

17 Question_8 Please design the webpage of a simple checkout system to help customers to check their goods from a foreign e-shop. A. (5%) Describe your algorithm briefly. B. (5%) Design a function which can convert an amount of US dollars to another amount of NT dollars. C. (10%) Please develop your program. It is required that a function call be used for the onclick part of the button. Show the resulting program..

18 Solution of Q8-A Ans: 1. Get the value of pieceBox and translate string to floating type. 2. Calculate the cost of each goods by Price*Goods_pieces. 3. Add extra fee of shipping and sum up all the cost. 4. Get the value of exchange rate from TextBox and translate to float type. 5. Calculate the cost translate USD$ to NTD$ by Total*exchangeRate 6. Show the value of total sum in check price.

19 Solution of Q8-B

20 Solution of Q8-C


Download ppt "Introduction to computers Exam 2 授課教授:李錫智. Question_1 For the following program: Suppose the user enters 43 、 57 in the text boxes numberBox1 and numberBox2,"

Similar presentations


Ads by Google