Java作業一

星期一, 10月 31, 2005

Lab 10-31 Lab (2) Fibonacci numbers

import javax.swing.JOptionPane;
public class Untitled5
{
public static void main(String[] args)
{
String PString=JOptionPane.showInputDialog("輸入一個數:");
int n=Integer.parseInt(PString);
double j=0,i=0,k=1,m=0,h=1;
while (i!=n)
{
j=h+m;
System.out.println(" 數列 "+j);
if(k==1)
{
System.out.println("ratio "+j/h);
m=j;k=2;
}
else if(k==2)
{
h=j;k=1;
System.out.println("ratio "+j/m);}
i++;
}
System.exit(0);
}
}








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

import javax.swing.JOptionPane;
public class Untitled5
{
public static void main(String[] args)
{
int x=1;
String myString = JOptionPane.showInputDialog("數入正整數(當輸入負數時結束運算):");
int n = Integer.parseInt(myString);
while(n>=0)
{
x*=n;
myString = JOptionPane.showInputDialog("數入正整數(當輸入負數時結束運算):");
n = Integer.parseInt(myString);
}
System.out.println(x);
System.exit(0);
}
}


10-31 Quiz Fibonacci

假設蜜蜂飛到第 n 號蜂巢有 f n 種方法。很明顯的,f 1 = 1,f 2 = 2 。則要飛到第 n 號一定會經過第n-1號或是第n-2號。如下圖,可得 fn = fn-1 + fn-2





費氏數列在植物界中的一個最驚人表現是:在某種向日葵的種子盤上的種子是按螺線排列,計有兩組對數螺線,一組順時針轉,一組逆時針轉。兩組螺線的條數不同,令人驚奇的是:兩組螺線的條數往往成為費氏數列中相繼的兩個。普通大小的向日葵有3455條螺線,但是,大向日葵有高達89144條的,甚至還有一個更大的向日葵有144233條螺線。菠蘿、冬菁、球花、牛眼菊和許多植物的花,更有另一樁巧事:這些花的花瓣數恰好費氏數列中的一個數;搞電子學的也許很高興知道專門設計的電路能產生斐波那契序列中的數。

星期日, 10月 30, 2005

Homework 10-24 (1) Lab exponential

import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Untitled4
{
public static void main(String[] args) throws IOException
{
String myString=JOptionPane.showInputDialog("Enter a number:");
int x= Integer.parseInt(myString);
int i=0;
double T=1,j=1;
while(i<170)
{
i+=1;j*=i;
T+=(double)(x)/j; x*=x;
if(i==10)
{
System.out.println("n=10 "+T);
}
else if(i==50)
{
System.out.println("n=50 "+T);
}
else if(i==100)
{
System.out.println("n=100 "+T);
}
}
}
}

星期一, 10月 24, 2005

Homework 10-17

Project 2.03

import javax.swing.JOptionPane;
public class Untitled2
{
public static void main(String[] args)
{
String myString=JOptionPane.showInputDialog("First number is");
int myNumber = Integer.parseInt(myString);
String myString1=JOptionPane.showInputDialog("Second number is");
int myNumber1 = Integer.parseInt(myString1);
System.out.println("First number is "+myNumber);
System.out.println("Second number is "+myNumber1);
System.out.println("-------------------");
System.out.println("Their sum is "+(myNumber+myNumber1));
System.out.println("Their difference is "+(myNumber-myNumber1));
System.out.println("Their product is "+(myNumber*myNumber1));
System.exit(0);
}
}


Project 2.05

import javax.swing.JOptionPane;
public class Untitled3
{
public static void main(String[] args)
{
String myString=JOptionPane.showInputDialog("Purchase price is ");
int P = Integer.parseInt(myString);
String myString2=JOptionPane.showInputDialog("Salvage value is ");
int S = Integer.parseInt(myString2);
String myString1=JOptionPane.showInputDialog("Years is ");
int Y = Integer.parseInt(myString1);
System.out.println("Purchase price is "+P);
System.out.println("Salvage value is "+S);
System.out.println("Years is "+Y);
System.out.println("-----------------");
double D= (double)(P-S)/Y;
System.out.println("Yearly depreciation is "+D);
System.exit(0);
}
}

星期日, 10月 23, 2005

Lab Max-Min

iimport java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Untitled2
{
public static void main(String[] args) throws IOException
{
System.out.println("要輸入幾個數字?");
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
String inputString = keyboard.readLine();
int x = Integer.parseInt(inputString);
System.out.println("輸入 "+x+" 個數字");
BufferedReader keyboard1= new BufferedReader(new InputStreamReader(System.in));
String inputString1 = keyboard1.readLine();
double x1 = Double.parseDouble(inputString1);
BufferedReader keyboard2= new BufferedReader(new InputStreamReader(System.in));
String inputString2 = keyboard2.readLine();
double x2 = Double.parseDouble(inputString2);
int i=x;
double max,min;
if(x2>x1)
{
max=x2;
min=x1;
}
else
{
max=x1;
min=x2;
}
for(i=x;i>2;i--)
{
BufferedReader keyboard3= new BufferedReader(new InputStreamReader(System.in));
String inputString3 = keyboard3.readLine();
double x3 = Double.parseDouble(inputString3);
if((x3>x1)&&(x3>x2))
max=x3;
else if((x3 min=x3;
}
System.out.println("max="+max);
System.out.println("min="+min);
}
}

Lab If-Else (2)

import javax.swing.JOptionPane;
public class Untitled1 {
public static void main(String[] args)
{
String myString=JOptionPane.showInputDialog("Enter a number:");
int n = Integer.parseInt(myString);
if(n<0){
System.out.println("n<0,n is "+n+".");
}
else if(n>=0 & n<100){
System.out.println("0<=n<100,n is "+n+".");
}
else if( 100<=n )
{
System.out.println("n>=100,n is "+n+".");
}
System.exit(0);
}
}


星期一, 10月 17, 2005

Lab If-Else

public class Untitled3 {
public static void main(String[] args)
{
double netIncome,tax,fivePercentTax,tenPercentTax;
System.out.println("Enter net income.\n"+"Do not include a dollar sign or any commas.");
netIncome=40000;
if (netIncome<=15000)
tax=0;
else if ((netIncome>15000)&&(netIncome<=30000))
tax=(0.05*(netIncome-15000));
else
{
fivePercentTax=0.05*15000;
tenPercentTax=0.10*(netIncome-30000);
tax=(fivePercentTax+tenPercentTax);
}
System.out.println("Tax due=$"+tax);
}
}

Lab Scanner

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Untitled2 {
public static void main(String[] args) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader

(System.in));
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
String inputString=keyboard.readLine();
String inputString1=keyboard.readLine();
int numberOfPods=Integer.parseInt(inputString);
int peasPerPod=Integer.parseInt(inputString1);
int totalNumberOfPeas=numberOfPods*peasPerPod;
System.out.print(numberOfPods+"pods and");
System.out.println(peasPerPod+"peas per pod.");
System.out.println("The total number of peas="+totalNumberOfPeas);
}
}

Lab JOptionPane

import javax.swing.JOptionPane;
public class Untitled1
{
public static void main(String[] args)
{
String myString=JOptionPane.showInputDialog("Enter a number:");
int myNumber = Integer.parseInt(myString);
System.out.println("The number is "+ myNumber);
}
}

星期一, 10月 03, 2005

Lab 10-03-2005

Project 5 of Chap. 1

public class untitled3
{
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 changed 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);
}
}




Display 1.7 of Chap. 1

public class untitled3
{
public static void main(String[] args)
{
String sentence = "I hate text processing!";
int position = sentence.indexOf("hate");
String ending = sentence.substring(position + "hate".length());
System.out.println("01234567890123456789012");
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 string is:");
System.out.println(sentence);
}
}

Homework3


public class homework03

{

public static void main(String[] args)

{

int Running=10 , Basketball=8 , Sleeping=1 ;

double Weight ;

Weight = 150/2.2 ;

double R_Cal , B_Cal , S_Cal , T_Cal;

R_Cal = 0.0175*Running*Weight*30;

B_Cal = 0.0175*Basketball*Weight*30;

S_Cal = 0.0175*Sleeping*Weight*60*6;

T_Cal = R_Cal+B_Cal+S_Cal ;

System.out.println("One person runs for 30 mins, plays basketball for 30 mins, sleeps for 6 hrs.");

System.out.println("Running`s Calories = " +R_Cal);

System.out.println("Basketball`s Calories = " +B_Cal);

System.out.println("Sleeping`s Calories = " +S_Cal);

System.out.println("One person burns "+T_Cal+" Calories.");

}

}