Question

In: Computer Science

Write a complete java program to get input of a person’s age and their years of...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
(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...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT