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

Vocabulary: Identify the vocabulary word for each definition below. Try It/Solve

ID: 3857568 • Letter: V

Question

Vocabulary:
Identify the vocabulary word for each definition below.


Try It/Solve It:
1. List four symbols used in regular expressions and describe what each of them represents.
2. Your teacher just gave you the answers to the test (hypothetically of course)! The only problem is that the
answer key is in a secret code, with numbers and other characters that will never be answer choices.

The only hint at this time that the teacher gives you is that any character in the key that she gave you that is a, A, b,
B, c, C, d, D, e, E, f, or F is part of the answer key in the order it appears in the coded answer key and any other
character is not a part of the answer key. Each character is on its own line in the file provided by the teacher.
To decipher the coded answer key, complete the program below so that it properly reads each line of the file
CodedAnswerKey as a string line (this part is done for you), use a regular expression to check if line is 1 of the 12
options for an answer on the test if it is, add it to the string answers. Finally, print out answers.
import java.util.regex.*;
import java.io.*;
public class AnswerKeyProblem {
public static void main(String args[]) throws IOException
{
//read in the file provided by your teacher
BufferedReader codedAnswers =
new BufferedReader(new FileReader("CodedAnswerKey"));
//initialize String line as the first line of the file
String line = codedAnswers.readLine();
//keep reading each line and adding answers that match answer //possibilities to
string answers until there are no more lines in //the file
while(line!=null)
{
//read the next line of the file
line=codedAnswers.readLine();
}
}
}
3. It's almost time for your test and your teacher just announced that the answers only range from [a-dA-D]! She
gives you one last instruction for decoding her answer key, “replace all e's with b's, all E's with A's, all f's with c's and
all F's with D's. Then make the answer string all lower case so you can use it on the test . If your answers are not
all lower case, you may not use the answer sheet on the test !”
Write a static method finalAnswers that takes in String anwers that you created in problem 2 and returns the string
changed according to the teacher's final announcements.
4. There is a student in your class that loves to bake cupcakes for peoples' birthdays, but since there are so many
people in your class he cannot remember everyone's birthday! You decide to write a program that collects
everyone's birthdays into an ArrayList of Dates called birthdays so that your classmate won't forget anymore.
The Date class is given below. Write a program that reads in a birthday from the user input, use Scanner and
System.in to read the user input from the console as a string called bday. Each birthday will be entered in the format
MM/DD/YYYY.
Create a Pattern for the date using groups. The first group will be the first two digits, store this group as String
month. The second group will be the two digits for the day, do not include the slash (/) as part of any group and store
the second group as String day. The third and final group will be the four digit year of the student's birth, store this
as String year. Finally, create a new Date for each bday that you read in and add it to the ArrayList birthdays.
Hint: you will need to create a Matcher in order to store and access the groups of bday. If ever the string bday does
not match the pattern, you have reached the end of the birthdays and therefore are done adding birthdays to the
ArrayList.
public class Date{
public String month;
3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
public String day;
public String year;
public Date(String m, String d, String y){
month=m;
day=d;
year=y;
}
}
5. Given the following regular expressions, determine which of the following values for the String makes the matches
method return true. The ? Symbol represents 0 or 1 occurrences of any character, the brackets [Bb] will only include
one occurrence of either 'B' or 'b', and the * represents 1 or more of any character.
1. str.matches(“?anana”);
str = “anana”;
str = “banana”;
str = “gabanana”;
2. str2.matches(“[Bb]anana”);
str2 = “banana”;
str2 = “anana”;
str2 = “shanana”;
3. str3.matches(“*anana”);
str3 = “montanana”;
str3 = “anana”;
str3 = “_anana”;

1)Any symbol in regular expressions that indicates the number of occurrences
a specified character appears in a matching string.--------? 2)A regular expression symbol that represents any character will create a
match.------------? 3)A class in the java.util.regex package that stores the format of a regular
expression.-----------? 4)Segments of regular expressions starting with “(“ and ending with”)”, which
may later be called by the Matcher method group(groupNumber).-------------? 5)Used in regular expressions to allow character variability; may contain either
a specified range of characters of a group of character options.----------? 6)A class in the java.util.regex package that stores the possible matches
between a Pattern and a String.-------------? 7)A character or sequence of characters that represents a string or multiple
strings and allows for variability in matches.--------?

Explanation / Answer

Vocab=============================

1."+" , e.g Y+ Finds one or several letter Y

2.".", matches any character

3."Pattern" class is used for storing patterns

4."Grouping"

5."[]", e.g [a-z] will seek for a charcter ranging from "a" to "z"

6."Matcher" class

7."[a-z0-9]+", search for a charcter ranging from a-z or 0-9, one or more times

====================================================

1.

2.

import java.util.regex.*;
import java.io.*;
public class AnswerKeyProblem {
public static void main(String args[]) throws IOException
{
//read in the file provided by your teacher
StringBuilder temp = new StringBuilder();
BufferedReader codedAnswers =new BufferedReader(new FileReader("data.txt"));
//initialize String line as the first line of the file
String line = codedAnswers.readLine();
//keep reading each line and adding answers that match answer
//possibilities tostring answers until there are no more lines in
//the file
while(line!=null)
{
//String class has inbuilt support for regex, hence can be used for simple regex
if(line.matches("[a-fA-F]")){
   temp.append(line.trim());
}
//read the next line of the file
line=codedAnswers.readLine();
}

String answers=temp.toString();
System.out.println(answers);

}
}

3. Add and use this in previous code

static String finalAnswers(String answers){
   answers=answers.replace('e', 'b');
   answers=answers.replace('E', 'A');
   answers=answers.replace('f', 'c');
   answers=answers.replace('F', 'D');
  
   return answers;
}

4.Add the date class given in the question

public class CupCakesRegex {

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       String line="";
       Pattern pattern = Pattern.compile("([0-9]{2})/([0-9]{2})/([0-9]{4})");
       String day="";
       String month="";
       String year="";
       ArrayList<Date> dates = new ArrayList<>();
      
       while(in.hasNextLine()){
           Matcher matcher = pattern.matcher(in.nextLine().trim());
           if(!matcher.matches()){
               break;
           }
          
           month=matcher.group(1);
           day=matcher.group(2);
           year=matcher.group(3);
          
           Date date = new Date(month,day,year);
           dates.add(date);
       }
      
   }

}


5.

==================================================================================