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.

Demo:


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.

Demo:


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.

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




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


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.

add System.out.println(date.month) and see what happens.

Why?
ans:
因為原本的程式是將month day ,year設成private,只要將其改成public即可

2007年11月16日 星期五

lab class definition

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


2007年11月9日 星期五

Homework 10-26-2007

Do Project 7 of Chap. 3. (2nd ed. & 3rd ed.) or Project 4 (1st ed.) Hint: You don't have to use nested loops.

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

2007年10月26日 星期五

import java.util.Scanner;
public class averageIncome
{
public static void main(String[] args)
{
int i,average;
System.out.println("請輸入總人數");
Scanner keyboard = new Scanner(System.in);
int number = keyboard.nextInt();
System.out.println("請輸入性別與收入");
int[] genderAndIncome = new int[number];
for()
}

}

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.

2007年10月25日 星期四

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.

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.

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.

所得1000000

所得:2000000

Quiz 10-12-2007

1. Let i, j be two integers. Write a program to exchange their values. How can you show your program is correct.


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?
ans:在C語言中使用縮寫的原因是因為要節省記憶體的損耗,不過那是在之前記憶體很昂貴時,才需要這麼做。將變數已完整的名稱命名可以讓使用者更容易看懂你所編寫的程式,這是不使用縮寫的好處,壞處就是會佔更多的資源,不過這應該無傷大雅。

10-5-2007 Homework

Project 1 of Chap. 2.
n = 10000

Project 3 of Chap. 2.

2007年10月5日 星期五

Lab: Keyborad Input

Lab: Scanner


執行程式後,在顯示結果的位置輸入數字

Lab: Adding Links on Your Blog

2007年9月28日 星期五

2007年9月27日 星期四

Homework 9/21/2007

1. Explain bytecode, JVM
Byte-Code:將所寫的程式轉換成機械語言。
JVM:Java Virtual Machine,that is a Java byte-code interpreter.

2. Explain class, object
Class是用來描述屬性(Property)跟方法(Method),class要宣告成物件才能 呼叫Method, Object包含屬性(Property)和方法(Method),object可以直接 呼叫其方法,不需要宣告即可。

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

2007年9月21日 星期五

Lab 2