HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Dimensioned Numbers in Java"
Vo1 1, Issue 1, p.20

	

Listing 1

double * time     t;
float  * distance d = 60 * meter;
t = 18.3 * second;
System.out.println ("speed: "
                   + ( (d/t) / (kilometer/hour))
                   +          " kilometer/hour");
double * electricTension    tension	   = 3.5 * Volt;
double * electricResistance resistance   = 1200 * Ohm;
System.out.println ("Electric current: "
                   + ( (tension/resistance)
                     / milliAmpere )
                   + "mA");

Listing 2

double * time     t;
t = 18.3; // compile error
System.out.println ("speed: " + (d/t)); // compile error

Listing 3

package physics;
interface dimensions {

// base dimensions
dimension time		unit second;
dimension distance		unit meter;
dimension mass		unit kilogram;
dimension temperature	unit Kelvin;
dimension electricCharge	unit Coulomb;

// derived dimensions
dimension speed		= distance	/ time;
dimension acceleration	= speed		/ time;
dimension impulse		= mass		* speed;
dimension force		= mass		* acceleration;
dimension torque		= force		* distance;
dimension energy		= force		* distance;
dimension power		= energy		/ time;
dimension frequency	= 1		/ time;
dimension electricCurrent	= electricCharge	/ time;
dimension electricTension	= power		/ electricCurrent;
dimension electricResistance	= electricTension / electricCharge;

// various units
mass				gram		= kilogram / 1000;
distance		kilometer		= 1000 * meter;
time				minute		= 60 * seconds;
time				hour		= 60 * minute;
time				day		= 24 * hour;
time				week		= 7 * day;
time				milliSecond	= second / 1000;
time				microSecond	= milliSecond / 1000;
time				nanoSecond	= microSecond / 1000;
frequency		Hertz		= 1 / second; 
force			Newton		= gravityAcceleration*kg;
energy		Joule		= Newton * meter;
power			Watt		= Joule / second;
electricCurrent	Ampere		= Coulomb / second;
electricCurrent	milliAmpere	= Ampere / 1000;
electricTension	Volt		= Watt / Ampere;
electricTension	milliVolt		= Volt / 1000;
electricResistance	Ohm		= Volt / Ampere;

// constants
double*speed lightspeed = 299999.97 * kilometer / second;
double*acceleration gravityAcceleration = 9.81 * meter /
(second*second);
}
package physics.shorthand;
interface dimensions extends physics.dimensions {
distance		m	= meter;
distance		km	= kilometer;
time				s	= second;
time				ms	= milliSecond;
time				us	= microSecond;
time				ns	= nanoSecond;
force			N	= Newton;
electricTension	V	= Volt;
electricTension 	mV	= Volt/1000;
...
}

Listing 4

package finance;
interface dimensions {

// base dimensions
dimension money unit usDollar;

// non-constant units: dimensioned numbers
double*money euro		= usDollar * 1.64;
double*money dutchGuilder	= usDollar / 1.59;
}

package finance.swift;
interface dimensions extends finance.dimensions {
double*money USD = usDollar;
double*money NLG = dutchGuilder;
}

Listing 5

package computing;
interface dimensions {

// base dimensions
dimension data unit byte;

// derived dimensions
data kiloByte = 1024 * byte;
data megaByte = 1024 * kiloByte;
data gigaByte = 1024 * megaByte;
data teraByte = 1024 * gigaByte;

// non-constant units
}

package computing.oldFashioned;
interface dimensions extends computing.dimensions {
data block = 512 * byte;
}

Listing 6

package physics;
interface dimensions {
final double	second		= 1.0d;
final double	meter		= 1.0d;
final double	kilogram		= 1.0d;
final double	Coulomb		= 1.0d;
final double	Kelvin		= 1.0d;
final double	gram		= 0.001d;
final double	kilometer		= 1000.0d;
final double	minute		= 60d;
final double	hour		= 60d * minute;
...

final double lightspeed = 299999.97d * kilometer / second;
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.