Question

In: Computer Science

10) Create a java program that will ask for name and age with method age where...

10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.

Solutions

Expert Solution

For the given program, a method checkAge() can be used to check eligibility to vote. The required program and corresponding output are as follows. The code is well commented for better understanding.

import java.util.Scanner;

public class Voting {
  
    public static int checkAge(int a){//cheks for eligibility
          
            int r; 
        if(a>18)//if age greater than 18
                {
                        r=0;
                }
                else
                {
                        r = (18 - a);//else returns difference in age
                } 
                return r;
    }           

        public static void main(String[] args) {
        
        int vote;
                Scanner scan = new Scanner(System.in);
                System.out.println("Enter your Name:");
                String name=scan.nextLine();//reads name
                System.out.println("Enter your Age:");
                int age = scan.nextInt();//reads age
                vote=checkAge(age);//calls function to check age
                System.out.println("Name:"+name);//displays name
                if(vote==0){//checking return value
                        System.out.println("You are eligible to vote.");
                }else{
                        System.out.println("Sorry, you can't vote now! You can vote after :"+ vote + " years");
                }

        }

}

Output:


Related Solutions

Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation.
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Here is the code that I have so far I can't seem to get it...
Create a Java method that does the following: 1) Ask the user for a dividend and...
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation. This is the code I have from the...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
create a python program that ask for a name and birth year separated by a comma....
create a python program that ask for a name and birth year separated by a comma. the program should keep prompting until the user inputs 'quit'. print the dictionary containing the key value pair name:year in the same order they were inputted. print the name and birth year on a single line for each of the entries. finally print the dictionary with the key value paris swapped and sorted by birth year. from youngest to oldeest.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT