In: Computer Science
FYIPI
(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 asingle 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.
Please do not submit anything other than source code files. .class files, etc. are neither wanted nor welcome.
This is in Java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
public class BirthDate {
private static final SimpleDateFormat dateFormat
=
new
java.text.SimpleDateFormat("MMddyy");
private String date;
private int currentCharIndex;
// used for marking in
BufferedReader
private boolean isLast=false;
// used to detect the
complete match of string Date
public BirthDate(String readDate) {
this.date=readDate;
this.currentCharIndex=0;
}
public char getCurrentChar() {
char
ch=this.date.charAt(this.currentCharIndex);
if(this.isLast){
this.reset();
}else{
this.currentCharIndex++;
if(this.currentCharIndex==date.length()-1)
this.isLast=true;
}
return ch ;
}
public boolean isLast() {
return isLast;
}
public int getCurrentCharIndex() {
return currentCharIndex;
}
public void reset() {
this.currentCharIndex=0;
this.isLast=false;
}
public boolean validate() {
if(this.date.trim().length()!=6)
return
false;
if (this.date != null) {
try {
Date ret = dateFormat.parse(this.date);
if (dateFormat.format(ret).equals(this.date))
{
return true;
}
} catch
(ParseException e) {
}
}
return false;
}
public void display(int index, int comparisons)
{
System.out.println("Your birthday
"+this.date+" was found at character position "+index+" in
pidigits.txt");
System.out.println("The number of
comparisons so far is: "+comparisons);
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class YourFirstNameYourLastNameIV {
public static void main(String[] args) {
BirthDate date=new
BirthDate(readDate());
if(date.validate()){
try
(BufferedReader br = new BufferedReader(new
FileReader("pidigits.txt"))) {
int asciiInt;
int index=-1;
char dateChar;
int comparisons=0;
while ((asciiInt = br.read()) !=
-1) {
char ch = (char) asciiInt;
if(!Character.isDigit(ch))
continue;
dateChar =
date.getCurrentChar();
if(date.getCurrentCharIndex()==1){
// Marking Position for Next Comparision
br.mark(6);
}
if(ch==dateChar){
if(date.isLast()){
date.display(index,comparisons);
date.reset();
}
}else{
br.reset();
// Going
back to marked position
date.reset();
index++;
}
comparisons++;
}
} catch
(IOException e) {
e.printStackTrace();
}
}else{
System.out.println("Invalid Date");
}
}
public static String readDate() {
System.out.println("Enter Your
birth Date in mmddyy format");
String input=null;
try {
BufferedReader
inp = new BufferedReader (new InputStreamReader(System.in));
input=
inp.readLine();
} catch (IOException e) {
}
return input;
}
}
This solution uses the property of Buffered reader's ability to mark a position and then come back to that position. This is achieved by mark() and read() functions. For more knowledge on this reference java documentation of BufferedReader is advised.