Thursday, June 02, 2005

3.04

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;

/**
Iteratively computes e^x for a given value x, outputing values at iteration
1 to 10, 50, and 100
*/
public class Ex {

public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));

// Make a NumberFormat object that we'll use when printing values
// of n in the loop below
DecimalFormat nFormat = new DecimalFormat("000");

// Read in a number for x
System.out.println("Enter a value for x (or a blank line to quit):");
String xString = console.readLine();
while ((xString != null) && (xString.length() > 0)) {
double x = Double.parseDouble(xString);


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
double down =1,up =1 ,sum =0,i;

System.out.println("Enter a value for n :");
String nString = console.readLine();
double n = Double.parseDouble(nString);
for ( i=1;i<=n ;i++)
{
down=down*i ;
up=up*x ;
sum = sum+up/down ;
}
System.out.println( "for x=" + x + " n=" + n + " the sum is " + (sum+1));
}
// --------------------------------
// --------- END USER CODE --------
// --------------------------------


// Start over again
System.out.println(
"Enter a value for x (or a blank line to quit):");
xString = console.readLine();
}
}

Friday, May 27, 2005

5/ 27 Lab Object Variables

public class ClassParameterDemo
{
public static void main(String[] args)
{
// ToyClass anObject = new ToyClass("Mr. Cellophane", 0);
ToyClass aObject = new ToyClass("Mrs. Smith",1);
ToyClass bObject = new ToyClass("Mrs. Smith",1);
ToyClass cObject ;
cObject = aObject ;
if (aObject == bObject)
System.out.println("a to b same reference");
else
System.out.println("a to b not same reference");
if (aObject.equals(bObject))
System.out.println("a to b same constant");
else
System.out.println("a to b not same constant");
if (aObject == cObject)
System.out.println("a to c same reference");
else
System.out.println("a to c not same reference");
if (aObject.equals(cObject))
System.out.println("a to c same constant");
else
System.out.println("a to c not same constant");

// System.out.println(anObject);
// System.out.println(aObject);
// System.out.println(
// "Now we call changer with anObject as argument.");
// ToyClass.changer(anObject);
// System.out.println(anObject);
}
}

Friday, May 20, 2005

3.4 做不出

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;

/**
Iteratively computes e^x for a given value x, outputing values at iteration
1 to 10, 50, and 100
*/
public class Ex {

public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));

// Make a NumberFormat object that we'll use when printing values
// of n in the loop below
DecimalFormat nFormat = new DecimalFormat("000");

// Read in a number for x
System.out.println("Enter a value for x (or a blank line to quit):");
String xString = console.readLine();
while ((xString != null) && (xString.length() > 0)) {
double x = Double.parseDouble(xString);


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
System.out.println("input n=1 to 10, 50, and 100");
int n = Integer.parseInt(console.readLine());
int a= 1 ;
for (int nj=1 ;nj<=n ;nj++)

a=a*x/nj
y=a+b

// --------------------------------
// --------- END USER CODE --------
// --------------------------------


// Start over again
System.out.println(
"Enter a value for x (or a blank line to quit):");
xString = console.readLine();
}
}

}

黃金比例

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
Computes the amount of green crud after a given number of days and a
given starting amount.
*/
public class GreenCrud {

public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));

// Read in the initial size of the crud population
System.out.println(
"Enter initial green crud population size (in pounds), ");
System.out.println("or a blank line to quit:");
String size = console.readLine();

// Stop on blank line of EOF (e.g., if the user presses Ctrl-D)
while ((size != null) && (size.length() > 0)) {
double initialSize = Double.parseDouble(size);

// Read in the number of days
System.out.println("Enter the number of (whole) days:");
int days = Integer.parseInt(console.readLine());


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
double f0=1 ,f1=1 ,third=0;
for (int i=2;i<=100;i++)
{third=f0+f1;
f0=f1;
f1=third ;
System.out.println("f"+i+"="+f1+","+"ratio="+f1/f0);
}




// --------------------------------
// --------- END USER CODE --------
// --------------------------------


// Start again, giving the user a chance to cancel by entering
// a blank line.
System.out.println();
System.out.println(
"Enter initial green crud population size (in pounds), ");
System.out.println("or a blank line to quit:");
size = console.readLine();
}

}

}

5/20 lab 3.4 4.1 4.3兩提

public class Fibonacci
{
private static int f0 = 1;
private static int f1= 1;
private static int third ;

public static int next()
{
third=f0+f1;
f0=f1;
f1=third ;
return third ;


}
}





public class Fibonaccimain
{
public static void main(String[] args)
{
for (int i=1 ;i<10 ;i++)
System.out.println(Fibonacci.next());
}


}



A:
C:\Borland\JBuilder2005\jdk1.4\bin\javaw -classpath "C:\Documents and Settings\CYEL\jbproject\59\classes;C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\indicim.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\thaiim.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\dnsns.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\sunjce_provider.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\ldapsec.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\localedata.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\jsse.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\plugin.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\sunrsasign.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\charsets.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\rt.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\jce.jar;C:\Borland\JBuilder2005\jdk1.4\jre\javaws\javaws.jar;C:\Borland\JBuilder2005\jdk1.4\lib\tools.jar;C:\Borland\JBuilder2005\jdk1.4\lib\dt.jar;C:\Borland\JBuilder2005\jdk1.4\lib\htmlconverter.jar" Fibonaccimain
2

3

5

8

13

21

34

55

89