Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a java application to build and display a random date between January 1,

ID: 639606 • Letter: C

Question

Create a java application to build and display a random date between January 1, 1900 and December 31, 2014 inclusive. Retrieve a random year, month, and day. The maximum day is determined by the month. Recall that April, June, September, and November have only 30 days. If the month is February, the maximum day is 28 unless it is a leap year in which case the maximum day is 29.

Display the date twice as follows:

     Random Date is: 10/3/2000

     Formatted date: October 3rd, 2000

Note that the day is followed by 'st', 'nd', 'rd', or 'th'

Here are some examples of valid dates for the display:

Random Date is: 5/6/1978

Formatted date: May 6th, 1978

Random Date is: 4/22/1921

Formatted date: April 22nd, 1921

Random Date is: 8/1/1987

Formatted date: August 1st, 1987

HINT: You must retrieve a year and month before you can retrieve a random day. Since the maximum day that can be retrieved is dependent on the month, and if the month is February, the maximum day can be 29 if it is leap year.

Explanation / Answer

Sample Run:

Random Date is:
9/3/1901
Formatted Date is:
March 9th,1901

Sample Run:

Random Date is:
29/11/1930
Formatted Date is:
November 29th,1930

Required Solution:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class RandomDate
{
   static List<String> months;
   //Initialize months
   static void prepareMonth()
       {
           months=new ArrayList<String>();
           months.add("January");months.add("February");months.add("March");months.add("April");
           months.add("May");months.add("June");months.add("July");months.add("August");
           months.add("September");months.add("October");months.add("November");months.add("December");
       }
   //generate random date
   static void randomDate() throws Exception
       {
           prepareMonth();
           Random rdm=new Random();
           //get random year
           int year=1900+rdm.nextInt(114);
           //get random month
           int month=1+rdm.nextInt(11);
           int maxdays=0;
           //get random date based on year
           boolean isLeapyear=false;
           if ( year%400 == 0)
               isLeapyear=true;
           else if ( year%100 == 0)
               isLeapyear=false;
           else if ( year%4 == 0 )
               isLeapyear=true;
           else
               isLeapyear=false;
           if(month==1 ||month==3 ||month==5 ||month==7||month==8||month==10||month==12)
                   maxdays=30;
           else if(month==2)
           {
               if(isLeapyear)
                   maxdays=29;
               else
                   maxdays=28;
           }
           else
               maxdays=31;
           int day=1+rdm.nextInt(maxdays-1);
           //print random date
           System.out.println( "Random Date is:");
           System.out.println(day+"/"+month+"/"+year);
           //print formatted date
           System.out.println( "Formatted Date is:");
           String postfix="";
           if(day==1||day==21||day==31)
               postfix="st";
           else if(day==2||day==22||day==31)
               postfix="nd";
           else if(day==3||day==23)
               postfix="rd";
           else
               postfix="th";
           System.out.println((months.get(month-1))+" "+day+postfix+","+year);
          
          
       }
   //test method
   public static void main(String[] args) throws ParseException
       {
           randomDate();
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote