# Java 快速學習自我挑戰 Day3

### Java 教學 - 表達式(Expressions)、陳述句(Statements)、程式碼區塊(Code Blocks)、方法(Methods)…等等

#### 關鍵字(Keywords)和表達式(Expressions)

1. Java Keyword 列表
2. int int = 5;，這樣的語法會出現錯誤，關鍵字是被保留的，所以在命名變數的時候，不能使用跟關鍵字同名的變數。
3. 裡面的 kilometers = (100 * 1.609344);highScore = 50;highScore == 50"This is an expression." 都是表達式，double kilometers = (100 * 1.609344);int highScore = 50; 則是陳述句(statement)，而 System.out.println("This is an expression."); 則是一個方法(method)

#### 陳述句(Statements)、空白(Whitespace)、縮排(Indentation)

1. 一行陳述句的結尾要有分號，中間不管放多少空白，Java 都可以讀取。
2. 寫程式碼一定要有縮排，沒有縮排不會影響功能，但是會影響閱讀。

#### 程式碼區塊(Code Blocks)和 If-then-else 控制陳述句(Control Statement)

1. if-else if-else 的範例。
1. 在最後一行會發生錯誤，因為在 if 的程式碼區塊裡面，完成之後，就會將 finalScore 刪除。
1. 程式碼區塊裡面的內容，叫做 scope，後面會細說。
2. 挑戰：將 score 設定為 10000，levelCompleted 設定為 8，bonus 設定為 200，然後確定上面的程式碼一樣被執行。
3. 可以將變數重複利用，不需要在前面在定義變數原始型態，直接給變數名稱數值。

#### if-then-else 回顧

1. if 陳述句根據表達式的值來判別要執行哪個陳述句或是程式碼區塊內容，換句話說，就是根據特定條件。
2. 程式碼區塊會用 { } 來定義，在裡面可以執行一到多行的陳述句。
3. 我們可以在 if 陳述句後面使用 else，當條件是錯誤的時候，else 的區塊程式碼會被執行。
4. 我們還可以使用 else if 去驗證多條件。
5. 基本的 if-else 陳述句架構如下：

#### Java 裡的方法(Methods)

1. 方法是用來讓重複功能寫在一起，只要呼叫方法就可以，不需要重複寫一樣的程式碼。
2. public static 就是方法，void 就是不回傳值，如果需回傳值，就要像 calculateScore 在前面加上想要回傳的值的原始型態。
1. 為什麼回傳 -1？找不到值、或是錯誤就會回傳 -1。

#### 更多方法(Methods)和挑戰

1. 挑戰：創建一個方法叫做 displayHighScorePosition，有兩個參數分別是 playerName 和 highScorePosition，要顯示訊息 playerName + “manage to get that position " + highScorePosition + " on the high score table”。再創建一個方法叫做 calculateHighScorePosition，只有一個參數 playerScore，要回傳 int，當分數 > 1000 回傳 1，分數 > 500 且 < 1000 回傳 2，分數 > 100 且 < 500 回傳 3，其餘回傳 4，最後分別顯示以下分數的結果 1500, 900, 400, 50。
1. calculateHighScorePosition 的程式碼也可以改寫如下

#### DiffMerge 工具

1. DiffMerge 是一個程式在任何作業系統下都可以幫助你視覺地比較和合併檔案。
2. 用 DiffMerge 可以視覺地比較一個檔案或整個資料夾的程式碼。
3. DiffMerge 下載頁面

#### 題目練習

##### 題目一
1. Write a method called toMilesPerHour that has 1 parameter of type double with the name kilometersPerHour. This method needs to return the rounded value of the calculation of type long.

If the parameter kilometersPerHour is less than 0, the method toMilesPerHour needs to return -1 to indicate an invalid value.
Otherwise, if it is positive, calculate the value of miles per hour, round it and return it. For conversion and rounding, check the notes in the text below.

1. Write another method called printConversion with 1 parameter of type double with the name kilometersPerHour.

This method should not return anything (void) and it needs to calculate milesPerHour from the kilometersPerHour parameter.
Then it needs to print a message in the format “XX km/h = YY mi/h”.
XX represents the original value kilometersPerHour.
YY represents the rounded milesPerHour from the kilometersPerHour parameter.
If the parameter kilometersPerHour is < 0 then print the text “Invalid Value”.

Use method Math.round to round the number of calculated miles per hour(double). The method round returns long.

1. How to use the method round and how it works?

The Math.round() is a built-in math method which returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result after adding 1/2, and typecasting the result to type long. The method returns the value of the argument rounded to the nearest int value.

USAGE EXAMPLE:

OUTPUT:

TIP: In the method printConversion, call the method toMilesPerHour instead of duplicating the code.
NOTE: All methods should be defined as public static like we have been doing so far in the course.
NOTE: 1 mile per hour is 1.609 kilometers per hour
NOTE: Do not add a main method to the solution code.

##### 題目二

Write a method called printMegaBytesAndKiloBytes that has 1 parameter of type int with the name kiloBytes.

The method should not return anything (void) and it needs to calculate the megabytes and remaining kilobytes from the kilobytes parameter.

Then it needs to print a message in the format “XX KB = YY MB and ZZ KB”.

XX represents the original value kiloBytes.
YY represents the calculated megabytes.
ZZ represents the calculated remaining kilobytes.

For example, when the parameter kiloBytes is 2500 it needs to print “2500 KB = 2 MB and 452 KB”

If the parameter kiloBytes is less than 0 then print the text “Invalid Value”.

TIP: Be extremely careful about spaces in the printed message.
TIP: Use the remainder operator
TIP: 1 MB = 1024 KB
NOTE: Do not set kilobytes parameter value inside your method.
NOTE: The solution will not be accepted if there are extra spaces.
NOTE: The printMegaBytesAndKiloBytes method needs to be defined as public static like we have been doing so far in the course.NOTE: Do not add a main method to solution code.

##### 題目三

We have a dog that likes to bark. We need to wake up if the dog is barking at night!
Write a method shouldWakeUp that has 2 parameters.
1st parameter should be of type boolean and be named barking it represents if our dog is currently barking.
2nd parameter represents the hour of the day and is of type int with the name hourOfDay and has a valid range of 0-23.
We have to wake up if the dog is barking before 8 or after 22 hours so in that case return true.
In all other cases return false.
If the hourOfDay parameter is less than 0 or greater than 23 return false.

TIP: Use the if else statement with multiple conditions.
NOTE: The shouldWakeUp method needs to be defined as public static like we have been doing so far in the course.
NOTE: Do not add a main method to solution code.

##### 題目四

Write a method isLeapYear with a parameter of type int named year.
The parameter needs to be greater than or equal to 1 and less than or equal to 9999.
If the parameter is not in that range return false.
Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is a leap year, otherwise return false.

To determine whether a year is a leap year, follow these steps:

1. If the year is evenly divisible by 4, go to step
2. Otherwise, go to step 5.2. If the year is evenly divisible by 100, go to step
3. Otherwise, go to step 4.3. If the year is evenly divisible by 400, go to step
4. Otherwise, go to step 5.4. The year is a leap year (it has 366 days). The method isLeapYear needs to return true.
5. The year is not a leap year (it has 365 days). The method isLeapYear needs to return false.

The following years are not leap years:
1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600
This is because they are evenly divisible by 100 but not by 400.

The following years are leap years:
1600, 2000, 2400
This is because they are evenly divisible by both 100 and 400.

NOTE: The method isLeapYear needs to be defined as public static like we have been doing so far in the course.
NOTE: Do not add a main method to solution code.

Share