2008年1月11日 星期五

lab recursive method

Write a recursive method to compute Fibonacci series.

Hint:

fib(n)=fib(n-1)+fib(n-2)

Lab Hanoi Tower

The pseudocode for Hanoi Tower is as follows:

Solve(N, Src, Aux, Dst)
if N is 0 return
Solve(N-1, Src, Dst, Aux)
Move N from Src to Dst
Solve(N-1, Aux, Src, Dst)

期末報告(三)

第三本書:Practical Java Peter Haggar著

出版商:碁峯資訊有限公司 2003年七月

題目:區分==與equals()
每當討論Java的相等性時,疑惑總是特別多。==到底何equals()有什麼不同呢?


解說:
從上面的例子中可以發現a、b的數值確實為10。但ia和fa實際上指的是兩個值為10的物件,因此他們的值並不為10,分別代表不同的物件
因此,可以使用==來測試兩個基本型別是否相同。使用equals()來比較倆物件是否一致(Same)

2008年1月10日 星期四

期末報告(二)

第二本書:JAVA2 550個應用範例 博碩文化編譯

出版商:博碩文化出版有限公司 2005年三月

問題139: 顯示一個多邊形

因為整個學期JAVA課都比較注重物件導向方面的題目 所以當我看到畫圖形的題目時 就想試試看



解說:

使用java.awt.Polygon類別 可以定義出多邊形 在建構子當中傳入頂點座標陣列及頂點數即可定義出多邊形

期末報告(一)

第一本書:JAVA2 語言實務 作者:袁葆宏 劉豐豪 吳建和

出版商:旗標出版有限公司 91年9月
練習題為:第四章 第三題

題目:設計出一個程式 將給定數值的4*3矩陣的總平均算出

因為想練習一下矩陣的用法 所以選了此題 除了題目要求外 我還另外寫了一個功能 讓他能將兩個矩陣相加





解說:
(1)
double a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
double b[][]={{11,22,33,44},{55,66,77,88},{9,10,11,12}};
double c[][]=a;

先宣告三個二維矩陣a,b,c 並設定a,b的初值;而c指向a的同一位置,用來儲存相加結果

(2)

sum=sum+a[i][j]
[i][j]=a[i][j]+b[i][j]
System.out.println(c[i][j])


以倆個迴圈執行相對位置的矩陣元素相加 另外將a矩陣的所有元素相加

2008年1月4日 星期五

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.

Lab Array

Study Display 6.1, and then write a program that can sort numbers in ascending order.

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.


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.


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

2007年12月21日 星期五

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

2007年12月14日 星期五

Homework 12-7-2007

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




2. Show comments on your blog.

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

Homework 11-30-2007

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



DEMO:


2. Do Lab Counter.
參考上一篇

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


加了六次減一次 執行結果為5

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?




修改後

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?

因為我們在class裡面定義month為private variable 所以欄位 DateThirdTry.Month 是不可見的

2007年11月21日 星期三

2007年11月16日 星期五

lab class definition

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