Question

In: Computer Science

FYIPI (Find yourself in PI) In this assignment you will find a numeric string (if it...

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

Solutions

Expert Solution

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.


Related Solutions

JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
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...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations...
Assignment Instructions Module 4 Writing Assignment (M1-4) You will soon find yourself fulfilling roles in organizations where the ability to think ethically about issues will make you much more valuable to employers. In this activity you will refer to the materials in your text and lectures to respond to the questions. Part A The questions in Part A refer to the material discussed in Module 1 of this course. Respond to the following. Discuss reasons why ethics are an important...
this is a C++ class Topics Numeric Input (Whole Numbers) Assignment Finding Quotient Finding Remainder Numeric...
this is a C++ class Topics Numeric Input (Whole Numbers) Assignment Finding Quotient Finding Remainder Numeric Output (Whole Numbers) Description Write a program that will convert a distance entered in inches to a distance in yards, feet and inches. The program will ask the user to input a distance in inches as a whole number. It will convert the distance entered into yards, feet and inches. Then it will display the distance in yards, feet and inches. For example, the...
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Assignment Reflections This is a reflection assignment to thoroughly examine yourself in the context of technical...
Assignment Reflections This is a reflection assignment to thoroughly examine yourself in the context of technical project management to understand what you can do, want to do, need to do, and will do to improve what have you learnt about yourself through technical project management. In this assignment, examine, discuss, and provide a complete two-page document by answering, but not limited to, the following questions, for example: What personality type are you and what does that mean about your management...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a...
Your assignment for this program is to evaluate a numeric expression in postfix notation using a dynamic (pointer based) stack. As stated in the handout, in order to evaluate a numeric expression, a compiler converts an infix numeric expression to postfix notation and then it uses an algorithm and a stack to evaluate the expression. Your program should implement the pseudocode algorithm described in the attached handout. Your program will read and evaluate expressions stored in an input file (infile.txt)....
Assume that you are a healthcare administrator, and you find yourself dealing with this exact issue...
Assume that you are a healthcare administrator, and you find yourself dealing with this exact issue regarding a patient giving birth in your facility. Prepare a 500-word paper, double-spaced and in APA style that presents the most important legal and ethical issues relevant to this topic from both perspectives. And then state your position on this issue and describe how you will handle it in your hospital. Copy and Paste. Please do not use any prior answers. Thanks.
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
You wake up one morning and find yourself in a room with Ren and Stimpy. You...
You wake up one morning and find yourself in a room with Ren and Stimpy. You are not sure how you got there but that is irrelevant for the problem. Ren and Stimpy each have an allocation of pizza and beer. You obtain the following information through a pleasant, yet somewhat strange, conversation. Ren will trade two pizzas for one six-pack of beer and be equally happy. At the same time, Stimpy will gladly exchange six pizzas for two of...
You’re at an arcade and you find yourself standing at a claw crane trying to win...
You’re at an arcade and you find yourself standing at a claw crane trying to win a prize One attempt at a prize will cost you a quarter. As it turns out, you’ve got a Ziploc bag full of quarters, and you decide you’ll play until they’re all gone. Each prize in the machine that you can try to pluck with the claw has some dollar value, but you can’t tell how much each item is worth, or even which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT