In: Computer Science
Write a complete java program to get input of a person’s age and their years of current USA citizenship. Tell them if they are eligible to run for US House of Representatives, US Senate, or President. At first, have the program just run once and give the answer for the given inputs. Give the answer in a nice format and be clear which offices the person can run for. Write good and complete pseudo code. Next, put that program in a function and call if from a loop that gets input’s from main (no input or output in the function itself). Have the loop run until the user enters 0 for their age. Finally, modify the input GUI so that the user enters only one number if years of citizenship is the same as age (two numbers when they are different). The rules for eligibility are: President: 35 years old and natural born citizen (age and year’s citizenship match) US Senate: 30 years old and 9 years citizenship US House of Representatives: 25 years old and 7 years citizenship.
USPolitics.java
import java.util.Scanner;
public class USPolitics {
public static String checkStatus(int age, int
citizenship)
{
if(age == citizenship &&
age >= 35)
return "You can
contest for President.";
else if(age >= 30 &&
citizenship >= 9)
return "You can
contest for US Senate.";
else if(age >= 25 &&
citizenship >= 7)
return "You can
contest for US House of respresntatives.";
return "You cannot contest for
elections.";
}
public static void main(String[] args) {
Scanner in = new
Scanner(System.in); // used to read
input from keyboard
int age, citizenship;
System.out.print("Enter age:
");
age = in.nextInt();
in.nextLine();
System.out.print("Enter US
citizenship(in years): ");
citizenship = in.nextInt();
in.nextLine();
// program in main
if(age == citizenship &&
age >= 35)
System.out.println("You can contest for President.");
else if(age >= 30 &&
citizenship >= 9)
System.out.println("You can contest for US Senate.");
else if(age >= 25 &&
citizenship >= 7)
System.out.println("You can contest for US House of
respresntatives.");
else
System.out.println("You cannot contest for elections.");
// calling method from loop
String ans = "";
do {
System.out.print("Enter age( 0 to terminate): ");
age =
in.nextInt();
in.nextLine();
if(age ==
0)
break;
System.out.print("Enter US citizenship(in years): ");
citizenship =
in.nextInt();
in.nextLine();
ans =
checkStatus(age, citizenship);
System.out.println(ans+"\n");
}while(age != 0);
in.close();
}
}
Output: