2009年6月15日 星期一

Lab Recursive method

Write a recursive method to compute Fibonacci series.

Hint:

1.
fib(n)=fib(n-1)+fib(n-2)

2.
public static long fib(int n)

------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabRecursive {
public static double fib(int n)
{
if((n==1) || (n==2))
return 1;
else
return fib(n-1)+fib(n-2);
}
    public static void main (String args[]) 
{
        int n;
n = 8;
System.out.println("F(" + n + ") = " + (int)fib(n));
for(int a=1; a<20; a++)
System.out.print((int)fib(a) + "  ");
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Factorial

Write a Java program that computes N! where N is a positive integer.

Hint:

public static long factorial(int n)

------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabFactorial {
public static double factorial(int n) {
if(n==1)
return 1;
else
return n * factorial(n-1);
}
    public static void main (String args[]) {
        int n;
n = 10;
System.out.println((int)factorial(5));
System.out.println((int)factorial(10));
System.out.println((int)factorial(15));
    }
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Hanoi Tower

The pseudocode for Hanoi Tower is as follows:

solve(N, Src, Aux, Dst)
if N is 0 return
solve(N-1, Src, Dst, Aux)
Move N from Src to Dst
solve(N-1, Aux, Src, Dst)


Write the Java program based on the pseudocode in the above.


------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabHanoiTower {
public static void move(int n, int s, int e)
{
System.out.println("move "+ n + " : " + s + " -> " + e);
}
public static void exchange(int n, int s, int m, int e)
{
Hanoi(n-1, s, e, m);
move(n, s, e);
Hanoi(n-1, m, s, e);
}
public static void Hanoi(int n, int s, int m, int e)
{
if(n==1)
move(n, s, e);
else
exchange(n, s, m, e);
}
    public static void main (String args[]) {
        int n;
n = 4;
Hanoi(n, 1, 2, 3);
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

2009年6月1日 星期一

Lab Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.
Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.


------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabModularSorting {
public static void sort (double score[])
{
int i, j;
double temp;
for(i=0; i<score.length; i++ )
for(j=0; j<score.length-1; j++)
if(score[j] < score[j+1])
{
temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}//if
}//sort
    public static void main (String args[]) {
        Scanner keyboard = new Scanner(System.in);
int i;
double[] score = new double[5];
        System.out.println("Enter 5 scores:");
for(i = 0; i < score.length; i++)
score[i] = keyboard.nextDouble();
sort(score);
for(i = 0; i < score.length; i++)
System.out.println(score[i]);
    }//main
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Array

Study Display 6.1, and then write a program that can sort numbers in ascending order.

------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabArray {
    public static void main (String args[]) {
        Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
//double[] score2 = new double[5];
int i, j;
double temp;
        System.out.println("Enter 5 scores:");
for(i = 0; i < score.length; i++)
score[i] = keyboard.nextDouble();
for(i=0; i<score.length; i++ ) {
for(j=0; j<score.length-1; j++) {
if(score[j] < score[j+1]) {
temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}//if
}//for j
}//for i
for(i = 0; i < score.length; i++)
System.out.println(score[i]);
    }//main
}
------------------------------------------------<程式碼結束>--------------------------------------
附註: