Methods
in Java
Instructor Khurram Iqbal
Exercise-1 –
JAVA Methods
•Write a method that computes the
sum of the digits in an integer. Use
the following method header: public
static int sumDigits(long n)
•For example, sumDigits(234) returns
9 (2 + 3 + 4).
public class DigitSum {
public static int sumDigits(long n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
[Link](sumDigits(234));
}
}
Exercise 2 –
JAVA Methods
•Write a method with the following
header to display an integer in
reverse order:
•public static void reverse(int number)
•For example, reverse(3456) displays
6543. Write a test program that
prompts the user to enter an integer
and displays its reversal.
import [Link];
public class ReverseInteger { public static void main(String[] args) {
public static void reverse(int int number;
number) {
int digit ; Scanner input = new Scanner([Link]);
while (number != 0) {
digit = number % 10 [Link]("Enter an integer:
[Link](digit); ");
number /= 10;
number = [Link]();
}
[Link]("Reversed
[Link](); number: ");
} reverse(number);
}
}
Exercise 3 –
JAVA Methods
•Write the methods with the following headers
•// Return the reversal of an integer, i.e., reverse(456)
returns 654
•public static int reverse(int number)
•// Return true if number is a palindrome
•public static boolean isPalindrome(int number)
•Use the reverse method to implement isPalindrome. A
number is a palindrome if its reversal is the same as itself.
Write a test program that prompts the user to enter an
integer and reports whether the integer is a palindrome
import [Link]; public static void main(String[] args)
{
public class PalindromeCheck { Scanner input = new
Scanner([Link]);
public static int reverse(int number) {
int reversed = 0; [Link]("Enter an
while (number != 0) { integer: ");
int digit = number % 10; int number = [Link]();
reversed = reversed * 10 +
digit; if (isPalindrome(number)) {
number = number / 10; [Link](number +
} " is a palindrome.");
return reversed; } else {
} [Link](number +
" is not a palindrome.");
// Return true if number is a }
palindrome
public static boolean [Link]();
isPalindrome(int number) { }
// A number is a palindrome if its }
reverse is the same as itself
return number ==
reverse(number);
}
Exercise 4 –
JAVA Methods
•Write a method with the following header to
display three numbers in increasing order:
•public static void
displaySortedNumbers(double num1,
double num2, double num3)
•Write a test program that prompts the user to
enter three numbers and invokes the method
to display them in increasing order.
import [Link];
[Link]("Sorted numbers:
public class SortThreeNumbers { " + num1 + " " + num2 + " " +
num3);
public static void }
displaySortedNumbers(double
num1, double num2, double
num3) { public static void main(String[] args)
double temp; {
if (num1 > num2) { double num1 ,num2, num3
temp = num1;
num1 = num2; Scanner input = new
num2 = temp; Scanner([Link]);
}
[Link]("Enter three
if (num2 > num3) { numbers: ");
temp = num2; num1 = [Link]();
num2 = num3; num2 = [Link]();
num3 = temp; num3 = [Link]();
}
displaySortedNumbers(num1, num2,
if (num1 > num2) { num3);
temp = num1;
num1 = num2; }
num2 = temp;
} }
Exercise 5 –
JAVA Methods
•Write a Java method to count all vowels in a
string. Here is sample run
•Enter a string: Welcome to Java
•Number of Vowels in the string: 6
import [Link]; public static void main(String[] args)
{
public class VowelCounter { Scanner input = new
Scanner([Link]);
String userInput;
public static int int vowelCount;
countVowels(String str) {
int count = 0; [Link]("Enter a string:
str = [Link](); ");
userInput = [Link]();
for (int i = 0; i <
[Link](); i++) { vowelCount =
char ch = [Link](i); countVowels(userInput);
if (ch == 'a' || ch == 'e' || [Link]("Number of
ch == 'i' || ch == 'o' || ch == 'u') Vowels in the string: " +
{ vowelCount);
count++; }
} }
}
return count;
}