Methods Practice
- Write a Java to check if a person is eligible to vote based on their age. Create a method to check the eligibility.
Sample Input
Enter your age:
19
Output
You are eligibile to vote!.
Solution Code
import java.util.Scanner;
public class VotingEligibilityChecker {
// Method to check if a person is eligible to vote
boolean isEligibleToVote(int age) {
return age >= 18;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your age:");
int personAge = input.nextInt();
VotingEligibilityChecker obj = new VotingEligibilityChecker();
boolean result = obj.isEligibleToVote(personAge);
// Check eligibility and print the result
if (result == true) {
System.out.println("You are eligible to vote!");
} else {
System.out.println("Sorry, you are not eligible to vote yet.");
}
}
}
- Write a program to find out if a number is odd or even using a method.
Sample Input
Enter a number:
5
Output
5 is odd.
Solution Code
import java.util.Scanner;
class Main {
// The method checks if a number is even (true) or odd (false).
boolean checkParity(int number) {
if (number % 2 == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Main obj = new Main();
System.out.println("Enter a number:");
int n = input.nextInt();
// The result will be either true (even) or false (odd).
boolean result = obj.checkParity(n);
// Prints if the result is true (even) or false (odd).
if (result) {
System.out.println(n + " is even.");
} else {
System.out.println(n + " is odd.");
}
}
}
Quick Tip: Even though using a method might make the program seem a bit longer, it actually makes our code easier to understand and lets us use it again if we need to.
- Write a method to compute the factorial of a number.
Sample Input
Enter a non-negative number:
5
Output
120
Solution Code
import java.util.Scanner;
public class FactorialCalculator {
// Method to compute the factorial
int computeFactorial(int n) {
// Initialize factorial to 1
int factorial = 1;
// Compute factorial using a loop
for (int i = 1; i <= n; i++) {
factorial *= i;
}
// Return the computed factorial
return factorial;
}
public static void main(String[] args) {
FactorialCalculator obj = new FactorialCalculator();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a non-negative number:");
int number = scanner.nextInt();
// Call computeFactorial method and store the result in total
int total = obj.computeFactorial(number);
// Print the result
System.out.println("The factorial of " + number + " is: " + total);
}
}
- Write a method to find out the largest number among three given numbers.
Solution Code
class Main {
// Method to determine and return the maximum of three numbers
int findMaximum(int num1, int num2, int num3) {
int maximum = num1; // Assume num1 is the maximum initially
if (num2 > maximum) {
maximum = num2; // Update maximum if num2 is greater
}
if (num3 > maximum) {
maximum = num3; // Update maximum if num3 is greater
}
return maximum; // Return the final maximum value
}
public static void main(String[] args) {
// Create an instance of the Main class
Main obj = new Main();
// Find and print the maximum among three numbers
int result = obj.findMaximum(3, 55, 10);
System.out.println("The maximum number is: " + result); // Output: 55
result = obj.findMaximum(0, 12, 30);
System.out.println("The maximum number is: " + result); // Output: 30
}
}