2009年3月30日 星期一

Homework 7 : 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();

Lab 15 : 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.mon
th);
and see what happens. Why?

因為month在DateThirdTry.java使用private的宣告,所以受到保護僅限在該Class使用。

Lab 14 : class definition

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





2009年3月27日 星期五

Homework 6

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।

2009年3月24日 星期二

Lab 13 : Cosine

Write a Java program to calculate the triangular function as follows:
Cos(x)=1 - x 2 /2!+ x 4/4!- x 6/ 6!...

2009年3月23日 星期一

Lab 12 : Fibonacci

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

Want to know more about Fibonacci number

最後答案會趨近於1.618033988749895


2009年3月21日 星期六

Homework 5

1. Project 7 of Chap. 3

for n=1 and n=10

for n=50 and n=100

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

2009年3月16日 星期一

Lab 11 : Finding the max of a list of numbers

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.

Lab 10 : Finding the max of three numbers

Write a program to decide the max number of the three input number.

Lab 9 : 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.

2009年3月15日 星期日

Homework 4

1. Do Project 4 in Chapter 2


2. Do Project 5 in Chapter 2


3. Do Project 6, in Chapter 2


4. Do Project 7, in Chapter 2

2009年3月9日 星期一

Lab 8: Keyboard precessing

Project 3 of Chap. 2.

Lab 7: Keyboard Input

Rewrite Display 2.6 using BufferedReader.

You need to import the following packages in the first place.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

Change

Scanner keyboard= new Scanner(System.in);

into

BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));

String inputString = keyboard.readLine();

Note the Main method needs IOException handling as follows:

public static void main (String[] args) throws IOException

Lab 6: Scanner

Do Display 2.6.

What is ASCII?

2009年3月7日 星期六

Homework 3

1. Write a program that can reverse the order of an input string. For example, if you input "ab", it will output "ba". If you input "abcdefg", it should return "gfedcba".



2. Write a program that can print names alternatively. The names are in the format of "First Name + Last Name" or "Last Name, First Name". For example, if you input "Walt Savitch", your program will output "Savitch, Walt". If you input "Savitch, Walt", your program will output "Walt Savitch".

2009年3月3日 星期二

Lab 5: String Processing

Do Project 5 of Chap. 1 on Page 56.

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.

2009年3月2日 星期一

Lab 4: Java casting

Write a Java program as follows:

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

Lab 3: Java operators

(a) Write a Java program as follows:

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

Ans: 2, 4, 3



(b) Write a Java program as follows:

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

Ans: 2, 6, 3

Homework 2

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


2. Write a Java program that displays the results of the expressions 15/4, 4/15, 15%4, 4%15. Calculate the values of these expressions manually to verify that the displayed values are correct.


15/4算出來的結果為3.75。
4/15算出來的結果為0.266666666...6,程式用浮點數值只算到小數點第八位,會有誤差而得到的結果為0.26666668。
15%4為15/4的值取餘數,與程式計算結果一樣為3。
4%15為4/15的值取餘數,與程式計算結果一樣為4。