0% found this document useful (0 votes)
13 views17 pages

Mobile Programming: Random Numbers & Timer

This document provides exercises for learning mobile programming using JavaScript and Timer functions. It includes 3 exercises: 1. Generate a random number between 1-10 by clicking a button. 2. Create a guessing game that randomly generates a number between 0-9 and allows the user 3 chances to guess. 3. Create a timer that counts down from 5 seconds, updating the display every second. It also provides 2 assignments: 1. Modify the countdown timer to display time in seconds with two decimals that counts down every 0.01 seconds. 2. Create a "click as fast as you can" game where the user clicks a button to count while a timer counts down from 5 seconds
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Mobile Programming: Random Numbers & Timer

This document provides exercises for learning mobile programming using JavaScript and Timer functions. It includes 3 exercises: 1. Generate a random number between 1-10 by clicking a button. 2. Create a guessing game that randomly generates a number between 0-9 and allows the user 3 chances to guess. 3. Create a timer that counts down from 5 seconds, updating the display every second. It also provides 2 assignments: 1. Modify the countdown timer to display time in seconds with two decimals that counts down every 0.01 seconds. 2. Create a "click as fast as you can" game where the user clicks a button to count while a timer counts down from 5 seconds
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Mobile Programming Laboratory

Week 5: Random numbers and Timer


Name:[Link] moontima
Date:

ID:5631301107
Due:

Objective

To generate random numbers


To implement Timer

Exercise 1 Generate a random number

Clicking a button will generate a random number between 1-10

[Link]
<Page
loaded="load">
<StackLayout>
<Label text="0" id="lblNum"/>
<Button text="Generate"
tap="generate"/> </StackLayout>
</Page>

[Link]
Label {
}

font-size: 36; textalign: center;

Button {
font-size: 22;
}

Section:01

[Link]
var page;
var lblNum;

[Link] = function(args)
{ page = [Link];
lblNum = [Link]("lblNum");
}
[Link] = function() {
//get random number [0-1) var
num = [Link]();
//change this number to 1-10
num = [Link](num*10 +
1); [Link] = num;
}

Assignment 1 Create a guess game. The program will random a number from 0-9 and ask a
player to input a number. There are three chances for guessing.

If enter a number and press OK, the app will show whether the guessing number is too large or
too small comparing to the answer.

Clicking REPLAY will reset the chance and generate a new random answer.
[Link]
<Page loaded="load">
<StackLayout>
<TextField hint="Enter number" id="num"/>
<Label text="Let's play" id="ttt"/>
<StackLayout orientation="horizontal">
<Button text="OK" id="lblok" tap="ok" />
<Button text="RESET" id="lblreset" tap="reset" />
</StackLayout>
</StackLayout>
</Page>

[Link]
Button {
font-size: 42;
}
TextField{
font-size: 42;
}
Label{
font-size: 42;
}
#lblok{
background: green;
color: white;
}

[Link]

var page;
var num,ttt,rand,number1;
var check=3;
[Link] = function(args){
page = [Link];
num = [Link]("num");
ttt = [Link]("ttt");
rand = [Link]([Link]()*10+1);
}
[Link] = function(args){
number1 = Number([Link]());
if(check>1){
if(number1>rand){
check--;
[Link]=("more can change"+number1+" "+rand);
}
else if(number1==rand){
check=0;
[Link]=("you win");
}
else{
check--;
[Link]=("least can change"+number1+" "+rand);
}

}
else{
check=0;
[Link]=("you lose");
}
}

[Link] = function(args){
[Link] = "";
}

Exercise 2 Using Timer to run a process at the fixed time


This app waits for 3 seconds before calling a function.

After 3 seconds,

[Link]
<Page loaded="pageLoad">
<StackLayout horizontalAlignment="center"
margin="10"> <Label id="lblTime"
text="Start"/>
</StackLayout>
</Page>

[Link]
var timer = require("timer");
var lblTime;

[Link] = function(args)
{ var page = [Link];
lblTime = [Link]("lblTime");
[Link](changeText, 3000);
};
function changeText() {
[Link] = "Stop";

Exercise 3 Update the timer every a defined interval

The number will decrease every second until time is up.

[Link]
<Page

loaded="pageLoa
d">
<StackLayout>

<Label id="lblTime"
text="5"/> </StackLayout>
</Page>

[Link]
Label {
font-size: 26;
text-align: center;
margin: 10
}

[Link]
var
var
var
var

timer = require("timer");
lblTime;
timeID;
t = 5;

[Link] = function(args)
{ var page = [Link];
lblTime = [Link]("lblTime");

};

//call a function every 1 second


timeID = [Link](countdown, 1000);

function countdown() {
t--;
if(t==0) {
//stop timer
[Link](timeID);
[Link] = "Time's up";

}
else {
}

[Link] = [Link]();

Assignment 2 Modify the previous exercise to be a timer in seconds with two decimals.

Clicking START will decrease the time every 0.01 second until 0.00.

[Link]
<Page loaded="load">
<StackLayout>
<Label text="1.0" id="ttt"/>
<Button text="OK" id="lblok" tap="ok" />
</StackLayout>
</Page>

[Link]
Button {
font-size: 42;
align-content: center;
}
TextField{
font-size: 42;
}
Label{
font-size: 42;
text-align: center;
}
#lblok{
background: green;
color: white;
}

[Link]

var
var
var
var

timer = require("timer");
page;
ttt,timeID;
t=1.00;

[Link] = function(args){
page = [Link];
ttt = [Link]("ttt");
}
[Link] = function(args){
t=t-0.01;
timeID = [Link](countdown, 1000);
}
function countdown() {
var n = [Link](2);
if(n<0) {
[Link](timeID);
[Link] = "Time's up";
}
else {
[Link] = [Link]();
}
t=t-0.01;
}

Assignment 3 A click as fast as you can game.

The time will decrease every second and you can click a button CLICK to count.

When the time is up, the CLICK button is not enabled.

If you click REPLAY, everything is reset and the countdown restarts.

[Link]
<Page loaded="load">
<StackLayout>
<Label text="5" id="ttt"/>
<Label text="CLICK=0" id="lll"/>
<Button text="CLICK" id="lblok" tap="ok" />
<Button text="REPLAY" id="lblre" tap="reset" />
</StackLayout>
</Page>

[Link]
Button {
font-size: 42;
align-content: center;
}
TextField{
font-size: 42;
}
Label{
font-size: 42;
text-align: center;
}
#lblok{
background: green;
}

[Link]
var
var
var
var
var

timer = require("timer");
page;
ttt,lll,timeID;
t=5;
test=0;

[Link] = function(args){
page = [Link];
ttt = [Link]("ttt");
lll = [Link]("lll");
timeID = [Link](countdown, 1000);
}
[Link] = function(args){
if(t>0){
test++;
[Link] = ("CLICK="+[Link]());
}
}
function countdown() {
t--;

if(t==0) {
[Link](timeID);
[Link] = "Time's up";
}
else {
[Link] = [Link]();
}
}
[Link] = function(args){
if(t<5){
timeID = [Link](countdown, 1000);
}
t=5;
test=0;
[Link] = "5";
[Link] = "CLICK=0";
}

Project A Create a countdown timer.

Clicking SET will allow us to edit the initial time.

Assume that we set the time to 1 minute 5 seconds.

Then press START will start to countdown every 1 second.

11

You can press STOP during the countdown. The time stops and the button changes to SET.

When the app is counting, you can wait until the time is up and the app will allow you to set the
starting time again.

Note that the app will not accept the non-integer input.

12

[Link]

[Link]

[Link]

13

Project B Create a stopwatch.

Clicking START will begin counting every 0.01 second and this button changes to PAUSE.

Clicking PAUSE pauses the time and this button changes to RESUME.

Clicking RESUME returns to


counting. Clicking RESET resets the
time to 0. [Link]

[Link]

[Link]

14

You might also like