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
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

2009年5月25日 星期一

Lab: Static Method II

Define a Complex class with a static method for computing complex addition. Use (2+3i)*(4+5i) in your test.

class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class ADTClass {
private int realNum = 0, imaginaryNum = 0;
public ADTClass(int realNum, int imaginaryNum) {
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
public ADTClass multiplication(ADTClass f) {
ADTClass ans = new ADTClass(1, 1);
ans.realNum = this.realNum * f.realNum - this.imaginaryNum * f.imaginaryNum;
ans.imaginaryNum = this.imaginaryNum * f.realNum + f.imaginaryNum * this.realNum;
return ans;
}
public String toString() {
return realNum + "+" + imaginaryNum + "i";
}
}
------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class StaticMethodII {
    public static void main (String args[]) {
ADTClass f1, f2;
f1 = new ADTClass(2, 3);
f2 = new ADTClass(4, 5);
    System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 x f2 = " + f1.multiplication(f2).toString());
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.

class程式碼

------------------------------------------------<程式碼開始>--------------------------------------
public class CarParked {
private String name;
public CarParked(String YourCar) {
this.name = YourCar;
}
void setCar (String YourCar) {
this.name = YourCar;
}
static CarParked outOfOrder(CarParked name) {
name.setCar("Torben");
return name;
}
public String toString() {
return name;
}
}
------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class CarParkedDemo {
public static void main (String args[]) {
CarParked Car = new CarParked("Benz");
        System.out.println("parking " + Car);
CarParked.outOfOrder(Car);
System.out.println("out " + Car );
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

2009年5月11日 星期一

Lab Static Method

Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.



------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
class ADTClass {
private int realNum = 0, imaginaryNum = 0;
public ADTClass(int realNum, int imaginaryNum) {
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
public static ADTClass add(ADTClass a, ADTClass b) {
ADTClass ans = new ADTClass(1, 1);
ans.realNum = a.realNum + b.realNum;
ans.imaginaryNum = a.imaginaryNum + b.imaginaryNum;
return ans;
}
public String toString() {
return realNum + "+" + imaginaryNum + "i";
}
}
public class LabStaticMethod {
    public static void main (String args[]) {
ADTClass f1, f2;
f1 = new ADTClass(2, 3);
f2 = new ADTClass(4, 5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + ADTClass.add(f1, f2).toString());
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Math methods

Compute the following mathematical functions.

Math.round(3.2)
Math.round(3.6)
Math.floor(3.2)
Math.floor(3.6)
Math.ceil(3.2)
Math.ceil(3.6)


------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
import java.math.*;
public class LabMath {
    public static void main (String args[]) {
        System.out.println("Math.round(3.2) = " + Math.round(3.2) );
System.out.println("Math.round(3.6) = " + Math.round(3.6) );
System.out.println("Math.floor(3.2) = " + Math.floor(3.2) );
System.out.println("Math.floor(3.6) = " + Math.floor(3.6) );
System.out.println("Math.ceil(3.2) = " + Math.ceil(3.2) );
System.out.println("Math.ceil(3.6) = " + Math.ceil(3.6) );
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

Lab Finding the max of three numbers

Write a static method that computes the maximum of three float numbers.



------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
import java.util.Scanner;
public class LabFindingmax {
public static float maximun(float n1, float n2, float n3) {
if(n1 > n2 && n1 > n3)
return n1;
else if(n2 > n1 && n2 > n3)
return n2;
else
return n3;
}
    public static void main (String args[]) {
Scanner Keyboard = new Scanner(System.in);
float a, b, c; 
System.out.println("Finding the max of three numbers\nNumber 1:");
a = Keyboard.nextFloat();
System.out.println("Number 2:");
b = Keyboard.nextFloat();
System.out.println("Number 3:");
c = Keyboard.nextFloat();
System.out.println("Max = " + maximun(a, b, c) );
    }
}


------------------------------------------------<程式碼結束>--------------------------------------
附註:手動輸入三個float數,找出最大值。

2009年5月4日 星期一

Lab Java Constructor

Write constructors in the lab Fraction Addition.



class程式碼
------------------------------------------------<程式碼開始>--------------------------------------
public class Fractions {
private int numerator, denominator, red;
public Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Fractions add(Fractions f) {
int x, a, b;
Fractions ans = new Fractions(1, 1);
ans.numerator = f.denominator * this.numerator + f.numerator * this.denominator;
ans.denominator = f.denominator * this.denominator;
red = reduction (ans.numerator, ans.denominator);
ans.numerator = ans.numerator / red;
ans.denominator = ans.denominator / red;
return ans;
}
public Fractions multiply(Fractions f) {
Fractions ans = new Fractions(1, 1);
ans.numerator = f.numerator * this.numerator;
ans.denominator = f.denominator * this.denominator;
red = reduction (ans.numerator, ans.denominator);
ans.numerator = ans.numerator / red;
ans.denominator = ans.denominator / red;
return ans;
}
private int reduction(int a, int b) {
int x;
if(a<b) {
x = a;
a = b;
b = x;
}
while (b != 0) {
x = a % b;
a = b;
b = x;
}
return a;
}
public String toString()
{
return (numerator + "/" + denominator);
}
}
------------------------------------------------<程式碼結束>--------------------------------------
主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class JavaConstructor {
    public static void main (String args[]) {
Fractions f1 = new Fractions(4, 2), f2 = new Fractions(1, 3);
        
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + f1.add(f2).toString());
System.out.println("f1 * f2 = " + f1.multiply(f2).toString() + "\n");
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註: