Answer 3

Up Answer 1i Answer 1ii Answer 2 Answer 3

The Question [Optional]:

Write a program in Java which will parse and recognise (output "Accept") all and only expressions which conform to ONE of the sets of syntax rules designed for the previous question.

 

The Answer

This is the program.

// Written by Steve Edwards 25/05/01
// This needs Hanson's class's to work

 import java.io.*;  
   
             class syntax_date {
                                public static void
main(String param[])throws Exception{              

                                                input in=new input();
                                                output out=new output();

                                                out.writeln("This program will except or reject a date input." +"\n"
                                               
+"If the date input is excepted it will write the output. "+"\n" 
                                               
+"Type in your date in this format dd/mm/yyyy followed by return." +"\n");
   
                                             int day=in.readint();
                                                in.readnext();         // skips the - / or blanks
   
                                             int month=in.readint();
                                                in.readnext();
   
                                             int year=in.readint();
                                                out.writeln();

                                                int count = 0;         // starts a counter

                                                if ((day<1)|(day>31)){                // rejects days out of range
   
                                                     out.writeln("Days are between 01 and 31," +"\n"
                                                        +"your day input is out of range.");
                                                        count=1;
                                                }

                                                 if ((month<1)|(month>12)){        // rejects months out of range
                                                        out.writeln("Month must be in the range 01 to 12." +"\n"

                                                       
+"your month input is out of range.");
                                                        count=1;
                                                }                                                              

                                                  if ((year<0)|(year>9999)){         // rejects years out of range
   
                                                     out.writeln("Years are between 0000 and 9999,"+"\n" 
                                                        +"your year input is out of range.");
                                                        count=1;
                                                }

                                                if ((month==4)|(month==6)|(month==9)|(month==11) && day>30){  // rejects month that should have 30 days
   
                                                     out.writeln("There are only 30 days in this month.");
                                                        count=1;
                                                }

                                                 else if (month==2){                  // starts checking Feb
                                                           
if (day>29){                  // rejects Feb days out of range
   
                                                                 out.writeln("February's day range must be in the 01 to 28,"

   
                                                                
+"(and 29 in a leap year).");
                                                                    count=1;
                                                           }              

                                                            if ((day==29 & (year%4!=0 && year%100==0 || year%400!=0)){     //rejects no leap year 29 days
   
                                                                 out.writeln("There are only 28 days in February year " +year);
                                                                    count=1;
                                                           }
                                                 }

                                                if (count==0){                 // each rejection has a count of 1, unless count is 0 the input is rejected
  
                                                         if (day < 10 && month >= 10 && year >=1000){                      // tidy up of date printout format
  
                                                                 out.writeln("The date 0" +day +"-" +month +"-" +year +" is accepted."+"\n");
                                                           }
                                                           else if (day >= 10 && month < 10 && year >=1000){                // tidy up of date printout format
                                                                    out.writeln("The date " +day +"-0" +month +"-" +year +" is accepted."+"\n");
                                                           }
                                                           else if (day < 10 && month < 10 && year >=1000){                  // tidy up of date printout format
                                                                    out.writeln("The date 0" +day +"-0" +month +"-" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day >=10 && month >= 10 && year <100){                    // tidy up of date printout format
                                                                    out.writeln("The date " +day +"-" +month +"-00" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day <10 && month >= 10 && year <100){                      // tidy up of date printout format
                                                                    out.writeln("The date 0" +day +"-" +month +"-00" +year +" is accepted."+"\n");
                                                           }
                                                          
else if(day >=10 && month < 10 && year <100){                     // tidy up of date printout format
                                                                    out.writeln("The date " +day +"-0" +month +"-00" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day <10 && month < 10 && year <100){                        // tidy up of date printout format
                                                                    out.writeln("The date 0" +day +"-0" +month +"-00" +year +" is accepted."+"\n");
                                                           }
                                                          
else if(day >=10 && month >= 10 && year <1000){                  // tidy up of date printout format
                                                                    out.writeln("The date " +day +"-" +month +"-0" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day <10 && month >= 10 && year <1000){                    // tidy up of date printout format
                                                                    out.writeln("The date 0" +day +"-" +month +"-0" +year +" is accepted."+"\n");
                                                          
}
                                                           else if(day >=10 && month < 10 && year <1000){                   // tidy up of date printout format
  
                                                                 out.writeln("The date " +day +"-0" +month +"-0" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day <10 && month < 10 && year <1000){                      // tidy up of date printout format
  
                                                                 out.writeln("The date 0" +day +"-0" +month +"-0" +year +" is accepted."+"\n");
                                                           }
                                                           else if(day >= 10 && month >= 10 && year >=1000){               // tidy up of date printout format
  
                                                                 out.writeln("The date " +day +"-" +month +"-" +year +" is accepted."+"\n");
                                                           }
                                                }

                                                 if (count >=1)                         // each rejection has a count of 1, unless count is 0 the input is rejected
  
                                                        out.writeln("The date you input has not been accepted"+"\n");                     

                                                in.close();
                                               
out.close();

                                }
                     }