2005-10-31

String Comparison

********************************************************
  1. 當 = = 遇上基本型態,則為判別內容是否相等。
  2. 當 = = 遇上Obiect,則為判別兩者的Address是否相等。
********************************************************
package comparestring;
public class CompareString
{
public static void main(String[] args)
{
String s1="I come from CYCU.";
String s2="I come from cycu.";
System.out.println("s1 = "+s1+"\ns2 = "+s2+"\ns1 equals s2? \nthe answer is "+s1.equals(s2));
}
}
/*answer:F*/兩者的內容不同→F(有大小寫之分)

=====================================================

package comparestring;public class CompareString
{
public static void main(String[] args)
{
String s1="I come from CYCU.";
String s2="I come from CYCU.";
System.out.println("s1 = "+s1+"\ns2 = "+s2+"\ns1 equals s2? \nthe answer is "+s1.equals(s2));
}
}
/*answer:T*/兩者的內容相同→T(大小寫相同)

=====================================================
package comparestring;
public class comparestring
{
public static void main(String[] args)
{
String s1="I come from CYCU.";
String s2="I come from cycu.";
System.out.println("s1 = "+s1+"\ns2 = "+s2+"\ns1 equals s2? \nthe answer is "+(s1==s2));
}
}
/*answer:F*/兩者存放位置(Address)不同→F(有大小寫之分,所以存放在不同位置)

====================================================
package comparestring;
public class comparestring
{
public static void main(String[] args)
{
String s1="I come from cycu.";
String s2="I come from cycu.";
System.out.println("s1 = "+s1+"\ns2 = "+s2+"\ns1 equals s2? \nthe answer is "+(s1==s2));
}
}
/*answer:T*/兩者存放位置(Address)相同→T(皆小寫,存放在同一位置)

lab product of n positive numbers

package loop;
import javax.swing.JOptionPane;
public class loop
{
public static void main(String[] args)
{
String enter=JOptionPane.showInputDialog("Please enter a nonnegative number.\n (If you enter a negative number, it exits)");
int x = Integer.parseInt(enter);
int s=1;
while(x >= 0)
{
s=s*x;
enter=JOptionPane.showInputDialog("Please enter a nonnegative number.\n (If you enter a negative number, it exits)");

x = Integer.parseInt(enter);
}
System.out.println("the answer of producting is "+ s);

System.exit(0);
}
}

Fibonacci number

  1. 植物的外觀
  2. 人體的比例
  3. 黃金比例的矩形邊長

2005-10-30

Lab 10-31-2005 Fibonacci numbers

/*這題目數據太大,用int or long都會不夠,所以只好用double*/
package fibonacci;
public class Fibonacci
{
public static void main (String[] args)
{
double r,num1=1,num2=1,num3;
int i;
r=num2/num1;
System.out.println("1\t"+num1+"\n"+"2\t"+num2+"\tthe ratio = "+r);
for(i=1;i<=98;i++)
{
num3=num1+num2; r=num3/num2;
System.out.println((i+2)+"\t"+num3+"\tthe ratio = "+r);
num1=num2;
num2=num3;
}
}
}

Lab Max-Min

/*用當初學習C語言的邏輯觀念來寫此程式,首次在Java中使用陣列 剛開始遇到一些障礙,因為Java的使用陣列語法似乎跟C語言不太一樣 後來翻閱了一下書籍,參閱了上面的寫法,更改程式如下*/
package maxmin;
import javax.swing.JOptionPane;
public class MaxMin
{
public static void main (String[] args)
{
int i,j,n[],count,max,min;
//如果直接打上數字,ex:n[6],無法正常執行
n= new int[6];
//沒有打這一行,就無法正常執行
for(i=0;i<=5;i++)
{
String number=JOptionPane.showInputDialog("Please enter the "+(i+1)+" of 6 nunnegative numbers.");
n[i]= Integer.parseInt(number);
}
for(i=0;i<=5;i++)
{
count=0; max=n[i]; for(j=0;j<=5;j++)
{
if (max>=n[j])
count=count+1;
}
if (count==6)
System.out.println("Max="+max+"");
//當某數大於等於其他六個數時,則為Max
count=0;
min=n[i];
for(j=0;j<=5;j++)
{
if (min <= n[j]) count = count + 1;
}
if (count==6) System.out.println("Min="+min+"");
//當某數小於等於其他六個數時,則為Min
}
}
}

2005-10-28

test Posted by Picasa

2005-10-25

exponential

/*無視窗:本來想用JOptionPane的,不過電腦教室的電腦會整個卡住動不了,所以後來改寫了*/

package exponential;
import java.io.*;
//今天新學的省略方法,不過容量會變大
public class exponential
{
public static void main(String[] args) throws IOException
{
BufferedReader keyin= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the number x of e^x.");
int x = Integer.parseInt(keyin.readLine());
System.out.println("Please make the number n be :");
int n = Integer.parseInt(keyin.readLine());
/*所輸入的n值大小會影響最外層回圈的執行次數,同時影響泰勒展開式的準確度。(這裡不侷限於n=10或50或100) */
double p,q,r,s,a,sum=0;
for(p=0;p<=n;p++)
{
r=1;
//剛開始這邊忘記將r重新給定1,所以跑出的數據怪怪的
for(q=p;q>0;q--)
r=r*q;
//所得的r=p!
a=1;
//問題同r=1
for(s=p;s>0;s--)
a=a*x;
//所得的a=x的p次方
/*因為不曉得Java的指數該怎麼表示,本來用"^"試試看,但是數字不太對。剛剛一查,原來那個是XOR的意思...,山不轉路轉,改用for迴圈去等效表示指數*/
sum=sum+(a/r);
//a/r=(x^p)/p! , 再利用sum去累加
}
System.out.println("e^x = "+sum);
//顯示結果
}
}

2005-10-24

If-else 2

package ifelse2;

import javax.swing.JOptionPane;

public class IfElse2

{

public static void main (String[] args)

{

String enter=JOptionPane.showInputDialog("Please enter a number");

int number=Integer.parseInt(enter);

if(number<0)>

System.out.println("The number you enter is "+number+",and "+number+" <0.");>

else if ((number>=0)&&(number<100))>

System.out.println("The number you enter is "+number+",and "+number+" between 0 and 100 (including 0).");

else

System.out.println("The number you enter is "+number+",and "+number+" >=100.");

}

}

Project 7

/*視窗化*/

package homework4_2;
import javax.swing.JOptionPane;
public class homework4_2
{
public static void main (String[] args)
{
while(1==1)
//好像不能用while(1)

{
String price=JOptionPane.showInputDialog("Please enter the price of an item which you want to buy.");
int p = Integer.parseInt(price);
String answer=JOptionPane.showInputDialog("It is " + p + " cents. Sure?(y=1/n=0)");
//可再次確認輸入是否正確,如不正確可以重新輸入
int ans = Integer.parseInt(answer);
if (ans == 1)
{
int a=100-p;
int q=a/25;
int b=a%25;
int d=b/10;
int c=b%10;
int n=c/5;
System.out.println("You bought an item for "+p+" cents and give me a dollar, so your change is\n"+q+" quartes,\n"+d+" dimes, and\n"+n+" nickels.");
break;
}
}
}
}



Project 5

/*非視窗化*/

package homework4;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class homework4
{
public static void main (String[] args) throws IOException
{
BufferedReader keyin= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the purchase price of an item.");
int P = Integer.parseInt(keyin.readLine());
System.out.println("Please enter the expected number of years of service.");
int Y = Integer.parseInt(keyin.readLine());
System.out.println("Please enter the expected salvage value.");
int S = Integer.parseInt(keyin.readLine());

int D=(P-S)/Y;
System.out.println("The yearly depreciation for item is "+ D +".");
}
}

2005-10-18

Lab If-else

public class display3_1
{
public static void main(String[] args)
{
double netIncome, tax, fivePercentTax, tenPercentTax;

System.out.println("Enter net imcome.\n" + "Do not include a dollar sign o
r any commas.");
netIncome = 30000;

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);
//用println,後面就要用"+",不可使用變數
}
}

===顯示結果===
Enter net imcome.
Do not include a dollar sign or any commas.

Tax due = 750.0

2005-10-17

keyboard key in

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*課本原例題為import java.util.Scanner
但JBuilder不接受Scanner指令,故改成上面三項import*/
public class display
{
public static void main (String[] args) throws IOException {
//發生"例外"時,丟到IOException作例外處理(為必須輸入指令)
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
//設定keyboard為一個bufferedreader
/*原程式為Scanner keyboard = new Scanner(System.in);
但是需配合import java.util.Scanner使用,故更改為現狀*/
System.out.println("Enter the number of pods follow by");
System.out.println("the number of peas in a pod:");
int numberOfPods = Integer.parseInt(keyboard.readLine());
int peasPerPod = Integer.parseInt(keyboard.readLine());
//因為readLine()一次只能讀一行,故分成兩行寫
//keyboard.readLine()處理後為字串,呼叫轉換的指令使其改變型態為integer
/*原程式為
intput numberOfPods = keyboard.nextInt();
intput peasPerPod = keyboard.nextInt();
因"nextInt" 不能使用,故更改為現狀*/
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);
//print結果
}
}

Lab JOptionPane(Java視窗input)

/*JOptionPane可以做Input&Output等動作*/

import javax.swing.JOptionPane;
//必須先匯入此項,swing為圖像

public class yusj
{
public static void main(String[] args)
{
String firstString=JOptionPane.showInputDialog("enter a number:");
//將所輸入的資料命為叫做firstString的字串

int firstNumber=Integer.parseInt(firstString);
//將firstString的資料改成integer的型態
System.out.println(firstNumber);
//將變成integer的資料print出來
System.exit(0);
//結束執行
}
}

2005-10-03

Project5

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


The line of text to be changed is

I hate you.

I have rephrased that line to read:

I love you.

Display1.7

package yusj;
public class yusj
{
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("012344567890123456789012");
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 changed string is:");
System.out.println(sentence);

}
}

012344567890123456789012

I hate text processing!

The word "hate" starts at index 2

The changed string is:

I adore text processing!

2005-10-02

Java作業三

1. Project 1 on Page 55.
package homework3;
public class homework3
{
public static void main(String[] args)
{
double poundweight=150;
double rate=0.0175;
double item1;
double item2;
double item3;
System.out.println("A person is 150 pound = "+poundweight/2.2+" kilograms.");
item1=rate*10*30*poundweight/2.2;
System.out.println("Running 6 MPH for 30 minutes is consumming "+item1+" calories.");
item2=rate*8*30*poundweight/2.2;
System.out.println("Playing basketball for 30 minutes is consumming "+item2+" calories.");
item3=rate*1*6*60*poundweight/2.2;
System.out.println("Sleeping for 6 hours is comsumming "+item3+" calories.");
System.out.println("The total number of calories burned is "+(item1+item2+item3)+".");
}
}

顯示結果:
A person is 150 pound = 68.18181818181817 kilograms.
Running 6 MPH for 30 minutes is consumming 357.9545454545455 calories.

Playing basketball for 30 minutes is consumming 286.3636363636363 calories.
Sleeping for 6 hours is comsumming 429.54545454545456 calories.

The total number of calories burned is 1073.8636363636363.