We need to write a program to report information about a President of the United
ID: 3808262 • Letter: W
Question
We need to write a program to report information about a President of the United States. We will need to read in information from the keyboard which contain the needed data for this report. The input consists of a line of text. The line of text will contain the data for a President in the form of multiple fields of imformation. The fields are separated by semicolons and may contain optional white space before and/or after the semicolons. The line contains four (4) fields: The number of the President (see examples below). The name of the President in the format lastName, firstName Imiddlelnitials The brackets around the middlebitials are meta-characters and serve to indicate that the middle initial is optional and not required. The brackets are not part of the format. However, the comma between the last and the first name is required syntax (see examples below). The first year of the President's term in office as a four (4) digit numeric value The number of full years that the President served. In the case of the current President, since this is the middle of his frst year, the field wil contain zero (00. Example input values: 40 Reagan, Ronald W. 1981 8 The output of the program will be a line in the following format firstName Imidalelnitialsj lastName, numWithordsuffix President of the United States from first Year to lastrear As above the brackets indicate an optional field. Num WithoraSuffix is the mumber of the president followed by the ordinality suffix 40th 41st 42nd 43rd 44th... note the comma after the last name). last hear will need to be computed based on the firstYear and the mumber of full years served. Example output vahues Ronald W. Reagan, 40th President of the United States from 1981 to 1989. Please upload your source code file: Exam1A. java to the Blackboard Assignment: ExamlA.Explanation / Answer
import java.util.Scanner;
public class Exam1A {
public static void main(String[] args) {
System.out.println("Enter the Information: ");
//reading the line from keyboard
Scanner sc = new Scanner(System.in);
String presidentInfo = sc.nextLine();
//splitted the string using regex ";"
String arr[] = presidentInfo.split(";");
//getting the Full name and splitting the string again using regex ","
String name[] = arr[1].trim().split(",");
//first Name of president from the name array
String firstName = name[1].trim();
//last Name of president from the name array
String lastName = name[0].trim();
//Number of president
String presidentNo = arr[0].trim();
//First Year of Term
String firstYearofTerm = arr[2].trim();
//Total years served
String totalYearofService = arr[3].trim();
//calculating the last year of term
int lastYearOfTerm = Integer.parseInt(firstYearofTerm) + Integer.parseInt(totalYearofService);
//Finding out the suffix to be added
String suffix = null;
if (presidentNo.endsWith("1")) {
suffix = "st";
} else if (presidentNo.endsWith("2")) {
suffix = "nd";
} else if (presidentNo.endsWith("3")) {
suffix = "rd";
} else {
suffix = "th";
}
//Printing in the format as specified
System.out.println(firstName +" "+ lastName + ", " + presidentNo + suffix + " President of the United States, from "+firstYearofTerm+" to "+lastYearOfTerm+".");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.