0% found this document useful (0 votes)
15 views

Java Methods

This document contains examples of Java method exercises with solutions. It includes 10 exercises with descriptions and sample inputs/outputs for writing methods to find the smallest of three numbers, compute an average, find the middle character of a string, count vowels in a string, count words in a string, compute the sum of digits in a number, display the first 50 pentagonal numbers, compute future investment values, print characters between two characters, and check if a year is a leap year.

Uploaded by

Suat UĞURLU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Methods

This document contains examples of Java method exercises with solutions. It includes 10 exercises with descriptions and sample inputs/outputs for writing methods to find the smallest of three numbers, compute an average, find the middle character of a string, count vowels in a string, count words in a string, compute the sum of digits in a number, display the first 50 pentagonal numbers, compute future investment values, print characters between two characters, and check if a year is a leap year.

Uploaded by

Suat UĞURLU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Genel

Java Methods: Exercises, Practice, Solution


Last update on August 19 2022 21:50:33 (UTC/GMT +8 hours)

Java Method Exercises [23 exercises with solution]


[An editor is available at the bottom of the page to write and execute the
scripts.]

1. Write a Java method to find the smallest number among three numbers. Go
to the editor
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
Click me to see the solution

2. Write a Java method to compute the average of three numbers. Go to the


editor
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
Click me to see the solution

3. Write a Java method to display the middle character of a string. Go to the


editor
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:

Genel
Genel

The middle character in the string: 5


Click me to see the solution

4. Write a Java method to count all vowels in a string. Go to the editor


Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4
Click me to see the solution

5. Write a Java method to count all words in a string. Go to the editor


Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9
Click me to see the solution

6. Write a Java method to compute the sum of the digits in an integer. Go to


the editor
Test Data:
Input an integer: 25
Expected Output:
The sum is 7
Click me to see the solution

7. Write a Java method to display the first 50 pentagonal numbers. Go to the


editor
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
Click me to see the solution

Genel
Genel

8. Write a Java method to compute the future investment value at a given


interest rate for a specified number of years. Go to the editor
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
Click me to see the solution

9. Write a Java method to print characters between two characters (i.e. A to


P ). Go to the editor
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
Click me to see the solution

10. Write a Java method to check whether a year (integer) entered by the user
is a leap year or not. Go to the editor

Expected Output:
Input a year: 2017
false
Click me to see the solution

Genel

You might also like