17.5k views
14 votes
Complete the static method yearsToVote, which returns a String displaying the number of years a person has been able to vote or the number of years they have to wait to vote (the voting age is 18). The age is passed in as an argument.

public static String yearsToVote (int age) {
** Complete the code **
}

User Jsfrocha
by
4.1k points

1 Answer

8 votes

Answer:

Following are the code to this question:

public class Main//defining a class

{

public static String yearsToVote(int age)//defining a method yearsToVote that holds an integer variable as a parameter

{

String s;//defining a String variable

int a;//defining a integer variable

if(age<18)//defining if block to check age

{

a=18-age;//calculating the age value

s="The Person has to wait "+a+ " more years before they can vote";//print string value with age

}

else//defining else variable

{

a=age-18;//calculating age value

s="Since last " +a+" years person has been abel to vote ";//print string value with age

}

return s; // return String value

}

public static void main(String[] args)//defining a mein method

{

int age=22;//defining an integer variable with value

System.out.println(yearsToVote(22));//use print method to call static method

}

}

Output:

Since last 4 years person has been abel to vote

Step-by-step explanation:

In this code, a static method "yearsToVote" is declared that accepts an integer variable "age" in its parameter, and defined one string and one integer variable, and use if block to check value and return string value with a message.

In the next step, the main method is declared, which defines an integer variable and holds its value and passes into the method "yearsToVote" use the print method to call and print its value.

User Steven Roose
by
4.2k points