2007年10月26日 星期五

Lab Fibonacci numbers

Do Project 3.3 (1st ed.) or Project 6 (2nd ed. & 3rd ed.) Fibonacci numbers.
List the first 100 numbers and the ratio of
a number to its previous number, such as 1/1 = 1, 2/1 = 2, 3/2 = 1·5, 5/3 = 1·666..., 8/5 = 1·6, 13/8 = 1·625, 21/13 = 1·61538....

Want to know more about Fibonacci number

Source code:
















After running:





























Lab Finding the max of three numbers

Write a program to decide the max number of the three input number.


2007年10月12日 星期五

Homework 10-12-2007: Finding the max and the min

Based on your study of Display 3.8, write a code to find the max and min of a list of number.
For example, given 1,3,5, and9, the max is 9 and the min is 1.
Your program should be able to process a list of any length.

方法一
根據課本 Display 3-8:



缺點:


心得:
此程式的缺點是不能判斷小於0的數字。
因為迴圈第一次讀到小於0的數字時,就自動會跳出。

方法二

流程圖:


程式碼:


執行結果:


心得:
此程式就可以處理小於0的數字。
如果用陣列寫此程式會更簡單。
聽說 a >= 0 比 a > -1 執行速度較慢??

Lab: Tax Calculation

Study Display 3.1. Based on the income tax rate in Taiwan,
calculate the income tax of a person whose annual income is 1,000,000 or 2,000,000.


Quiz

1. Let i, j be two integers. Write a program to exchange their values. How can you show your program is correct.


2. The identifier BufferedReader is normally abbreviated as BR in programming language C. However, Java programmers normally do not use abbreviations for identifiers. What are the advantages and disadvantages of not using abbreviations?

因為C語言發展的比JAVA早,在當時記憶體很貴,所以需要用abbreviation 來減少記憶體的使用.

advantage:
讓Java 的 programmers 可以很容易的去改變記憶體的內容值。

disadvantage:
需要用到很大的記憶體。

10-5-2007 Homework

Project 1 of Chap. 2.


Project 3 of Chap. 2.

2007年10月5日 星期五

Keyborad Input

Rewrite Display 2.6 using BufferedReader.

You need to import the following packages in the first place.

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

Change

Scanner keyboard= new Scanner(System.in);

into

BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));

String inputString = keyboard.readLine();

Note the Main method needs IOException handling as follows:

public static void main (String[] args) throws IOException