2007年11月30日 星期五

Lab counter

Define a class called Counter whose objects count things. An object of this class records a count that is a nonnegative integer. Include methods to set the counter to 0, to increase the count by 1, and to decrease the count by 1. Include an accessor method that returns the current count value and a method that outputs the count to the screen. Write a program to test


counter.reset();
counter.inc();
counter.inc();
counter.dec();
counter.output();

Step1. Writing Methods

插曲: value++ 、++value or value--、 --value 會有不同的結果!!


















(1) return value++ ← 先回傳直,再對 value 做 +1 的動作。
(2) return ++value ← 先對 value 做 +1 的動作,然後再回傳。 (--value) 相同
(3)可能有人會問,同樣 value 都有做 +1 或 -1 的動作,為什麼後 ++ 的最後運算完會沒有值呢?

Ans: 因為在 method 內宣告的變數為區域變數 ( 其實 Mr. Java 說: 「我沒有全域變數(global variable) !!」)。Hence,每執行完一次 method 其變數會被釋放回記憶體,換句話說,沒有記憶的能力,所以最後的 value 會為 0。當然, 先 ++ 的 value 早在它被釋放為記憶體前,就先回傳給 number 了 。

Step2. testing


















Step3.After testing...........

Class Definition 3

Do Display 4.7 (3rd, 2nd ed.) or 4.5 (1st ed.). Then use Display 4.8 to call 4.7.

Question
In Display 4.7, if the method setDate has the parameter as setDate(int month, int day, int year), what kind of changes should be made in its body of codes?

因為 setDate methods 所使用的 parameter 在之前的宣告裡已經用過(見下圖),因此在 methods 的動作 day = day or year = year .........,被視為無效。(Mr. Java 會把這兩個 variable 看作相同。 )


















解決的辦法就是,把原先的 variable 前加上 " this. " (named "calling object "),如此 Mr. Java 就懂了。


















(附) this."variable" 代表 傳入" parameter " 前的變數,用以和 " parameter " 名稱做區隔。

Homework 11/16/2007: lab class definition 2

Study Display 4.4 (2nd ed. and 3rd ed.) or Display 4.2 & Display 4.3 (1st ed.) and then
1. Comment out date.setDate(6, 17, year); by // date.setDate(6, 17, year);
2. At the next line below, add date.readInput();
3. Run the program again. Fix any problems you may encouter along the way.
4. At the last line of your program, add System.out.println(date.month);
and see what happens. Why?

根據Display 4.4原始的程式碼:


















下圖紅色框起來的地方為關鍵點!!



































------------------------------------更改後----------------------------

加入 System.out.println(date.month);

我們可以發現到程式出現錯誤。因為在之前的程式碼(紅框的地方) month 被定義為private,所以date.month無法被叫出來。


















如果把 month 改為 public 則程式就可以執行。(如下圖)

2007年11月16日 星期五

11/16 隨堂筆記

1. Class (型別)
(1) primitive: float, double, int;
(2) Scanner (Java 內建的 class)
(3) String (這是別人已經寫好的)
(4) 可視為集合名詞。( instance ← 個體(實例))
(5) 在Class下有 :
*1. attributes (members, fields),稱作-屬性

attribute:
#1. public
#2. private

*2. methods (operations)

2. 100% 物件導向程式語言(Ex: Java),不能用非物件導向的方法來撰寫。
Ex: 寫一個向量的程式

(1). C語言的寫法

innerproduct(*a, *b);

缺點是不知道 *a, *b 所接受到的值是否為 vector 的值;

(2). Java的寫法

vector a, b;

answer = a.innerProduct(b);

(以下為錯誤寫法)

int c, d;

answer = c.innerProduct(b);

answer = a.innerProduct(d);

因為 c 和 d 皆不屬於 vector 的 members。

3. Java 有執行速度較慢的缺點,但隨著科技的進步,這項缺點已慢慢有所改善。

4. Java有跨平台的優點。(Ex: C/C++ 有 windows版、Linux版,但Java沒有)

5. "A class is a Type" (Ex: Type Var Date d)

6. 繼承
當A物件的屬性和B物件的屬性相同時,就可以讓 A繼承 B。
(Ex: Toyota & Nissen)

7. "new" Operator

Date a; //宣告一個為 Date 型別的 object named "a", 但是 a 沒有被配置記憶體。

a = new Date(); ← memory allocation;

8. Java Mechine(JVM) 有garbage collectors (釋放沒有用的記憶體)的功能。

9. obj.?? (屬性or方法)
判別方式:
methods 有 "()"

Ex: obj.attribute
obj.method()

10. Java程式的撰寫看重的是一個整體性。
Ex: Mr. Java 開車子,而不是開一個引擎或輪子..........。

lab class definition

Study Display 4.1 and then do Self-Test Exercise 1.

1.Write a method called "makeItNewYears" that could be added to the class DateFirstTry in Display 4.1. The medthod "makeItNewYears" has no parameters and sets the month instance variable to "January" and the day instance variable to 1. It does not change the year instance variable.

1.紅色框框是新加入的 methods named "makeItNewYears"















2.紅色框框內表示使用新加入的 method();
















3.最後程式的執行結果

2007年11月10日 星期六

9*9

Write a program to generate the following table of arithmetic expressions

1*1=1 1*2=2 1*3=3 ... 1*9=9
2*1=2 2*2=4 2*3=6 ... 2*9=19
...
9*1=9 9*2=18 9*3=27 ... 9*9=81















※ i 和 j 位置的互換,會造成不同的排法

2007年11月9日 星期五

Average income by gender

Write a program to calculate average income by gender based on the following data, where F stands for female and M for male.

F 62,000
M 25,000
F 38,000
F 43,000
M 65,000
M 120,000
F 80,000
M 30,100

You should be able to allow users to type in a whole line such as F 80,000 followed by next line M 30,100.

Without any change made to your program, your program should be able to process a new set of data, such as follows:

M 52,000
M 35,000
F 48,000
M 33,000
F 75,000
F 110,000
F 90,000
M 30,100

Source code:





























After running:

the sum of the odd numbers or even numbers

Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 25.

Source code and answer:


Taylor polynomials: sin(x)

1. (50) Write a Java program to calculate the sin function as follows:
sin(x)=x - x 3/ 3!+ x5/ 5! - x7/7! ...

Source code:






























執行結果:

Exponential Function

Do Project 7 of Chap. 3. (2nd ed. & 3rd ed.) or Project 4 (1st ed.)

Hint: You don't have to use nested loops.

Source code:














after running
x = 2
n = 1















n = 10
















n = 50















n = 100















由程式的計算結果證明,n越大其e^x越接近精確值e^2 = 7.389056099.....