Java Methods: Exercises, Practice, Solution
Java Method Exercises [23 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Find Smallest Number Among Three
Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Expected Output:
The smallest value is 25.0
2. Compute Average of Three Numbers
Write a Java method to compute the average of three numbers.
Test Data:
Input the first number: 25
Input the second number: 45
Input the third number: 65
Expected Output:
The average value is 45.0
3. Find Middle Character(s) of String
Write a Java method to display the middle character of a string.
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
Input a string: 350
Expected Output:
The middle character in the string: 5
4. Count Vowels in String
Write a Java method to count all vowels in a string.
Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4
5. Count Words in String
Write a Java method to count all the words in a string.
Test Data:
Input the string: The quick brown fox jumps over the lazy
dog.
Expected Output:
Number of words in the string: 9
6. Sum of Digits in Integer
Write a Java method to compute the sum of digits in an integer.
Test Data:
Input an integer: 25
Expected Output:
The sum is 7
7. Display First 50 Pentagonal Numbers
Write a Java method to display the first 50 pentagonal numbers.
Note: A pentagonal number is a figurate number that extends the concept of triangular and square numbers to the pentagon, but, unlike the first two, the patterns involved in the construction of pentagonal numbers are not rotationally symmetrical.
Expected Output:
1 5 12 22 35 51 70 92 117 145 176 210 247 287 330 376 425 477 532 590 651 715 782 852 925 1001 1080 1162 1247 1335 1426 1520 1617 1717 1820 1926 2035 2147 2262 2380 2501 2625 2752 2882 3015 3151 3290 3432 3577 3725
8. Compute Future Investment Value
Write a Java method to compute the future investment value at a given interest rate for a specified number of years.
Sample data (Monthly compounded) and Output:
Input the investment amount: 1000
Input the rate of interest: 10
Input number of years: 5
Expected Output:
Years FutureValue 1 1104.71 2 1220.39 3 1348.18 4 1489.35 5 1645.31
9. Print Characters Between Two Characters
Write a Java method to print characters between two characters (i.e. A to P).
Note: Prints 20 characters per line
Expected Output:
( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
10. Check Leap Year
Write a Java method to check whether a year (integer) entered by the user is a leap year or not.
Expected Output:
Input a year: 2017 false
11. Validate Password
Write a Java method to check whether a string is a valid password.
Password rules:
A password must have at least ten characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Expected Output:
1. A password must have at least eight characters. 2. A password consists of only letters and digits. 3. A password must contain at least two digits Input a password (You are agreeing to the above Terms and Conditions.): abcd1234 Password is valid: abcd1234
12. Display n-by-n Matrix
Write a Java method (takes n as input) to display an n-by-n matrix.
Expected Output:
Input a number: 10 1 0 0 1 1 0 0 0 1 1 0 0 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 0
13. Calculate Triangle Area
Write Java methods to calculate triangle area.
Expected Output:
Input Side-1: 10 Input Side-2: 15 Input Side-3: 20 The area of the triangle is 72.6184377413890
14. Calculate Pentagon Area
Write a Java method to create a pentagon's area.
Expected Output:
Input the number of sides: 5 Input the side: 6 The area of the pentagon is 61.93718642120281
15. Display Current Date and Time
Write a Java method to display the current date and time.
Expected Output:
Current date and time: Wednesday January 25, 2017 7:47:43
16. Find Twin Primes Less Than 100
Write a Java method to find all twin prime numbers less than 100.
Expected Output:
(3, 5) (5, 7) (11, 13) (17, 19) (29, 31) (41, 43) (59, 61) (71, 73)
17. Count Occurrences of Digit '2' in Integer
Write a Java method to count the number of digits in an integer with the value 2. The integer may be assumed to be non-negative.
Expected Output:
Input a number: 12541 1
18. Check Consecutive Integers
Write a Java method that accepts three integers and checks whether they are consecutive or not. Returns true or false.
Expected Output:
Input the first number: 15 Input the second number: 16 Input the third number: 17 Check whether the three said numbers are consecutive or not!true
19. Check If One Integer is Midpoint of Others
Write a Java method that accepts three integers and returns true if one is the middle point between the other two integers, otherwise false.
Expected Output:
Input the first number: 2 Input the second number: 4 Input the third number: 6 Check whether the three said numbers has a midpoint! true
20. Extract First Digit of Integer
Write a Java method for extracting the first digit from a positive or negative integer.
Expected Output:
Input an integer(positive/negative): 1234 Extract the first digit from the said integer: 1
21. Display Factors of 3 in Integer
Write a Java method to display the factors of 3 in a given integer.
Expected Output:
Input an integer(positive/negative): 81 Factors of 3 of the said integer: 81 = 3 * 3 * 3 * 3 * 1
22. Check If All Digits in Integer Are Even
Write a Java method to check whether every digit of a given integer is even. Return true if every digit is odd otherwise false.
Expected Output:
Input an integer: 8642 Check whether every digit of the said integer is even or not! true
23. Check If All Characters Are Vowels
Write a Java method that checks whether all the characters in a given string are vowels (a, e,i,o,u) or not. Return true if each character in the string is a vowel, otherwise return false.
Expected Output:
Input a string: AIEEE Check all the characters of the said string are vowels or not! true
Java Code Editor
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/method/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics