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日 星期一

[090330]

執行效能
避免做多餘動作
class 開頭大寫


Hint:

n!

i=0i n;i++

c=1

c*(i+1)



-----

c=x*c;

TEMP

import java.util.Scanner;
public class Lab0330 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double i, a=1,b=1,n,s,c;
System.out.println("請問要Fibonacci數列的第幾項?");
n = keyboard.nextInt();

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

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

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

}
}

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);

}
}

TEMP

/*
* Main.java
*
* Created on 2009年3月23日, 下午 4:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication13;

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

/** Creates a new instance of Main */

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int x, n, s;
System.out.println("Please input a value 'x'");
x = keyboard.nextInt();
if ( x < 0)
{
System.out.println( "x should > 0" );
}
System.out.println("Please input a value 'n'.");
n = keyboard.nextInt();
if ( n < 0 )
{
System.out.println( "n should > 0" );
}
do{

double ans = 0.0;
ans = tailor( x, n );
System.out.println("The answer of e^"+x+"="+ans);
System.out.println("Please input '0' to stop this program or other number to continue.");
s = keyboard.nextInt();
}while( s != 0 );


// TODO code application logic here
}
public static double tailor( int x, int n ) {

double sum = 0.0;
for( int i = 0 ; i <= n ; i++ ) {
System.out.println( "x^i:" + comp(x,i) ) ;
System.out.println( "n!:" + comp1(i) ) ;
sum = sum + comp( x, i )/comp1( i );
}
return sum;
}
public static double comp( int x , int n )
{
if ( n == 0 )
return 1.0;
double sum = 1.0;
for(int i = 0 ; i < n ; i++)
sum = sum * x;
return sum ;
}
public static double comp1( int n )
{
if(n == 0 )
return 1;

return n * comp1( n-1 );
}
}