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

JAVA: (Find yourself in PI) In this assignment you will find a numeric string (i

ID: 3785422 • Letter: J

Question

JAVA: (Find yourself in PI)

In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available for download from D2L. Your code should prompt the user for their birthday and then verify that the string entered by the user is indeed a valid date string. Invalid date strings include the following:

013284 // No month has more than 31 days

022905 // 2005 was not a leap year

150184 // There are only 12 months

093196 // September only has 30 days etc.

Thus, a birth date string is of the form: mmddyy where mm is a two digit string representing the month, dd is a two digit string representing the day and yy is a two digit string representing the year. Once a birth date string has been entered by the user and validated, you will open pidigits.txt and read in a single character at a time using a BufferedReader instance. If a non-digit character such as a space, tab, or blank line is read, discard it. If the character is a digit character, “keep it” until a match/non-match has been established. As you read the characters, if there is a match between the birth date string and the characters read, report this to the user. Your code must find all such matches in the file, report all such matches to the user, report the character position in the file at which a given match begins, report the number of comparisons made in establishing the match, and report the total number of comparisons used in reading the entire file.. The format used for reporting is:

Your birthday 022348 was found at character position 1013664 in pidigits.txt

The number of comparisons so far is: 919560

Your birthday 022348 was found at character position 1073634 in pidigits.txt

The number of comparisons so far is: 973942

The total number of comparisons made in reading this file is: 1111091

If there are no matches for the birth date string, then your code should simply output the total number of comparisons made in reading the file. Constraints:

Your must use BufferedReader for all IO. No Scanner instances nor JOptionPanes allowed. Failure to observe this constraint will result in a significant loss of points.

Each character in pidigits.txt will be read in exactly once using the read() method of the BufferedReader class. You must use the version of read() that takes no parameters. You do not need to “back up” and reread characters that have already been read. Failure to observe this constraint will result in a significant loss of points.

You are not allowed to store the characters read in an array or any other kind of container. They are to be stored in char variables only. It should be obvious that you need only 6 such variables for this assignment. Failure to observe this constraint will result in a significant loss of points. You are not allowed to use any methods from the String class for finding matches.

You may and should use the isDigit() method. The isDigit() method is a static method in the Character class. Failure to observe this constraint will result in a significant loss of points. Your code must validate that the birth date string entered by the user is indeed a valid date. You must do this by passing the string as a parameter to a constructor of some sort of Date class. The Date class can be one that you build from scratch or can be based on one of the Date classes in the Java API (Calendar, GregorianCalendar, etc.). The code in your Date class is allowed to use methods from the String class. Failure to observe this constraint will result in a significant loss of points.

The name of the file containing your main() method must be YourFirstNameYourLastNameIV.java. The name of the file containing your code for your Date class must be BirthDate.java. Failure to observe this constraint will result in a significant loss of points.

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Scanner;


public class BirthDateTester {

   /**
   * @param args
   */
   //Change the name of this class to YourFirstNameYourLastNameIV
   public static void main(String[] args) {
       System.out.println("Enter your birth date: ");
       Scanner scanner = new Scanner(System.in);
       String birthdate = scanner.next();
       try {
           BirthDate date = new BirthDate(birthdate);
       } catch (NumberFormatException e1) {
           System.out.println("Invalid character in the date");
           System.exit(0);
       } catch (Exception e1) {
           System.out.println(e1.getMessage());
           System.exit(0);
       }
       String readDate = "";
       FileInputStream inputStream;
       try {
           inputStream = new FileInputStream(new File("pidigits.txt"));
           InputStreamReader reader = new InputStreamReader(inputStream);
           BufferedReader buffReader = new BufferedReader(reader);
           int digit;
           int counter = 0;
           char chars[] = new char[6];
           int charCounter = 0;
           int compCounter = 0;
           while((digit = buffReader.read()) != -1)
           {
               char ch = (char)digit;
               counter++;
               if(Character.isDigit(ch))
               {
                   chars[charCounter++] = ch;
                   compCounter++;
                   if(charCounter>5)
                   {
                       String sub = chars.toString();
                       if(sub.equals(birthdate))
                       {
                           System.out.println("Your birthday "+ birthdate + " was found at character position " + counter + " in pidigits.txt");
                           System.out.println("The number of comparisons so far is: " + compCounter);
                       }
                   }
               }
           }
           System.out.println("The total number of comparisons made in reading this file is: " + compCounter);
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
      

   }

}


public class BirthDate {
  
   private int month;
   private int day;
   private int year;
  
   public BirthDate(String date) throws NumberFormatException, Exception {
      
   this(Integer.valueOf(date.substring(0,2)), Integer.valueOf(date.substring(2,4)), Integer.valueOf(date.substring(4,6)));
      
   }
   BirthDate(int month,int day,int year) throws Exception
   {
       if(month > 0 && month < 13)
       {
           this.month = month;
       }
       this.year = year;
       switch(month)
       {
       case 1:
       case 3:
       case 5:
       case 7:
       case 8:
       case 10:
       case 12: if(day < 32) this.day = day; else throw new Exception("Invalid Date format");break;
       case 2: if(isLeapYear(year) && day <30) this.day = day; else throw new Exception("Invalid Date format");break;
       case 4:
       case 6:
       case 9:
       case 11: if(day<31) this.day = day;else throw new Exception("Invalid Date format"); break;
       }
   }
  
   private boolean isLeapYear(int yr)
   {
       if((yr % 4 == 0) && (yr % 100 != 0))
           return true;
       else if(yr % 400 == 0)
           return true;
       else return false;
      
   }
   //getters and setters for the data variables in date class
   public int getMonth() {
       return month;
   }

   public void setMonth(int month) {
       this.month = month;
   }

   public int getDay() {
       return day;
   }

   public void setDay(int day) throws Exception {
       switch(month)
       {
       case 1:
       case 3:
       case 5:
       case 7:
       case 8:
       case 10:
       case 12: if(day < 32) this.day = day; else throw new Exception("Invalid Date format");break;
       case 2: if(isLeapYear(year) && day <30) this.day = day; else throw new Exception("Invalid Date format");break;
       case 4:
       case 6:
       case 9:
       case 11: if(day<31) this.day = day; else throw new Exception("Invalid Date format"); break;
       default: throw new Exception("Invalid Date format");
       }
      
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }
   //checks if this date is after other date
   public boolean after(BirthDate other)
   {
       if(this.year > other.year || (this.year == other.year && this.month > other.month)
               ||(this.year == other.year && this.month == other.month && this.day > other.day))
           return true;
       else return false;
   }
   //checks if this date is before the other date
   public boolean before(BirthDate other)
   {
       if(this.year < other.year || (this.year == other.year && this.month < other.month)
               ||(this.year == other.year && this.month == other.month && this.day < other.day))
           return true;
       else return false;
   }
   //checks if this date and other date are equal
   public boolean equals(Object o)
   {
       BirthDate other = (BirthDate)o;
       if(this.year == other.year && this.month == other.month && this.day == other.day)
           return true;
       else return false;
   }
  
   //returns the date in DD/MM/YYYY format
   public String toString() {
      
       return day + "/" + month + "/" + year;
   }
}

more coming....