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.

2007年11月9日 星期五

Homework 10-26-2007

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



Lab Exponential Function

2007年10月26日 星期五

Lab 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

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....

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.


Bonus: Lab for-loop


Write a program to generate the series 1, 1, 2, 3, 5, 8, 13, ...
The series has a property that the third number is the sum of the first and second numbers. For example, 2=1+1, 3=1+2, and 5=2+3

2007年10月12日 星期五

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 10-12-2007



先輸入的為i 後輸入的為j

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不使用縮寫的好處是可讓人淺顯易懂,壞處是佔記憶體。

10-5-2007 Homework

Project 3 of Chap. 2.


Project 1 of Chap. 2.

2007年10月5日 星期五

Lab: Keyborad Input


Enter the number of pods:
the number of peas in a pod:
10
20
10 pods and 20 peas per pod.
The total number of peas = 200

Lab: Scanner


Enter the number of pods followed by
the number of peas in a pod
50 21
50pods and21peas per pod
The total number of peas =1050

Lab: Adding Links on Your Blog

1. Check into your blog homepage.
2. Enter "自訂"
3. Select "連結清單"
4. Write down "http://javaatcycu.blogspot.com/" and the title as "Java物件導向程式設計"
5. Also add your own favorite links.

2007年9月28日 星期五

Homework 9-28-2007 String Processing

Write a program that starts with a line of text and then outputs that line of text with the first occurrence of "hate" changed to "love". For example, a possible sample output might be

The line of text to be changed is:
I hate you.
I have rephrased that line to read:
I love you.

Hint: You may consider use the methods: indexOf(A_String) and substring(Start, End) in your program.

Lab 9-28-2007 Simple Calculation

Suppose you are a landscape architect who charges $5,000 per mile to landscape a highway, and suppose you know the length in feet of the high way you are working on. Write a Java program to calculate the price you charge when the length is 6000 and 4000, respectively.

Hint: There are 5280 feet in a mile.


Ans:
如果是4000feet的話,需要3787.878787878788元
如果是6000feet的話,需要5681.818181818182元

Homework 9/21/2007

"tell me and I'll forget; show me and I may remember; involve me and I'll understand"

1. Explain bytecode, JVM
Byte-Code:可將我們所寫的程式經過編譯後產出的中間碼。
JVM:Java Virtual Machine.一個以軟體模擬出來的機器環境,可用來執行java 的 byte code。

2. Explain class, object
class是object的集合,用來描述屬性(Property)跟方法(Method)。
object是物件,包含在CLASS裡



4.1 Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (i++);
Print i;


Ans: 2, 4, 3

4.2 Write a Java program as follows:

Let i=2;
Print i;
Print 2 * (++i);
Print i;


Ans: 2, 6, 3

4.3 Write a Java program as follows:

Let m=7, n=2;
Print (double) m/n;
Print m/ (double)n;

Ans: 3.5, 3.5