Java Methods Exercises
Java Methods Exercises
1. Make a Java program that accepts an integer number and display the next even number. You must
declare a method named “GetNextEvenNumberOf” with a single integer parameter and returns an
integer number (the next even number).
Example1:
Enter a number: 5
Next Even number: 6
Example2:
Enter a number: 22
Next Even number: 24
2. Make a Java program that accepts integer number as month and display the equivalent month name.
Your program must have a method with one integer argument and returns a string(month name).
Note: 1 for January, 2 for February, and so on.
If the number is not in the range of 1 to 12, return a string “No Corresponding Month Name”.
Example1:
Enter month number(1 to 12): 11
Month Name: November
Example2:
Enter month number(1 to 12): 15
Month Name: No Corresponding Month Name
3. Make a Java program that accepts four integer numbers representing the coordinates of the two
points in a Cartesian plane. Display the distance between two points and the slope of the line. Your
program must have two methods. One method is “GetDistance” that has four integer parameters and
returns the distance of two points. Another method is “GetSlope” that has four integer parameters
also and returns the slope of the line.
( y 2− y 1)
Formulas: distance = √ (x 2−x 1)2 +( y 2− y 1)2 and slope =
( x 2−x 1)
Example:
Enter X1: 5
Enter Y1: 3
Enter X2: 12
Enter Y2: 7
Distance: 8.06225775
Slope: 0.57142857
Example2:
Enter a year: 2016
Leap Year: true
5. Make a Java program that accepts user’s name and an integer number(N) and display the name Nth
times. Your program must declare a method name “Print” with two parameters (string and integer)
and prints the string nth times specified on the integer parameter.
Example:
Enter your name: “Aiden”
Enter number of times to display: 5
Aiden
Aiden
Aiden
Aiden
Aiden
6. Make a Java program that accepts an integer number and display the sum of all digits of a number.
Your program must have a method name “SumOfAllDigits” with single integer parameter and returns
the sum of all digits of the number.
Example:
Enter a number: 12345
Sum of all digits: 15
Example2:
Enter 1st number: 4
Enter 2nd number: 1
Enter iteration: 5
Fibonacci Series: 4 1 5 6 11
8. Make a Java program that accepts two integer number and display the Greatest Common Divisor (GCD)
of two inputted number. Your program must have a method “GetGCD” that has two integer
parameters and returns an integer number representing the GCD of the two numbers.
Example:
Enter number1: 12
Enter number2: 32
GCD: 4
9. Make a Java program that accepts 10 integer numbers and stores them in array variable. Display the
range of the inputted numbers. Range is the difference of the highest number and the lowest number
from the list of numbers. Your program must declare a method “GetRange” that accepts integer array
and return the range of the array numbers.
Example:
Enter number[1]: 2
Enter number[1]: 4
Enter number[1]: 5
Enter number[1]: 2
Enter number[1]: 3
Enter number[1]: 9
Enter number[1]: 5
Enter number[1]: 5
Enter number[1]: 3
Enter number[1]: 7
Range: 7