80 likes | 170 Views
Chapter 5 Practice. Practice with void method. Write a void dozens(int n) method that converts n into dozens.
E N D
Practice with void method Write a void dozens(int n) method that converts n into dozens. A dozen of apples is 12 apples. Given a natural number n (number of apples), it can convert n into number of dozens and the remaining number of apples. For example, if you have 35 apples, it will display “ You have 2 dozens and 11 apples.” dozens(35) “ You have 2 dozens and 11 apples.”
Practice with void method Write a void hms(int n) method that converts n into hours, minutes and second. hms(3672) “ 1 hour 1 minute and 12 seconds.”
Practice with boolean method A number n ≥ 10 is called “good” if the last 2 digits has a sum of 9. For example, 3472, 124936 are good numbers. Write a boolean isGood(int n) method that returns true if n is a good number, or false otherwise.
Practice with boolean method Write a boolean isPrime(int n) method that returns true if n is a prime number, or false otherwise. Note that a prime number is a number p > 1 which is only divisibe by 1 and itself.
Practice with char method Write a char mode(String s) method that returns a character that appear most frequently in the string s. If there are more than 1 such chracters, return the first one. For example, mode(“hello”) ‘l’ mode(“saigontech”) ‘s’ mode(“hoang thuy linh”) ‘h’
Practice with int method Write a int sum(int n) method that returns the sum of all numbers from 1 to n. For example, sum(5) 15 sum(10) 55 sum(8) 36
Practice with double method Write a double circleArea(double r) method that returns the area of a circle having radius r, rounded with 2 decimal places. For example, circleArea(1) 3.14 circleArea (10) 314.16