2007年12月28日 星期五

Lab: Static Method II

Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.

Main.















Complex Class.















Math Class and addition static method.

Lab Static Method

Study Display 5.2.
Using static variables and static methods to implement the class Fibonacci such that
the first call to Fibonacci.next()
returns 2, the second returns 3, and then 5, and so on.

示意圖:

variable number1, number2 are static.(記憶體位置固定)
variable Ans are not static.(記憶體位置非固定)


















class

















main

Lab Java Constructor

Use Display 4.14 to call 4.13 (2nd ed.) or
Display 4.12 to call 4.11 (1st ed.).

After you finish the above, try the following

Date birthday = new Date("Jan",1,2000);
birthday.Date("Feb",1,2000);
birthday.setDate("Feb",1,2000);
birthday=new Date("Mar",1,2000);

Source code:
1.

















2.

















3.

















4.

















5.

















6.

















7.

















8.

















main.

















(附)如果今天程式有使用到constructor,但你的程式沒有寫到constructor(),因此在主程式寫到Date object = new Date( ),此時程式就會出錯。如下圖:



Homework 12/21/2007

Design a method that can compute the vector inner product. You must define Vector class in the first place. Write a demo program to verify your program works. You should use constructors to initialize the two vectors.

Hint: The inner product is not a vector. It is a number (scalar).

Sample answer



















2007年12月14日 星期五

Homework 12-7-2007

Homework1. Define a Complex class and write an object oriented program to compute (2+3i)+(4+5i) in Java.

Source codes:

Complex class.

1."input method" This method can deal with negative numbers.

















2."add and minus methods"

















3."multiply and divide methods"

















main
1.

















2.

















Running program

















Homework 2. Show comments on your blog.

How to show comments on your blog

result:

2007年12月7日 星期五

lab Fraction equality test

Write a program to implement a method that can check whether 2 fractions are equal. You will implement a class called Fraction consisting of a numerator and a denominator. The equality test of 2 fractions should return a boolean value.

Use the following as the tests.
  • 1/2, 2/4
  • 5/6, 6/7

Hints:
Fraction f1, f2;
f1.equals(f2);



































本題的答案在此!!



















(附)最簡分數

因為要讓電腦做約分的動作,所以就要讓電腦知道什麼是〝最大公因數〞。在此我利用〝輾轉相除法〞,讓電腦知道分子、分母的最大公因數為何,進而得到最簡分數。



































主程式




































程式執行結果

最簡分數:


































判斷兩個分數是否相等

lab Fraction Addition

Write a program to implement a method that can do additions of 2 fractions. You will implement a class called Fraction consisting of a numerator and a denominator. The additions of
2 fractions should be equal to a fraction.
Use 1/2+1/3 as the test.

Hints:
Fraction f1, f2;
f1.add(f2);

1.在 methods add() 裡面增加一個 Fraction otherFraction2 用來暫存分子、分母個別相乘或相加的值。


2.

















3.執行結果

Homework 11-30-2007

1. Do Temperature Project, which is Project 7 (3rd, 2nd ed.) or Project 3 (1st ed.).
2. Do Lab Counter.

1. Write a Temperature class that has two instance variables: a temperature value(a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable(assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameter for the two instance variable, and a no-argument constructor (set to zero degrees Celsius). Include the following:

(1) two accessor methods to return the temperature, one to return the degrees Celsius, the other to return the degrees Fahrenheit, use the following formulas to write the two methods, and round to the nearest tenth of a degree:

degreesC = 5(degreesF - 32) / 9.
degreesF = (9(degreesC) /5 ) + 32.

(2)three mutator methods: one to set the value, one to set the scale(F or C), and one to set both;

(3) three comparison methods: an equals method to test whether two temperatures are equal, one method to test whether one temperature is greater than another, and one method to test whether one temperature is less than another(note that a Celsius temperature can be equal to a Fahrenheit temperature as indicated by the above formulas); and

(4) a suitable toString method.

Then write a driver program(or programs) that tests all the methods. Be sure to use each of the constructors, to include at least one true and one false case for each of the comparison methods, and to test at least the following temperature equalities:

*1. 0.0 degrees C = 32.0 degrees F.
*2. -40.0 degrees C = -40.0 degrees F.
*3. 100.0 degrees C = 212.0 degrees F.



2. My Lab Counter

2007年12月2日 星期日

11/30 隨堂筆記

1. Java dose not have global variable.

2. Java only call-by-value.

3. 傳統習慣( from Fortran)
(1) int i, j, k, l, m;
(2) double a, b, c, x, y, z;

4. parameter : 參數
argument : 引數

5. this : calling object

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.最後程式的執行結果