星期一, 10月 31, 2005

Product

import java.io.*;
public class product
{
public static void main(String[] args)throws IOException
{
BufferedReader keyboard =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a list of number.");
System.out.println("Please enter one number one line.");
System.out.println("Enter negative number to end");
String numString = keyboard.readLine();
double num = Double.parseDouble(numString.trim());
double product=1;
while(num>=0)
{
product=num*product;
numString = keyboard.readLine();
num = Double.parseDouble(numString.trim());
}
System.out.println("Product:"+product);
System.exit(0);
}
}

Quiz

1.Leaf arrangements
The number of turns in each direction and the number of leaves met are three consecutive Fibonacci numbers!
2.Fibonacci's Rabbits
3.
Fibonacci Numbers, the Golden Section and Plants
One plant in particular shows the Fibonacci numbers in the number of "growing points" that it has. Suppose that when a plant puts out a new shoot, that shoot has to grow two months before it is strong enough to support branching. If it branches every month after that at the growing point, we get the picture shown here.

Fibonacci number

public class Fibonacci
{
public static void main(String args[])
{
double f0=0, f1=1, f2=0, ratio=0;
for (int i=1; i<=100; i++)
{
f2=f0+f1;
ratio = f2/f1;
f0= f1;
f1=f2;
System.out.println(i+"\t"+f2+"\t"+" of the ratio is "+ratio);
}
}
}

Max_Min

import javax.swing.JOptionPane;
public class Max_Min
{
public static void main (String[] args)
{
int i,j,max=0,min=0;
int num[]= new int[6];
for(i=0;i<=5;i++)
{
String number=JOptionPane.showInputDialog("Enter the "+(i+1)+" nonnegative numbers.");
num[i]= Integer.parseInt(number);
}
for(i=0;i<=5;i++)
{
max=num[i]; for(j=0;j<=5;j++)
{
if (num[j]>max)
max=num[i];
}
min=num[i];
for(j=0;j<=5;j++)
{
if (num[i]
min=num[i];
}
}
System.out.println("Max="+max);
System.out.println("Min="+min);
System.exit(0);
}
}
//還是有點錯誤,最小值還是找不出來

exponential

import javax.swing.JOptionPane;
public class exponential
{
public static void main(String[] args)
{
String inputx=JOptionPane.showInputDialog("Enter the number x of exp(x) ");
int x=Integer.parseInt(inputx);
String inputn=JOptionPane.showInputDialog("Enter a number n ");
int n=Integer.parseInt(inputn);
double i,j,k,b,a,sum=0;
for(i=0;i<=n;i++)
{
k=1;
for(j=i;j>0;j--)
k=k*j;
a=1;
for(b=i;b>0;b--)
a=a*x;
sum=sum+(a/k);
}
JOptionPane.showMessageDialog(null,"exp(x) = "+sum);
}
}

星期一, 10月 24, 2005

If_Else_2

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

星期日, 10月 23, 2005

project2_7

import javax.swing.JOptionPane;
public class project2_7
{
public static void main(String[] args)
{
String myPrice=JOptionPane.showInputDialog("Enter price of item\n"+"(from 25 cents to a dollar, in 5-cent increments):");
int price=Integer.parseInt(myPrice);
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);
}
}

project2_5


import javax.swing.JOptionPane;
public class project2_5
{
public static void main(String arg[])
{
String pString=JOptionPane.showInputDialog("Enter the purchase: ");
double myPrice = Double.parseDouble(pString);
String sString=JOptionPane.showInputDialog("Enter the salvage: ");
double myvalue = Double.parseDouble(sString);
String yString=JOptionPane.showInputDialog("Enter the number of years the item is used: ");
double myyear = Double.parseDouble(yString);
double D;
D=(myPrice-myvalue)/myyear;
System.out.println("The yearly depreciation for the item "+ D);
System.exit(0);
}
}

星期一, 10月 17, 2005

Display1.3

public class IncomeTax
{
public static void main(String[] args)
{
double netIncome=30000,tax,fivePercentTax,tenPercentTax;
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 ScannerDemo
{
public static void main(String[] args) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
String inputString = keyboard.readLine();
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
int numberOfPods=Integer.parseInt(keyboard.readLine());
int peasPerPod=Integer.parseInt(keyboard.readLine());
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 text
{
public static void main(String arg[])
{
String myString=JOptionPane.showInputDialog("Enter a number: ");
int myNumber = Integer.parseInt(myString);
System.out.println("The number is "+ myNumber);
System.exit(0);
}
}

星期一, 10月 03, 2005

Project5

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

Display1.7


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

JAVA作業三


public class HW3
{
public static void main(String[] args)
{
int running6MPH=10;
int basketball=8;
int sleeping=1;
double weight=150/2.2;
double rCal,bCal,sCal,total;
rCal=0.0175*10*weight*30;
bCal=0.0175*8*weight*30;
sCal=0.0175*1*weight*360;
total=rCal+bCal+sCal;
System.out.println("The 150-pound person is "+weight+" Kg.");
System.out.println("He runs 6MPH for 30 minutes burning "+rCal+" Calories.");
System.out.println("He plays basketball for 30 minutes burning "+bCal+" Calories.");
System.out.println("He sleeps for 6 hours burning "+sCal+" Calories.");
System.out.println("He total number of calories burned "+total+" Calories.");
}

}