星期三, 1月 04, 2006

Lab 1-02-2006 Recursion

import javax.swing.JOptionPane;
public class Recursion
{
public static void main(String[] arg)
{
String PString=JOptionPane.showInputDialog("Please enter a number n=");
int i=Integer.parseInt(PString);
System.out.println( +i+"! is "+Recursion.com(i));
System.exit(0);
}
public static int com(int i)
{
int n=1;
if (i==0)
{
return 1;
}
else if(i>0)
{
return(com(i-1)*i);
}
else return 0;
}
}

Lab 1-02-2006 modular sorting

package qq1;
public class labmodular_sorting
{
public static void main(String[] args)
{
double[] num1={7,2,-1,3,8,9,10,-3};
double[] num2;
num2=input(num1);
output(num2);
}
public static double[] input(double[] num1)
{
int a,b;
double k;
for(a=0;a < b="a+1;b" a="">num1[b])
{k=num1[a];
num1[a]=num1[b];
num1[b]=k;
}}}
return num1;
}
public static void output(double[] k)
{
System.out.print("Result:");
for(int i=0 ; i < k.length ;i++)
{
System.out.print(" "+k[i]);
}}}

結果:

-3,-1,2,3,7,8,9,10

星期一, 1月 02, 2006

LAB12-26 2005(2)

public class program{
public static void main(String [] args){
int i;if(args.length == 0){
System.out.println("No input numbers.");
}else
{
System.out.println("Output:");
for(i=(args.length-1) ; i >= 0 ; i--){
System.out.print(args[i]+" ");}}}}

星期三, 12月 28, 2005

Lab 12-26-2005 (1)

public class Yourprogram
{
public static void main(String[] arg)
{
System.out.println( arg[0] + "" + arg[2] + "" + arg[1] );
System.out.println("The length of parameter is " + arg.length);
}
}


輸入 hi ! there

結果 hi there !

12-19-2005 Lab Equal Arrays

public class untitledc {
public untitledc() {
}

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};
if (a == b)
System.out.println("a and b are equal by ==.");
else
System.out.println("a and b are not equal by ==.");

System.out.println("== only tests memory addresses.");

if (equalArrays(a, b))
System.out.println(
"a and b are equal by the equalArrays method.");
else
System.out.println(
"a and b are not equal by the equalArrays method.");

System.out.println(
"An equalArrays method is usually a more useful test.");

}

public static boolean equalArrays(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;
}
}

Lab 12-19-2005 Sorting

import java.io.*;

public class untitled {
public untitled() {
}

public static void main(String[] args) throws IOException {
double[] score = new double[5];
int index, a, b;
double x;
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter 5 numbers :");
for (index = 0; index < 5; index++) {
score[index] = Double.parseDouble(keyboard.readLine());
}
System.out.println("and the new score =");
for (a = 0; a < 5;a++) {
for(b=a+1;b<5;b++){
if (score[a] > score[b]) {
x = score[a];
score[a] = score[b];
score[b] = x;
}
}
}
for (index = 0; index < 5; index++) {
System.out.println(score[index]);
}
}
}

星期一, 12月 19, 2005

12.12homework

package q1;
public class counter1 {
public static void main(String[] args) {
counter count1 = new counter(),
count2 = new counter();
count1.increment();
count1.decrement();
count2.increment();
count2.decrement();
if (count1.equals(count2))
System.out.println("The are equal");
else
System.out.println("The are not equal");
count2.increment();
if (count1.equals(count2)) {
System.out.println("The are equal");
} else {
System.out.println("The are not equal");

}
}
}
////////////////////////////////////////////////
package q1;
public class counter {
private int count = 0;
public void resetToZero() {
count = 0;
}

public void increment() {
count++;
}

public void decrement() {
if (count <= 0)
count = 0;
else
count--;
}
public int getValue()
{
return count;
}
public void output() {
System.out.println(" The counter is " + count);
}
public boolean equals(counter otherCounter)
{
return this.count == otherCounter.count;
}
public String toString() {
return Integer.toString(count);
}
}