JAVA

Monday, October 31, 2005

Lab 10-31 (1) product of N positive numbers

import java.io.*;
public class ananfish{
public static void main(String[] args) throws IOException {

BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number.");

String number = keyboard.readLine();
int input = Integer.parseInt(number);
int sum=1;
do
{
sum=input*sum;
System.out.println("Enter a number.");
number = keyboard.readLine();
input = Integer.parseInt(number);

}while(input>=0);
System.out.println("Answer:"+sum);
}
}

http://140.135.112.9/~u9340135/ananfish.jpg

小考

1 蝸牛殼的螺旋紋路是黃金比例也就是類似費伯那西數列的ratio值(約為1.6...)
http://imagecache2.allposters.com/images/EDL/3466.jpg
2許多花瓣的數目是黃金比例的數目例如;百合花 ...是3瓣5瓣8瓣...等
3蜜蜂家族如果男蜂是只有母親而女蜂是有雙親則一隻男蜂的家譜其比例是以黃金比例往上推論的

Sunday, October 30, 2005

Homework 10/24/2005

import javax.swing.JOptionPane;
public class exponential{public static void main(String[] args){
String nString = JOptionPane.showInputDialog("Enter the number of n:(n=10,50,100)");
int n = Integer.parseInt(nString);
String xString = JOptionPane.showInputDialog("Enter the number of x:");
int x = Integer.parseInt(xString);
double all = 1, sum1 = 1, sum2 = 1;
for(int j=1;j<=n;j++)
{sum1 = sum1 * x;
sum2 = sum2 * j;
all = all + (sum1/sum2);
}
System.out.println("1+"+ x +"/1!+"+ x +"^"+ x +"/2!+.......answer="+all);
System.exit(0);
}
}









代表圖1
代表圖2
代表圖3

Monday, October 24, 2005

Homework 10-17

17Project 2.03 (1st ed.)


import javax.swing.JOptionPane;

public class Project2_03{

public static void main(String[] args){

String PurchaseString =
JOptionPane.showInputDialog("Enter purchase of price:");
double p = Double.parseDouble(PurchaseString);
String SalvageString =
JOptionPane.showInputDialog("Enter salvage value:");
double s = Double.parseDouble(SalvageString);
String YearsOfServiceString =
JOptionPane.showInputDialog("Enter expected number of years of service:");
double y = Double.parseDouble(YearsOfServiceString);
double d = (p-s)/y;
JOptionPane.showMessageDialog(null,"The yearly depreciation =" +d);
System.exit(0);
}
}
代表圖




Project 2.05 (1st ed.)


public class Project2_05{

public static void main(String[] args){

String PriceString=JOptionPane.showInputDialog("Enter price of item\n"+"(from 25 cents to a dollar, in 5-cent increments):");
int price=Integer.parseInt(PriceString);

System.out.println("You bought an item for "+price+" cents and gave me a dollar,\n"+"so your change is");

int change = 100 - price;
int quarter=0,dime=0,nickel=0;

if(change >= 25) {
quarter = change / 25;
change = change-25*quarter;
}
if(change>=10 && change <25){
dime = change/10;
change = change-10*dime;
}
if(change>=5 && change <10){
nickel = change/5;
change = change-5*nickel;
}
if(change<0){
System.out.println("Error");
}

System.out.println(quarter+" quarters,");
System.out.println(dime+" dimes,and");
System.out.println(nickel+" nickels.");
System.exit(0);
}
}
代表圖1
代表圖2

Sunday, October 23, 2005

Fibonacci numbers

public class fb{

public static long fib(long n) {
if (n==1 || n==2)
return 1L;
else
return fib(n-1)+fib(n-2);
}

public static void main(String[] args){
int n;
double previous = 0, next = 0;
double ratio ;

for (n=1;n<=100;n++){
next = fib(n);
ratio = next/previous;
previous = next;
System.out.println(""+n+":"+fib(n));
System.out.println("ratio:"+ratio);
}


}
}

圖檔

MAXMIN

public class maxmin //*用陣列寫比較方便
{
public static void main(String[] args)

{
int i,max,min;
int a[]={23,24,67,1,90};
max=min=a[0];
System.out.println("Numbers are:");
for(i=0;iSystem.out.println("MINI Number:"+min);
}
}
圖檔

Lab If-Else (2)

public static void main(String[] args)
{
int n;

String fishString=JOptionPane.showInputDialog(" please enter a number: ");
n = Integer.parseInt(fishString);


if(n<0){ n= " +n+ ">=0 & n<100){ n= " +n+ ">=100 )
{
System.out.println("n>=100,n= " +n+ " , ");
}
System.exit(0);
}
}

圖檔

Monday, October 17, 2005

Lab10/17(三)

import javax.swing.JOptionPane;
public class Untitled1 {

public static void main(String[] args) {
double netIncome, tax, fivePercentTax, tenPercentTax;
String netIncomeString =
JOptionPane.showInputDialog("Enter net income.\n" +
"do not include a dollar sign or any commas.");
netIncome = Double.parseDouble(netIncomeString);
if (netIncome <= 15000) tax = 0; else if ((netIncome > 15000) &&amp;amp; (netIncome <= 30000) tax = (0.05 * (netIncome - 15000)); else { fivePercentTax = 0.05 * 15000; tenPercentTax = 0.10 * (netIncome - 3000); tax = (fivePercentTax + tenPercentTax); } JOptionPane.showMessageDialog(null, "Tax due=$" + tax); System.exit(0);
}
}
連結一
連結二

Lab10/17(二)

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

public class Untitled4
{

public static void main(String[] args) throws IOException
{
BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number:");
String podString = keyboard.readLine();
int numberOfPods = Integer.parseInt(podString);
System.out.println("Enter number of peas in a pod:");
String peaString = keyboard.readLine();
int peasPerPod = Integer.parseInt(peaString);

int totalNumberOfPeas= numberOfPods*peasPerPod;
System.out.println(numberOfPods +"pods and ");
System.out.println(peasPerPod +"peas per pod.");
System.out.println("number of peas="+ totalNumberOfPeas);
}
}

Lab10/17

import javax.swing.JOptionPane;

public class Untitle2{
public static void main(String[] args) {
String podString=
JOptionPane.showInputDialog("enter number a number:");
int numberOfpods=Integer.parseInt(podString);

String peaString=
JOptionPane.showInputDialog(
"enter another number of peas in a pod:");
int peasPerPod=Integer.parseInt(peaString);
int tatalNumberOfPeas=numberOfpods*peasPerPod;
JOptionPane.showMessageDialog(null,"The tatal number of peas="+tatalNumberOfPeas);
System.exit(0);


}
}

Sunday, October 09, 2005


Posted by Picasa

Monday, October 03, 2005

LAB10/3

1
public class Untitled1 {
public static void main(String[] args)
{ String sentence ="I hate processing!";
int position =sentence.indexOf("hate");
String ending= sentence.substring(position +"hate".length());
System.out.println("123456789012");
System.out.println(sentence);
System.out.println("The word \"hate\" starts at index"+position);

sentence=sentence.substring(0,position)+"adore" +ending;
System.out.println("The change is:");
System.out.println(sentence);
}
}

2

public class Untitled2 {
public static void main(String[] args)
{ String sentence ="I hate you!";
int position =sentence.indexOf("hate");
String ending= sentence.substring(position +"hate".length());
System.out.println("The line of text to be change is:");
System.out.println(sentence);
sentence=sentence.substring(0,position)+"love"+ending;
System.out.println("I have rephrased that line to read:");
System.out.println(sentence);
}
}