顯示具有 LAB 標籤的文章。 顯示所有文章
顯示具有 LAB 標籤的文章。 顯示所有文章

2009年6月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

.


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)




fib(1)


fib(5)


fib(10)


Lab Factorial

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

Hint:

public static long factorial(int n)




0!的情形


1!的情形


10!的情形

2009年6月1日 星期一

"Lab Array"

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


-----
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double[] score = new double[5];
int index;
double max, temp;
System.out.println("Enter 5 Scores");
score[0] = keyboard.nextDouble();
max = score[0];
for(index = 1; index <>
score[index] = keyboard.nextDouble();
for( index = score.length-1; index >-1 ; --index){
for (int j = 0; j <>
if(score[index] > score[j]){
temp = score[j];
score[j] = score[index];
score[index] = temp;
}
}
}
System.out.println("The scores are" );
for(index = 0; index<>
System.out.println(score[index]);
}

Lab: Static Method II

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



public class ADT {
private int real, image ;
public ADT(){
}
public ADT(int real , int image) {
this.real = real;
this.image = image;
}
public ADT mult(ADT ADT2){
ADT ans = new ADT(0,0) ;
ans.real = this.real * ADT2.real + this.image * ADT2.image;
ans.image = this.image * ADT2.real + this.real * ADT2.image;
return ans;
}
public String toString(){
return real + "+" + image + "i";
}
}

-----

public class StaticMethodII {
public static void main(String[] args) {
ADT f1, f2;
f1 = new ADT(2,3);
f2 = new ADT(4,5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 x f2 = " + f1.mult(f2).toString());
}



2009年5月25日 星期一

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.



-----


public class CarParked {
private String car;  //p275 3 name改car
public CarParked( String car )
{
this.car = car;
}
public CarParked()
{
car = " No car yet. ";
}
void setCar(String car)
{
this.car = car;
}
public static CarParked outOfOrder( CarParked car )
{
car.setCar( "Torben" ) ;
return car ;
}
public String toString()
{
return car;
}

}

----

public class CarParkedDemo {
public static void main (String args[]) {
CarParked car = new CarParked( "Benz" ) ;
 
System.out.println("進入停車場的車為" + car);
 
CarParked.outOfOrder( car );
 
System.out.println("離開時的車為" + 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.




-------------
public class Complex {
int real;
int image;
public Complex()
{}
public Complex( int real , int image)
{
this.real = real;
this.image = image;
}
public static Complex add(Complex a, Complex b) {
Complex ans = new Complex();
ans.real = a.real + b.real;
ans.image = a.image + b.image;
return ans;
}
public String toString() {
return real + "+" + image + "i";
}
}

---------------
public class LabStaticMethod {
        public static void main (String args[]) {
 
 Complex f1, f2;
 f1 = new Complex(2, 3);
 f2 = new Complex(4, 5);
 
 System.out.println("f1 = " + f1.toString());
 System.out.println("f2 = " + f2.toString());
 System.out.println("f1 + f2 = " + Complex.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)



----
public class LabMath {

/**
* @param args
*/
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.2) = " + Math.ceil(3.6) );
// TODO 自動產生方法 Stub

}

}

Lab Finding the max of three numbers

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

--------

import java.util.Scanner;
public class Maximum {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入三個數字比大小");
float a = scan.nextFloat();
float b = scan.nextFloat();
float c = scan.nextFloat();
System.out.println("最大者為"+Maximum.Max(a, b, c));
}
public static float Max( float a,float b,float c )  
{
if( a > b && a > c)
return a;
else if( b > c && b >a )
return b;
else
return c;
}

}

Lab Finding the max of three numbers

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





2009年5月4日 星期一

Lab Method Overloading

依據Class definition 3,修改程式使其接受三種setDate

date1.setDate(1,2,2008);
date2.setDate("February",2, 2008);
date3.setDate(2008);



Lab Java Constructor

Write constructors in the lab Fraction Addition.





------程式碼------


public class FractionAddtion {

public static void main(String[] args)
{
Fraction f1 = new Fraction(1 ,2);
Fraction f2 = new Fraction(1 ,3);

System.out.println("f1 = " + f1.outputstring() + "\nf2 = " + f2.outputstring());
System.out.println("f1 + f2 = "+ f1.add(f2).outputstring());

}

}

---------------------------

public class Fraction {
private int numerator;
private int denominator;

public void number( int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
public String outputstring() {

return (numerator + "/" + denominator); //分數的表示形式
}

public Fraction(int x, int y) {
this.numerator = x;
this.denominator = y;
}

public Fraction add(Fraction Fraction2)
{
Fraction countOfTemp = new Fraction(numerator ,denominator);
countOfTemp.numerator = numerator*Fraction2.denominator + denominator*Fraction2.numerator;
countOfTemp.denominator = denominator*Fraction2.denominator;
//交叉相乘
return countOfTemp;
}





}

2009年4月17日 星期五

Lab Firefox Accessibility Extension

Use Firefox Accessibility Extension to check the accessibility of three sites that you visit most.
Report the summary of all the errors and warnings for each site.


中原大學:http://itouch.cycu.edu.tw/

阿榮福利味:http://azo-freeware.blogspot.com/

google:http://www.google.com.tw

2009年4月13日 星期一

lab Fraction equality test

Write a program to implement a method that can check whether 2 fractions are equal. You will implement a class called Fraction consisting of a numerator and a denominator. The equality test of 2 fractions should return a boolean value.

Use the following as the tests.

* 1/2, 2/4
* 5/6, 6/7


Hints:
Fraction f1, f2;
f1.equals(f2);





lab Fraction Addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of
2 fractions should be equal to a fraction.
Use 1/2+1/3 as the test.

Hints:
Fraction f1, f2;
f1.add(f2);


2009年3月30日 星期一

lab class definition

Study Display 4.1 and then do Self-Test Exercise 1.


2009年3月27日 星期五

Lab Cosine

Write a Java program to calculate the triangular function as follows:
Cos(x)=1 - x 2 /2!+ x 4/4!- x 6/ 6!...



要往前兩階
就乘以你要往前的那兩階的數字就好了
也就是下個數字和下下個數字....
何苦想要把奇數階層消去勒
我是笨蛋...


-------


public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double a=1,b=1,n,s=1,c = 0;
double active = 1; // 分子
double denominator = 1; // 分母
double multiple = 2 ;
System.out.println( "請輸入x" );
double x = keyboard.nextInt();
System.out.println( "請給予精確度n;n>0" );
n = keyboard.nextInt();
double result = 1.0;
for(int i = 1 ; i < n + 1 ; i ++ ){
active = active * x * x;
System.out.println( "x^: "+active );
denominator = -1*denominator * multiple * ( multiple-1 ); // 分母
System.out.println( "n^: "+denominator );
result += active / denominator;
multiple = multiple + 2;
}
System.out.println( "cos("+x+")="+ result );

// TODO code application logic here
}

}

TEMP

package javaapplication14;
import java.util.Scanner;
/**
*
* @author KFN
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double a=1,b=1,n,s=1,c = 0;
double active = 1; // 分子
double denominator = 1; // 分母
double multiple = 2 ;
double x = keyboard.nextInt();
n = keyboard.nextInt();
double result = 1.0;
for(int i = 1 ; i < n + 1 ; i ++ ){
active = active * x * x;
System.out.println( active );
denominator = denominator * multiple * ( multiple-1 ); // 分母
System.out.println( denominator );
result += active / denominator;
multiple = multiple + 2;
}
System.out.println( result );

// TODO code application logic here

2009年3月23日 星期一

Lab Fibonacci



List the first 100 numbers and the ratio of
a number to its previous number, such as 1/1 = 1, 2/1 = 2, 3/2 = 1·5, 5/3 = 1·666..., 8/5 = 1·6, 13/8 = 1·625, 21/13 = 1·61538....

Want to know more about Fibonacci number


------
超過能列出來的數字就怪怪的...
就算改成LONG也只能列到第93項...
之後會出現1.618033988749895的規律
------
import java.util.Scanner;
public class Lab0330 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
long i, a=1,b=1,n,s;
System.out.println("請問要Fibonacci數列的第幾項?");
n = keyboard.nextInt();

for(i=1; i < n-1 ; i ++ ){

s=a+b;
b=a;
a=s;
double c=(double)a/b;
System.out.println(a+"/"+b+"="+c);

}
System.out.println("上列為後項除以前項之數列");
System.out.println("第"+n+"項為"+a);

}
}

2009年3月16日 星期一

Lab Finding the max of a list of numbers

Based on your study of Display 3.8, write a code to find the max and min of a list of number.
For example, given 1,3,5, and9, the max is 9 and the min is 1.
Your program should be able to process a list of any length.



主要概念是創造一個arrary,並宣告一個變數令array會往前跑(arrary[0]變arrary[1],在這裡宣告可使用arrary[0]~[99]),也就是將每次key入的數字儲存在這個arrary中(從0開始放)

而while是繼續執行的條件
.
.
.
最後就是大ㄧ有敎過的泡沫排序法...