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.
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 java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT