Question

In: Computer Science

A date written in MMDDYYYY format is palindromic if it is read identically from left to...

A date written in MMDDYYYY format is palindromic if it is read identically from left to right and from right to left. For example, the date February 2, 2020 (02022020).

Develop a Java program (called Palindrome Dates.java) that identifies all palindromic dates in a given year. The program must have the following functionality:

When running the program, the user is prompted to enter a year A.
Afterwards, the program calculates all the palindromic dates and prints them on the screen (separated by a space) in MMDDYYYY format.
Finally, the program prints on the screen the total number of palindromic dates in that year.
Tip: use the LocalDate and DateTimeFormatter classes included in Java to handle dates. For this, it requires doing two imports:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
To iterate between dates you can use the following example which increments the start date by one day until it arrives just before the end date.
for (LocalDate date = start; date.isBefore (end); date = date.plusDays (1)) {
         // Processing of the date contained in the date object
}

Solutions

Expert Solution

Hi,

Please find below given code as per your requirement.

Let me know if you have any concern/doubt in this answer via comments.

Hope this answer helps you.

Thanks.

/********************JAVA CODE***********************/

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class PalindromeDates {

        //Date format
        static DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMddyyyy");
         
        public static void main(String[] args) {
                //Scanner class is used to take user input from console
                Scanner sc = new Scanner(System.in);
                //prompting user to enter year
                System.out.print("Please enter year : ");
                //storing year in variable
                int year = Integer.valueOf(sc.nextLine());
                //calling method which identifies palindroms in given year
                identifyPalindromicDates(year);
        }

        /**
         * This method takes year as parameter and prints all palindrom dates present in that year.
         * @param year
         */
        public static void identifyPalindromicDates(int year) {
                //setting year in local date with first day of year
                LocalDate start = LocalDate.of(year, 1,1);
                //setting end date to next year to set boundary
                LocalDate end = LocalDate.of(year+1, 1, 1);
                int totalPanlindromeDates = 0;
                for (LocalDate date = start; date.isBefore(end); date = date.plusDays (1)) {
                        //formatting date in string 
                        String formattedDate = customFormatter.format(date);
                        //reversed the formatted string
                        String reverseDate = reverseString(formattedDate);
                        //if formatted and reversed string is same  then it is palindrom
                        if(formattedDate.equals(reverseDate)) {
                                System.out.print(formattedDate + " ");
                                totalPanlindromeDates++;
                        }
                }
                System.out.println("\nTotal Palindrom in "+year+ " is : "+totalPanlindromeDates);

        }
        
        /**
         * Reverse the given string in parameter and returns
         * @param inputString
         * @return
         */
        public static String reverseString(String inputString) {
                
                char[] strCharArray = new char[inputString.length()];
                int counter = 0;
                for (int i = inputString.length() - 1; i >= 0; i--) {
                        strCharArray[counter] = inputString.charAt(i);
                        counter++;
                }
                return String.valueOf(strCharArray);
        }

}


Related Solutions

A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
Read the article, “Big Data in Big Companies” (written by Thomas Davenport and Jill Dyche) from...
Read the article, “Big Data in Big Companies” (written by Thomas Davenport and Jill Dyche) from the International Institute from Analytics. Comment on the role of business analytics in business today. ( Answer Needs to be at least 150 words)
I have a table of bike riders in the following format: Bike_number, Start_date(date and time), End_date(date,...
I have a table of bike riders in the following format: Bike_number, Start_date(date and time), End_date(date, time) I also have a table of temperatures per hour Using sql: how do I calculate Count/min/max/average of riders as compared to the temperature at the hour
Yes, it needs to be written in essay format. During the final week of the course,...
Yes, it needs to be written in essay format. During the final week of the course, you will submit a paper that will be written as if you were making recommendations to a member of Congress. Imagine that the United States Congress was considering sweeping legislation that would provide intensive regulation of nonprofit fundraising, including the imposition of more filing requirements to the Internal Revenue Service for nonprofits, as well as more active obligations on the part of nonprofit leaders...
C++ Create the code Read  numbers from stdin and print their sum to stdout. Input Format One...
C++ Create the code Read  numbers from stdin and print their sum to stdout. Input Format One line that contains  space-separated integers:a , b, c and . Constraints 1≤ a,b,c ≤1000 Output Format Print the sum of the three numbers on a single line. Sample Input 1 2 7 Sample Output 10 Explanation The sum of the three numbers is 1+2+7=10
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
An article was written that on average only 35% of people read, and that the number...
An article was written that on average only 35% of people read, and that the number of books that are read are correlated to the reader’s age. A simple random survey was conducted on 34 people. The binomial question being “Do you read books?”. The 2 quantitative question being: “How many books have you read in the last 2 years?” and “What is your age”. The results are below: 18 people stated they do not read, 0 books per year....
In a written paragraph format please list and highlight the career goals and objectives of someone...
In a written paragraph format please list and highlight the career goals and objectives of someone who is pursuing a business career in Sales.
Related to Point of care testing: an excellent policy is written in format that includes purpose...
Related to Point of care testing: an excellent policy is written in format that includes purpose of the policy and appropriate sections for the outside department responsibility and the laboratory responsibility and monitoring.
The paper must be written in current APA format. All sources must be of a scholarly...
The paper must be written in current APA format. All sources must be of a scholarly nature; at least 5 references if possible. The paper must have 3 Level 1 headings that correspond to the following case points: 1) How did the corporate culture of Enron contribute to its bankruptcy? 2) In what ways did Enron's bankers, auditors, and attorneys contribute to Enron's demise? 3) What role did the company's Chief Financial Officer play in creating the problems that led...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT