JAVA

2006/01/04

Lab 1-02-2006 Recursion

package hk;

import java.io.*;

public class sh {
public static double recursive(double number) {
if (number == 0)
return 0;
else if (number == 1) {
return 1;
}
else return (number * recursive(number - 1));
}

public static void main(String[] args) throws IOException {
double n;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.
in));
System.out.println("please type positive number");
String input = keyboard.readLine();
double temp = Double.parseDouble(input.trim());
n = recursive(temp);
System.out.println(temp + "!=" + n);
}
}

2006/01/03

Lab 12-26-2005 (2)


2006/01/02

Lab 1-02-2006 modular sorting

package slelectionsort;
public class QuickSort {
public static void sort(double[] a, int left, int right) {
int i, j;
double s;

if (left < right) {
s = a[left];
i = left;
j = right + 1;

while (true) {
while (i + 1 < a.length && a[++i] < s);
while (j - 1 > -1 && a[--j] > s);
if (i >= j)
break;
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}

a[left] = a[j];
a[j] = s;

sort(a, left, j - 1); // 對左邊進行遞迴
sort(a, j + 1, right); // 對右邊進行遞迴
}
}
}

--------------------------------------------------------
package slelectionsort;

public class SelectionSort {
public static void sort(double[] a, int numberUsed)
{
int index, inedxOfNextSmallest;
for(index = 0 ; index < numberUsed - 1 ; index++ )
{
inedxOfNextSmallest = inedxOfSmallest(index, a, numberUsed);
interchange(index, inedxOfNextSmallest, a);
}
}

private static int inedxOfSmallest(int startIndex, double[] a, int numberUsed)
{
double min = a[startIndex];
int indexOfMin = startIndex;
int index;
for(index = startIndex + 1 ; index < numberUsed ; index++ )
if(a[index] < min)
{
min = a[index];
indexOfMin = index;
}
return indexOfMin;
}

private static void interchange(int i, int j, double[] a)
{
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

-------------------------------------------------------
package slelectionsort;

public class SelectionSortDemo {
public static void main(String[] args)
{
double[] b = {7.7 , 8 , 5.5 , 77 , 11 , 3 , 100.5 , 4.4 , 20 , 47 , 49};
double[] a = {83.5 , 84.4 , 5.5 , 10 , 0 , 3 , 983.1 , 88.555 , 32.44 , 47.6 , 49};

System.out.println("Array(b) contents before sorting:");
for(int i = 0; i < b.length ; i++)
System.out.print(b[i] + " ");
System.out.println();

System.out.println("Array(a) contents before sorting:");
for(int i = 0; i < b.length ; i++)
System.out.print(a[i] + " ");
System.out.println();

SelectionSort.sort(b, b.length );
QuickSort.sort(a, 0, a.length-1 );

System.out.println("Sorted array(b) values:");
for(int i = 0; i < b.length ; i++ )
System.out.print(b[i] + " ");
System.out.println();

System.out.println("Sorted array(a) values:");
for(int i = 0; i < a.length ; i++ )
System.out.print(a[i] + " ");
System.out.println();
}
}

2006/01/01

Lab 12-26-2005 (1)

package lab1;

public class sh {
public static void main(String[] a)
{
System.out.println("input is: "+a[0]+" "+a[1]+" "+a[2]);
System.out.println("output is: "+a[0]+" "+a[2]+" "+a[1]);
System.out.println("the length of parameter is: "+a.length);
}
}




Homework 12-19-2005 Lab Equal Arrays

package hw;

public class equal {
public static void main(String[] args) {
int[] a = {1, 9, 6, 4, 0, 2, 1, 2};
int[] b = {1, 9, 6, 4, 0, 2, 1, 2};
System.out.println("a=1,9,6,4,0,2,1,2");
System.out.println("b=1,9,6,4,0,2,1,2");
if (equal(a, b))
System.out.println("a and b are equal ");
else
System.out.println("a and b are not equal ");
}

public static boolean equal(int a[], int b[]) {
if (a.length != b.length) {
return false;
}
else {
int i = 0;
while (i < a.length) {
if (a[i] != b[i]) {
return false;
}
i++;
}
}
return true;
}
}