博文

目前显示的是 八月, 2009的博文

Determine if a number is prime

Determine if a number is a prime: Public Function IsPrime(TestPrime As Long) As Boolean Dim TestNum As Long Dim TestLimit As Long ' Eliminate even numbers If TestPrime Mod 2 = 0 Then Exit Function ' Loop through ODD numbers starting with 3 TestNum = 3 TestLimit = TestPrime Do While TestLimit > TestNum If TestPrime Mod TestNum = 0 Then ' Uncomment this if you really want to know ' MsgBox "Divisible by " & TestNum Exit Function End If ' There's logic to this. Think about it. TestLimit = TestPrime \ TestNum ' Remember, we only bother to check odd numbers TestNum = TestNum + 2 Loop ' If we made it through the loop, the number is a prime. IsPrime = True End Function

Java properties and synchronize/unmodifiable collections

Properties Class:   Is a persistent Hashtable that store key-value pairs of Strings. Properties are intended to use for user accessable file of application preferences. EX of P: private Properties table; table = new properties(); table.setProperty("color","red"); table.setProperty("size","200"); Some useful method: FileOutputStream output = new FileOutputStream("props.dat"); table.store(output,"sample properties"); FileInputStream input = new FileInputStream("props.dat"); table.load(input); table.getProperty(Strint(key)); table.clear(); Synchronized Collections: EX: List list1 = new ArrayList (); List list2 = Collections.synchronizedList(list1); //static method provide a wrapper synchornized object Unmodifiable collecitons: EX: List list1 = new ArrayList (); List list2 = Collections.unmodifiableList(list1); //unmodifiable wrapper to create a collection taht offers read-only access to others 中...

FORTRAN90 allocatable arrays

Declaration: INTEGER, DIMENSION(:), ALLOCATABLE :: ages ! 1D REAL, DIMENSION(:,:), ALLOCATABLE :: speed ! 2D Allocation: READ*, isize ALLOCATE(ages(isize), STAT=ierr) IF (ierr /= 0) PRINT*, "ages : Allocation failed" ALLOCATE(speed(0:isize-1,10),STAT=ierr) IF (ierr /= 0) PRINT*, "speed : Allocation failed" the optional STAT = field reports on the success of the storage request. If the INTEGER variable ierr is zero the request was successful otherwise it failed. Deallocating Arrays IF (ALLOCATED(ages)) DEALLOCATE(ages,STAT=ierr) Note: this ALLOCATED() is an intrinic function return a logic value reporting the status of the array

FORTRAN77 Arithmetic Expressions

Note:The order of evaluation of an expression is: sub-expressions in parentheses function references exponentiation, i.e. raising to a power multiplication and division addition, subtraction, or negation. Within each of these groups evaluation proceeds from left to right, except that exponentiations are evaluated from right to left. Thus: A / B / C is equivalent to (A / B) / C whereas X ** Y ** Z is equivalent to X ** (Y ** Z) . Note: An expression does not have to be evaluated fully if its value can be determined otherwise: for example the result of: X * FUNC(G) can be determined without calling the function FUNC if X happens to be zero. This will not cause problems if you only use functions that have no side effects.(Like short-circute of ||) A Fortran system is not required to evaluate every term in a logical expression completely if its value can be determined more simply.

JAVA equals() and hashCode()

Introduction The Java super class java.lang.Object has two very important methods defined in it. They are - public boolean equals(Object obj) public int hashCode() These methods prove very important when user classes are confronted with other Java classes, when objects of such classes are added to collections etc. These two methods have become part of Sun Certified Java Programmer 1.4 exam (SCJP 1.4) objectives. This article intends to provide the necessary information about these two methods that would help the SCJP 1.4 exam aspirants. Moreover, this article hopes to help you understand the mechanism and general contracts of these two methods; irrespective of whether you are interested in taking the SCJP 1.4 exam or not. This article should help you while implementing these two methods in your own classes. public boolean equals(Object obj) This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked. The defa...

Swing repaint() problem

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.BorderLayout; public class MovingBall extends JFrame { private int ballPositionX; private int ballPositionY; private JButton controlButton; private MyDrawPanel drawArea; public MovingBall() { ballPositionX = 70; ballPositionY = 70; controlButton = new JButton("play"); controlButton.addActionListener(new ButtonHandler()); BorderLayout buttonLayout = new BorderLayout(); this.getContentPane().add(controlButton, buttonLayout.SOUTH); drawArea = new MyDrawPanel(); BorderLayout drawAreaLayout = new BorderLayout(); this.getContentPane().add(drawArea, drawAreaLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 500); this.setVisible(true); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { go(); } } private class MyDrawPanel extends JPane...