package assignment01;/*確保這個類在包assignment01中。 */
import java.util.ArrayList; import java.util.Arrays;
/** *該類是執行各種數學函數的方法的集合。這些方法都是靜態的。這個 *類是作業#1的一部分。 * * @author .......... * @version 2010年6月15日 */ 公共類MathLibrary {
/**
* Computes and returns the mean (average) of the numbers in the list.
*
* If the input list is empty, the returned mean is 0.0. If the list contains illegal values, the behavior of this
* method is undefined.
*
* @param list
* an ArrayList of double values
* @return the mean of the values in the list
*/
public static double mean (ArrayList<Double> list)
{
// Variables
Double sum = 0.0;
int arraySize = 0;
// Check for empty list
if (list.size()== 0)
return 0.0;
// Take sum
arraySize = list.size();
for(int i = 0; i < arraySize; i++)
{
// Check for null Double
if(list.get(i) == null)
{
System.out.println("Mean Function: Array has null element at index: " + i + ".");
return -1.0;
}
// Add element
sum += list.get(i);
}
// Return average of results
return (sum/arraySize);
}
/**
* Computes and returns the median value of the numbers in the list. If the list has an odd number of elements, the
* exact median value is returned. If the list has an even number of elements, the average of the middle two
* elements is returned.
*
* If the input list is empty, the returned mean is 0.0. If the list contains illegal values, the behavior of this
* method is undefined.
*
* The order of the elements in the input array may be changed by this method.
*
* @param arr
* an array of double values
* @return the median of the values in the list
*/
public static double median (double[] arr)
{
// Variables
double arraySize = 0;
double arrayMedian = 0.0;
double temp = 0.0;
boolean unsorted = true;
// Check for empty array
arraySize = arr.length;
if (arraySize == 0.0)
return 0.0;
// Sort array
while(unsorted)
{
unsorted = false;
for(int i=0; i < arraySize - 1; i++)
{
if(arr[i] > arr[i+1])
{
unsorted = true;
temp = arr[i + 1];
arr[i+1] = arr[i];
arr[i] = temp;
}
}
}
// Find median
if((arraySize % 2) == 0)
{
// Take average of two middle array indicies
arrayMedian = (arr[(int) (arraySize/2.0 - 0.5)] + arr[(int) (arraySize/2.0 + 0.5)])/2.0;
} else {
arrayMedian = arr[(int) (arraySize/2.0)];
}
return arrayMedian;
}
/**
* Computes and returns the largest integer that divides (without remainder) both of the input integers (the
* greatest common divisor).
*
* If either of the input integers is not positive, this method returns -1.
*
* @param a
* any positive integer
* @param b
* any positive integer
* @return the greatest common divisor of a and b
*/
public static int gcd (int a, int b)
{
int gcd = 1;
int minimum;
// Check for (a || b) < 0
if ((a < 0) || (b < 0))
return -1;
if ((a == 0) || (b == 0))
return 0;
// Compute half the minimum(a,b)
minimum = Math.min(a,b);
// For each number below half the minimum of the two check for divisibility
for(int i = 1; i <= minimum; i++) // zero case is already accounted for
{
// Check for divisibility
if(((a % i) == 0) && ((b % i) == 0))
{
gcd = i;
}
}
return gcd;
}
/**
* Computes and returns the smallest integer that can be divided by (without remainder) both of the input integers
* (the least common multiple).
*
*
* If either of the input integers is not positive, this method returns -1. If the least common multiple exceeds the
* maximum possible integer value, the behavior of this method is undefined.
*
* @param a
* any positive integer
* @param b
* any positive integer
* @return the least common multiple of a and b
*/
public static int lcm (int a, int b)
{
// Variables
int lcm = 0;
// Check for negative numbers
if((a < 0) || (b < 0))
return -1;
// Use gcd to get lcm
lcm = (a * b)/gcd(a,b);
return lcm;
}
/**
* Given a number n, this method computes and returns the smallest prime number larger than n.
*
* If the input integer is not positive, this method returns 2. If the next prime number exceeds the maximum
* possible integer value, the behavior of this method is undefined.
*
* @param n
* an integer
* @return the smallest prime number larger than n
*/
private static boolean isPrime(int n)
{
if(n == 2 || n == 3)
{
return true;
}
if(n == 1 || n % 2 == 0)
{
return false;
}
for(int i = 3; i * i <= n; i += 2)
if(n % i == 0)
{
return false;
}
return true;
}
public static int nextPrime(int n)
{
if (n<0)
{
return 2; //if n is not positive.
}
int value = n;
if (n == 2)
value = 3; //finds the next prime number.
else
{
do
{
value += 1;
} while (!isPrime(value));
}
return value; //displays the next prime number.
}
}
看看我在同一領域的問題,也許你會發現有用的東西在那裏http://stackoverflow.com/questions/1318770/impressive-examples-in-java – Roman 2010-02-23 14:09:58