Introduction to Computers
- 3rd exam授課教授:李錫智
1. Consider the following program:
<html> <head>
<title> Magic 8-ball</title>
<script type="text/javascript" src=http://dave-reed.com/book/random.js>
</script>
<script type="text/javascript“>
function GetResponse() {
var response;
if (document.getElementById('inputArea').value == "") {
alert(“You have to ask a question first!”); }
else {
response = RandomOneOf(["yes", "no", "definitely",
"highly unlikely“, "maybe", "reply hazy, try again"]);
document.getElementById('outputBox').value = response; }
} </script> </head>
<body>
<div style="text-align:center">
<h2>Have a Fun</h2>
<p>Enter your question in the text area below: </p>
<p>
<textarea id="inputArea" rows="5" cols="40"></textarea>
</p>
<p>
<input type="button" value="Play the Game"
onclick="GetResponse()" />
</p>
<p>
The page says:
<input type="text" name="outputBox" size="30" value="" />
</p>
</div>
</body> </html>
1-(a) What the page looks like without any user’s input?
Ans:
1-(b) What happens if the user hits the button without any other
input?
Ans:
1-(c) What happens if the user hits the button after entering “Am
I pretty?” in the text area?
Ans:
The outputBox will show one result of the RandomOneOf
function ["yes", "no", "definitely",
"highly unlikely","maybe", "reply hazy, try again"]
2. Consider the following program:
<html>
<head>
<title> Dot Race </title>
<script type=
"text/javascript" src=http://dave-reed.com/book/random.js >
</script>
<script type="text/javascript">
function DoStep()
{
var goal, redPos, bluePos;
goal = parseFloat(document.getElementById('len').value);
redPos = parseFloat(document.getElementById('redDot').value);
bluePos = parseFloat(document.getElementById('blueDot').value);
if (redPos < goal && bluePos < goal) {
redPos = redPos + RandomInt(1,3);
bluePos = bluePos + RandomInt(1,3);
document.getElementById('redDot').value = redPos;
document.getElementById('blueDot').value = bluePos;
}
if (redPos >= goal && bluePos >= goal) {
alert("It's a tie!"); }
else if (redPos >= goal) {
alert("Red Dot wins!"); }
else if (bluePos >= goal) {
alert("Blue Dot wins!"); }
}
<body>
<div style="text-align:center">
<h2>Dave's Online Dot Race</h2>
<p>
Race length: <input type="text" id="len" size=3 value="20" />
</p>
<hr />
<p>
<span style="color:red">Red Dot :</span>
<input type="text" id="redDot" size="3" value="0" />
<input type="text" id="blueDot" size="3" value="0" />
<span style="color:blue">: Blue Dot</span>
</p>
<p>
<input type="button" value="Take a Step" onclick="DoStep();" />
</p>
</div> </body> </html>
2-(a) What the page looks like without any user’s input?
Ans:
2-(b) What happens if the user continues to hit the button?
Ans: goal的預設值為20,以redPos為例,redPos會從redDot中
取值,經過運算後再存回redDot,因為使用者不斷的hit
the button,所以可以視為redPos一直累加RandomInt(1,3)
的值,同理bluePos也是如此,一直到redPos或是bluePos大
於等於goal為止,如果兩者都大於等於goal,則輸出“It’s a
tie!”,如果只有redPos≧goal,則輸出 “Red Dot wins!” ,
若只有bluePos≧goal,則輸出“Blue Dot wins!”
3. A grayscale image is one in which each pixel can be white,
or black, or various shades of gray in between. On the
other hand, a color image is one in which each pixel
contains three components, red, green, and blue.
Assuming that there are 256 discernable shades of gray.
Also, assume that there are 256 different values for red,
green, and blue, respectively. Suppose you are given an
image of 1024×1024 pixels.
(a) How many Mega bits are required for the image if it is a
grayscale one?
Ans: 1024*1024*8 bits 8 Mega bits
We use 8 bits to represent 256 discernable shades of gray
1Mega bits=1024*1024 bits
The grayscale image has 1024*1024 pixels,
Each pixel has 256 discernable shades of gray
3-(b) How many Mega bits are required for the image if it is
a color one?
Ans: 1024*1024* 3*8 bits = 24Mega bits 24 Mega bits
The Colorful image has 1024*1024 pixels each pixel has
256 different values for red, green, and blue, respectively
3-(c) How many Mega bits are required for a video of 2
hours? Suppose a video requires 30 images per second?
Ans: Assume the video is a colorful video.
2*60*60*30*24 = 5184000 Mega bits.
3-(d) How many Mega bits are required for the video if you
compress the video using Mpeg which has a compression
ratio 100:1?
Ans: 5184000/100 = 51840 Mega bits.
4. Please write a program to record the number of rolls that
occur before doubles are obtained. Please show the
number in a text box. Also, the user has to hit the button
in order to start the counting. Note that a double means
two dice having the same number.
PS. 你必須顯示在第幾次出現Double的情況
Ans:
<html>
<head>
<title> Test 4 </title>
<script type="text/javascript"
src="http://dave-reed.com/book/random.js">
</script>
<script type="text/javascript">
function twodice()
{
參考—初始值設定
var d1,d2,count;
d1=RandomInt(1,6);
d1=-1;
d2=-2;
d2=RandomInt(1,6);
count=0;
count=1;
while(d1 != d2)
{
count=count+1;
d1=RandomInt(1,6);
d2=RandomInt(1,6);
document.getElementById("OutputBox").value=
document.getElementById("OutputBox").value+"Dice1:
"+d1+"; Dice2:"+d2+"\n";
}
document.getElementById("OutputCount").value=count;
}
</script> </head>
<body>
<div style="text-align:center">
<input type="button" value="Click for starting the program"
onclick="twodice();">
<br/><hr/><br/>
The <input type="text" id="OutputCount" size="5" value="0">
round(s) occur double!<br/>
<textarea id="OutputBox" rows="20" clos="20"></textarea>
</div>
</body>
</html>
5. Predict the output sequences that would be produced by
each of the following while loops.
(a)
num = 1;
while (num >= 5 && num <= 30)
{
document.write(num + "<br />");
num = 3*num + 1;
}
document.write("DONE WITH FIRST!");
Ans: DONE WITH FIRST!
5-(b)
x = 1;
y = 30;
while (x < y) {
document.write(x + " " + y + "<br />");
x =2* x + 1;
y = y – 2;
}
document.write("DONE WITH SECOND!");
Ans:
1 30
3 28
7 26
15 24
DONE WITH SECOND!
© Copyright 2026 Paperzz