function validateCollectPaymentInformationForm()
{
var method, name, number, expiry, errMsg
errMsg = "";
method = document.getElementById('paymentMethod');
name = document.getElementById('txtCCName');
number = document.getElementById('txtCCNumber');
if (method.value == "" || method.value == "0" || method.value == null){
errMsg = "Please select a payment method \n";
}
if (name.value == "" || name.value == null){
errMsg = errMsg + 'Please write down your name \n';
}
if (number.value == "" || number.value == null){
errMsg = errMsg + 'Please provide credit card number \n';
}else if (number.value.length < 16 ){
errMsg = errMsg + 'Invalid credit card number \n';
}
if (errMsg.length > 0 ){
alert(errMsg);
method.focus();
return false;
}else{
return true;
}
return false;
}
function validateCollectPaymentInformationForm()
{
var method, name, number, expiry, errMsg
method = document.getElementById('paymentMethod');
name = document.getElementById('txtCCName');
number = document.getElementById('txtCCNumber');
if (method.value == "" || method.value == "0" || method.value == null){
alert('Please select a payment method');
method.focus();
return false;
}
if (name.value == "" || name.value == null){
alert('Please write down your name');
name.focus();
return false;
}
if (number.value == "" || number.value == null){
alert('Please provide credit card number');
number.focus();
return false;
}else if (number.value.length < 16 ){
alert('Invalid card number');
number.focus();
return false;
}
return true;
}
Aug 29
JavaScript Form Validation: Validate a Form Collecting Credit Card Information
Aug 29
Print three-dimensional valarray line-by-line
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <valarray>
using namespace std;
// print three-dimensional valarray line-by-line
template<class T>
void printValarray3D (const valarray<T>& va, int dim1, int dim2)
{
for (int i=0; i<va.size()/(dim1*dim2); ++i) {
for (int j=0; j<dim2; ++j) {
for (int k=0; k<dim1; ++k) {
cout << va[i*dim1*dim2+j*dim1+k] << ' ';
}
cout << '\n';
}
cout << '\n';
}
cout << endl;
}
int main()
{
/* valarray with 24 elements
* - two groups
* - four rows
* - three columns
*/
valarray<double> va(24);
// fill valarray with values
for (int i=0; i<24; i++) {
va[i] = i;
}
// print valarray
printValarray3D (va, 3, 4);
// we need two two-dimensional subsets of three times 3 values
// in two 12-element arrays
size_t lengthvalues[] = { 2, 3 };
size_t stridevalues[] = { 12, 3 };
valarray<size_t> length(lengthvalues,2);
valarray<size_t> stride(stridevalues,2);
// assign the second column of the first three rows
// to the first column of the first three rows
va[gslice(0,length,stride)]
= valarray<double>(va[gslice(1,length,stride)]);
// add and assign the third of the first three rows
// to the first of the first three rows
va[gslice(0,length,stride)]
+= valarray<double>(va[gslice(2,length,stride)]);
// print valarray
printValarray3D (va, 3, 4);
}
/*
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 20
21 22 23
3 1 2
9 4 5
15 7 8
9 10 11
27 13 14
33 16 17
39 19 20
21 22 23
*/
Aug 29
C++ Template Example
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
using namespace std;
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << ' ';
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
// return whether the second object has double the value of the first
bool doubled (int elem1, int elem2)
{
return elem1 * 2 == elem2;
}
int main()
{
vector<int> coll;
coll.push_back(1);
coll.push_back(3);
coll.push_back(2);
coll.push_back(4);
coll.push_back(5);
coll.push_back(5);
coll.push_back(0);
PRINT_ELEMENTS(coll,"coll: ");
// search first two elements with equal value
vector<int>::iterator pos;
pos = adjacent_find (coll.begin(), coll.end());
if (pos != coll.end()) {
cout << "first two elements with equal value have position "
<< distance(coll.begin(),pos) + 1
<< endl;
}
}
/*
coll: 1 3 2 4 5 5 0
first two elements with equal value have position 5
*/
Aug 29
Javascript : Miscellaneous Code
var browser=navigator.appName;
var b_version=navigator.appVersion;
<a href="http://www.justetc.net" target="_blank">
<img border="0" alt="hello" src="b_pink.gif" id="b1" width="26" height="26" onmouseover="mouseOver()" onmouseout="mouseOut()" />
Place the following code under script tag/in a javascript file
function mouseOver()
{
document.getElementById("b1").src ="b_blue.gif";
}
function mouseOut()
{
document.getElementById("b1").src ="b_pink.gif";
}
<map name="planetmap">
<area shape ="rect" coords ="0,0,82,126"
onMouseOver="writeText('You are over the target area')"
href ="target.htm" target ="_blank" alt="target" />
var t1=setTimeout("document.getElementById('id1').value='2 seconds!'",2000);
var t2=setTimeout("document.getElementById('id1').value='4 seconds!'",4000);
var t3=setTimeout("document.getElementById('id1').value='6 seconds!'",6000);
Direct Instance:
pObj=new Object();
pObj.firstname="John";
pObj.lastname="Doe";
pObj.age=50;
pObj.eyecolor="blue";
document.write(pObj.firstname + " is " + pObj.age + " years
function car(brand,make,model)
{
this.brand=brand;
this.make=make;
this.model=model;
}
var myCar=new car("Honda","2009","Accord");
document.write(myCar.brand + myCar.make + myCar.model );
try
{
}
catch(err)
{
}
Under script tag/javascript file:
onerror=handleErr;
var alertTxt = ""
function handleErr(msg, url, l)
{
alertTxt = "Error Information:.\n\n";
alertTxt += msg + "\n";
alertTxt += url + "\n";
alertTxt += l + "\n\n";
alertTxt += "Click OK to continue.\n\n";
alert(alertTxt);
return true;
}
var x;
var myFriends= new Array();
myFriends[0] = "Shafiq";
myFriends[1] = "Rafiq";
myFriends[2] = "Abba";
myFriends[3] = "Amma";
for (x in myFriends)
{
document.write(myFriends[x] + "
");
}
var d = new Date();
theDay=d.getDay();
switch (theDay)
{
case 5:
document.write("Friday");
break;
case 6:
document.write("Saturday");
break;
case 0:
document.write("Sunday");
break;
}
var pattern = new RegExp("e","g");
do
{
result=pattern .exec("This is the line where the regular expression will be searched on");
document.write(result);
}
while (result!=null)
Aug 29
Example demonstrating the use of packages
&&&&&&&&&&&&&&&&&&&
Example demonstrating the use of packages.
* Class1.java defined in package1.
* Class2.java defined in package2.
* Class3.java defined in package2.package3.
* Class1.java defined in package4.
* PackageExample.java Driver for package example
&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~
Class1.java defined in package1.
~~~~~~~~~~~~~~~~~~~~~
package package1;
*****************
public class Class1 {
public static void printInfo() {
System.out.println("This is Class1 in package1.");
}
}
&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~
Class2.java defined in package2.
~~~~~~~~~~~~~~~~~~~~~
package package2;
$$$$$$$$$$$$$$$$
public class Class2 {
public static void printInfo() {
System.out.println("This is Class2 in package2.");
}
}
&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~
Class3.java defined in package2.package3
~~~~~~~~~~~~~~~~~~~~~~
package package2.package3;
@@@@@@@@@@@@@@@@@@@@@@@@@
public class Class3 {
public static void printInfo() {
System.out.println("This is Class3 in " +
"package2.package3.");
}
}
&&&&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~~~~
Class1.java defined in package4.
~~~~~~~~~~~~~~~~~~~~~~~~~
package package4;
@@@@@@@@@@@@@@@@
public class Class1 {
public static void printInfo() {
System.out.println("This is Class1 in package4.");
}
}
&&&&&&&&&&&&&&&&&&&&&&&&&&
~~~~~~~~~~~~~~~~~~~~~~~~~~
PackageExample.java Driver for package example.
~~~~~~~~~~~~~~~~~~~~~~~~~~
import package1.*;
import package2.Class2;
import package2.package3.*;
***************************
public class PackageExample {
public static void main(String[] args) {
Class1.printInfo();
Class2.printInfo();
Class3.printInfo();
package4.Class1.printInfo();
}
}
****************************
Aug 29
Code examples for interfaces
****************************
Code examples for interfaces:
* Class1.java implements Interface1.java
* Abstract Class2.java implements Interface1.java and Interface2.java
* Class3.java extends abstract class Class2.java
* Interface3.java extends Interface1.java and Interface2.java
***************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Class1.java
~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This class is not abstract, so it must provide
// implementations of method1 and method2.
public class Class1 extends SomeClass
implements Interface1 {
public ReturnType1 method1(ArgType1 arg) {
someCodeHere();
...
}
public ReturnType2 method2(ArgType2 arg) {
someCodeHere();
...
}
...
}
>>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~
Interface1.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
public interface Interface1 {
ReturnType1 method1(ArgType1 arg);
ReturnType2 method2(ArgType2 arg);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~
Abstract Class2.java implements Interface1.java and Interface2.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
Class2.java
~~~~~~~~~~~~~~~~~~~~~~~~~~
// This class is abstract, so does not have to provide
// implementations of the methods of Interface 1 and 2.
public abstract class Class2 extends SomeOtherClass
implements Interface1,
Interface2 {
...
}
>>>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~~~~
Interface2.java
~~~~~~~~~~~~~~~~~~~~~~~~~
public interface Interface2 {
ReturnType3 method3(ArgType3 arg);
}
~~~~~~~~~~~~~~~~~~~~~~~~~
# Class3.java extends abstract class Class2.java
~~~~~~~~~~~~~~~~~~~~~~~~~
Class3.java
>>>>>>>>>>>>>>>>>>>>>>>>>
// This class is not abstract, so it must provide
// implementations of method1, method2, and method3.
public class Class3 extends Class2 {
public ReturnType1 method1(ArgType1 arg) {
someCodeHere();
...
}
public ReturnType2 method2(ArgType2 arg) {
someCodeHere();
...
}
public ReturnType3 method3(ArgType3 arg) {
someCodeHere();
...
}
...
}
>>>>>>>>>>>>>>>>>>>>>>>
# Interface3.java extends Interface1.java and Interface2.java
>>>>>>>>>>>>>>>>>>>>>>>
~~~~~~~~~~~~~~~~~~~~~~
Interface3.java
~~~~~~~~~~~~~~~~~~~~~~
// This interface has three methods (by inheritance) and
// two constants.
public interface Interface3 extends Interface1,
Interface2 {
int MIN_VALUE = 0;
int MAX_VALUE = 1000;
}
<<<<<<<<<<<<<<<<<<<<<
Aug 29
A Ship class illustrating object-oriented programming concepts
************************
Ship.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test.
************************
/** Ship example to demonstrate OOP in Java.
*
* @author
* Larry Brown
* @version 2.0
*/
public class Ship {
// Instance variables
private double x=0.0, y=0.0, speed=1.0, direction=0.0;
private String name;
// Constructors
/** Build a ship with specified parameters. */
public Ship(double x, double y, double speed,
double direction, String name) {
setX(x);
setY(y);
setSpeed(speed);
setDirection(direction);
setName(name);
}
/** Build a ship with default values
* (x=0, y=0, speed=1.0, direction=0.0).
*/
public Ship(String name) {
setName(name);
}
/** Move ship one step at current speed/direction. */
public void move() {
moveInternal(1);
}
/** Move N steps. */
public void move(int steps) {
moveInternal(steps);
}
private void moveInternal(int steps) {
double angle = degreesToRadians(direction);
x = x + (double)steps * speed * Math.cos(angle);
y = y + (double)steps * speed * Math.sin(angle);
}
private double degreesToRadians(double degrees) {
return(degrees * Math.PI / 180.0);
}
/** Report location to standard output. */
public void printLocation() {
System.out.println(getName() + " is at (" + getX() +
"," + getY() + ").");
}
/** Get current X location. */
public double getX() {
return(x);
}
/** Set current X location. */
public void setX(double x) {
this.x = x;
}
/** Get current Y location. */
public double getY() {
return(y);
}
/** Set current Y location. */
public void setY(double y) {
this.y = y;
}
/** Get current speed. */
public double getSpeed() {
return(speed);
}
/** Set current speed. */
public void setSpeed(double speed) {
this.speed = speed;
}
/** Get current heading (0=East, 90=North, 180=West,
* 270=South). I.e., uses standard math angles, not
* nautical system where 0=North, 90=East, etc.
*/
public double getDirection() {
return(direction);
}
/** Set current direction (0=East, 90=North, 180=West,
* 270=South). I.e., uses standard math angles,not
* nautical system where 0=North,90=East, etc.
*/
public void setDirection(double direction) {
this.direction = direction;
}
/** Get Ship's name. Can't be modified by user. */
public String getName() {
return(name);
}
private void setName(String name) {
this.name = name;
}
}
*********************
ShipTest.java
*********************
public class ShipTest {
public static void main(String[] args) {
Ship s1 = new Ship("Ship1");
Ship s2 = new Ship(0.0, 0.0, 2.0, 135.0, "Ship2");
s1.move();
s2.move(3);
s1.printLocation();
s2.printLocation();
}
}
Aug 29
Accesses instance variables in a Ship object.
Test1.java Accesses instance variables in a Ship object.
********************************************************
// Create a class with five instance variables (fields):
// x, y, speed, direction, and name. Note that Ship1 is
// not declared "public", so it can be in the same file as
// Test1. A Java file can only contain one "public" class
// definition.
class Ship1 {
public double x, y, speed, direction;
public String name;
}
// The "driver" class containing "main".
public class Test1 {
public static void main(String[] args) {
Ship1 s1 = new Ship1();
s1.x = 0.0;
s1.y = 0.0;
s1.speed = 1.0;
s1.direction = 0.0; // East
s1.name = "Ship1";
Ship1 s2 = new Ship1();
s2.x = 0.0;
s2.y = 0.0;
s2.speed = 2.0;
s2.direction = 135.0; // Northwest
s2.name = "Ship2";
s1.x = s1.x + s1.speed
* Math.cos(s1.direction * Math.PI / 180.0);
s1.y = s1.y + s1.speed
* Math.sin(s1.direction * Math.PI / 180.0);
s2.x = s2.x + s2.speed
* Math.cos(s2.direction * Math.PI / 180.0);
s2.y = s2.y + s2.speed
* Math.sin(s2.direction * Math.PI / 180.0);
System.out.println(s1.name + " is at ("
+ s1.x + "," + s1.y + ").");
System.out.println(s2.name + " is at ("
+ s2.x + "," + s2.y + ").");
}
}
**********************
Aug 29
Application that reports all command-line arguments
******************
ShowArgs.java Application that reports all command-line arguments.
******************
*/
public class ShowArgs {
public static void main(String[] args) {
for(int i=0; i
Aug 29
DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground
DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground
************************************************************
/** Simulating dropping a ball from the top of the Washington
* Monument. The program outputs the height of the ball each
* second until the ball hits the ground.
*
*****************************************
public class DropBall {
public static void main(String[] args) {
int time = 0;
double start = 550.0, drop = 0.0;
double height = start;
while (height > 0) {
System.out.println("After " + time +
(time==1 ? " second, " : " seconds,") +
"the ball is at " + height + " feet.");
time++;
drop = freeFall(time);
height = start - drop;
}
System.out.println("Before " + time + " seconds could " +
"expire, the ball hit the ground!");
}
/** Calculate the distance in feet for an object in
* free fall.
*/
public static double freeFall (float time) {
// Gravitational constant is 32 feet per second squared
return(16.0 * time * time); // 1/2 gt^2
}
}
@@@@@@@@@@@@@@@@@@@@
