Java Methods Worksheet: Loops and Logical
Operators
Exercise 1: Sum of Numbers
Task:
Write a method named sumUpTo that takes a single integer parameter n and returns the sum of all
numbers from 1 to n (inclusive). Use a loop (either a for loop or a while loop) to compute the
sum.
Hints:
Validate that n is positive; if n is less than 1, you may return 0.
Use a loop to iterate through numbers from 1 to n.
Example Usage:
public static void main(String[] args) {
System.out.println("Sum of numbers up to 5: " + sumUpTo(5));
// Expected output: 15
}
Exercise 2: Check for Prime Number
Task:
Create a method called isPrime that takes an integer parameter num and returns a boolean
indicating whether num is a prime number. A prime number is a number greater than 1 that has
no positive divisors other than 1 and itself.
Hints:
Use a loop to check divisibility from 2 up to the square root of num (or up to num - 1 for
simplicity).
Use logical operators to combine conditions (e.g., if (num < 2)).
Example Usage:
public static void main(String[] args) {
System.out.println("Is 7 prime? " + isPrime(7)); // Expected output: true
System.out.println("Is 8 prime? " + isPrime(8)); // Expected output: false
}
Exercise 3: Multiplication Table
Task:
Write a method named printMultiplicationTable that accepts an integer n and prints the
multiplication table for that number from 1 to 10.
Hints:
Use a loop (e.g., a for loop) to iterate from 1 to 10.
Use formatted printing to display the result neatly.
Example Usage:
public static void main(String[] args) {
printMultiplicationTable(3);
}
/* Expected output:
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
*/
Exercise 4: Factorial Calculation
Task:
Create a method called factorial that calculates and returns the factorial of a non-negative
integer n. The factorial of n (written as n!) is the product of all positive integers less than or
equal to n. (By definition, 0! = 1.)
Hints:
Use a loop to multiply numbers from 1 to n.
Handle the special case when n is 0.
Example Usage:
public static void main(String[] args) {
System.out.println("Factorial of 5: " + factorial(5)); // Expected output:
120
}
Exercise 5: Palindrome Checker
Task:
Develop a method named isPalindrome that takes a String as a parameter and returns true if
the string is a palindrome (reads the same forward and backward) and false otherwise. Ignore
case differences when comparing characters.
Hints:
Use a loop to compare characters from the beginning and the end of the string.
Use logical operators to continue checking until the middle of the string is reached.
Consider using the toLowerCase() method for case-insensitive comparison.
Example Usage:
public static void main(String[] args) {
System.out.println("Is 'Racecar' a palindrome? " +
isPalindrome("Racecar")); // Expected: true
System.out.println("Is 'Java' a palindrome? " + isPalindrome("Java"));
// Expected: false
}
Exercise 6: Counting Vowels in a String
Task:
Write a method named countVowels that takes a String as input and returns the number of
vowels (a, e, i, o, u) present in the string. Treat uppercase and lowercase vowels as equivalent.
Hints:
Use a loop to iterate over each character of the string.
Use charAt(i) inside a loop to retrieve each character from the string so you can
compare it with vowels.
Use a conditional statement with logical operators to check if a character is a vowel.
Example Usage:
public static void main(String[] args) {
System.out.println("Number of vowels in 'Hello World': " +
countVowels("Hello World")); // Expected: 3
}