# Java 快速學習自我挑戰 Day4 #### 題目練習

##### 題目五

Write a method areEqualByThreeDecimalPlaces with two parameters of type double.
The method should return boolean and it needs to return true if two double numbers are the same up to three decimal places. Otherwise, return false.

TIP: Use paper and pencil.
TIP: Use casting.
NOTE: The areEqualByThreeDecimalPlaces 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 hasEqualSum with 3 parameters of type int.
The method should return boolean and it needs to return true if the sum of the first and second parameter is equal to the third parameter. Otherwise, return false.

NOTE: The hasEqualSum 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’ll say that a number is “teen” if it is in the range 13 -19 (inclusive).
Write a method named hasTeen with 3 parameters of type int.
The method should return boolean and it needs to return true if one of the parameters is in range 13(inclusive) - 19 (inclusive). Otherwise return false.

Write another method named isTeen with 1 parameter of type int.
The method should return boolean and it needs to return true if the parameter is in range 13(inclusive) - 19 (inclusive). Otherwise return false.

NOTE: All methods need 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.

##### 題目七(答案)

1. 方法多載就是一個功能可以允許我們使用超過一種同樣名稱的方法，只要我們使用不同的變數。它就是一個功能可以創建很多同名的方法並有不同的應用。呼叫一個多載的方法可以運行該方法特別的應用。
2. 挑戰
Create a method called calcFeetAndInchesToCentimeters. It needs to have two parameters. Feet is the first parameter, inches is the second parameter．

You should validate that the first parameter feet is >= 0.
You should validate that the second parameter inches is >=0 and <= 12.
return -1 from the method if either of the above is not true.

If the parameters are valid, then calculate how many centimeters comprise the feet and inches passed to this method and return that value.

Create a second method of the same name but with only one parameter, inches is the parameter, validate that its >= 0, return -1 if it is not true.
But if its valid, then calculate how many feet are in the inches and then here is the tricky part
call the other overloaded method passing the correct feet and inches calculated so that it can calculate correctly.

hints: Use double for your number data types is probably a good idea.
1 inch = 2.54cm and 1 foot = 12 inches.
use the link I give you to confirm your code is calculating correctly.
calling another overloaded method just require you to use the right number of parameters.
3. 挑戰的答案

1. 方法多載的好處
• 它改善了程式碼的可讀性和重複使用性。
• 記住一個名稱比起記住很多名稱更容易。
• 保持命名方法的一致性，單一名稱的方法是很常被使用的。
• 方法多載給予程式設計師彈性去呼叫一個有不同變數型態但相似的方法。

#### 秒和分的挑戰

1. 題目
• Create a method called getDurationString with two parameters, first parameter minutes and second parameter seconds.
• You should validate that teh first parameter minutes is >= 0.
• You should validate that the second parameter seconds is >=0 and <= 59.
• The method should return “Invalid value” in teh method if either of the above are not true.
• If the parameters are valid then calculate how many hours minutes and seconds equal the minutes and seconds passed to this method and return that value as string in format “XXh XXm ZZs” where XX represents a number of hours, YY the minutes and ZZ the seconds.
• Create a second method of the same name but with only one parameter seconds.
• Validate that it is >= 0, and retrun “Invalid value” if it is not true.
• If it is valid, then calculate how many minutes are in the seconds value and then call the other overloaded method passing the correct minutes and seconds calculated so that it can calculate correctly.
• Call both methods to print values to the console.
• Tips:
• Use int or long for your number data types is probably a good idea.
• 1 minute = 60 seconds and 1 hour = 60 minutes or 3600 seconds.
• Methods should be static as we have used previously.
• Bonus:
• For the input 61 minutes output should be 01h 01m 00s, but it is ok it is 1h 1m 0s (Tip: use if-else)
• Create a new console project and call it SecondsAndMinutesChallenge
2. 答案
1. final 就是 constant 變數裡面的一個關鍵字，如果定義了，就不能改變它。可以將上面的程式碼改成下面這樣。

#### 四個挑戰

##### 題目一

Write a method named area with one double parameter named radius.
The method needs to return a double value that represents the area of a circle.
If the parameter radius is negative then return -1.0 to represent an invalid value.
Write another overloaded method with 2 parameters x and y (both doubles), where x and y represent the sides of a rectangle.
The method needs to return an area of a rectangle.
If either or both parameters is/are a negative return -1.0 to indicate an invalid value.
For formulas and PI value please check the tips below.

TIP: The formula for calculating the area of a rectangle is x * y.
TIP: The formula for calculating a circle area is radius * radius * PI.
TIP: For PI use a constant from Math class e.g. Math.PI
NOTE: All methods need to be defined as public static like we have been doing so far in the course.

##### 題目二

Write a method printYearsAndDays with parameter of type long named minutes.
The method should not return anything (void) and it needs to calculate the years and days from the minutes parameter.
If the parameter is less than 0, print text “Invalid Value”.
Otherwise, if the parameter is valid then it needs to print a message in the format “XX min = YY y and ZZ d”.
XX represents the original value minutes.
YY represents the calculated years.
ZZ represents the calculated days.

TIPS:

• Be extra careful about spaces in the printed message.
• Use the remainder operator
• 1 hour = 60 minutes
• 1 day = 24 hours
• 1 year = 365 days

NOTES

• The printYearsAndDays method needs to be defined as public static like we have been doing so far in the course.
• Do not add main method to solution code.
• The solution will not be accepted if there are extra spaces
##### 題目三

Write a method printEqual with 3 parameters of type int. The method should not return anything (void).
If one of the parameters is less than 0, print text “Invalid Value”.
If all numbers are equal print text “All numbers are equal”
If all numbers are different print text “All numbers are different”.
Otherwise, print “Neither all are equal or different”.

TIP: Be extremely careful about spaces in the printed message.

NOTES

• The solution will not be accepted if there are extra spaces.
• The method printEqual needs to be defined as public static like we have been doing so far in the course.
• Do not add main method to solution code.
##### 題目四

The cats spend most of the day playing. In particular, they play if the temperature is between 25 and 35 (inclusive). Unless it is summer, then the upper limit is 45 (inclusive) instead of 35.
Write a method isCatPlaying that has 2 parameters. Method needs to return true if the cat is playing, otherwise return false
1st parameter should be of type boolean and be named summer it represents if it is summer.
2nd parameter represents the temperature and is of type int with the name temperature.

NOTES

• The isCatPlaying method needs to be defined as public static like we have been doing so far in the course.
• Do not add the main method to the solution code.
Share