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