DEV Community

Cover image for Voting Age Program in Java
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on • Edited on

Voting Age Program in Java

Basic problem solving using functions/methods in Java. Tried to call a method for reusability and it will enrich the skill of using methods in java.

Problem Statement

So I have to take an age value from user. And check the age value, if the age is greater than or equal to 18 then he is a voter and give his vote. If his age is less than 18, then he is not a voter and can not provide his vote.

Solving Approach

We are going to take an integer input from the user and store it to age variable and pass it to a method. In the method we compare the value with 18. If (age >= 18) then he is a voter. If (0 <= age <18) then he is not a voter.

**Code

**

package FunctionsOrMethods;

import java.util.Scanner;

public class VoterOrNot {
    public static void main(String[] args) {
        System.out.println("Please Enter your age");
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        Voter(age);
    }

    private static void Voter(int age) {
        if (age >= 18){
            System.out.println("Cngratualtion you are a voter");
        }
        else {
            System.out.println("Your are not old enough to give vote,.. wait little more");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.