Introduction to computer science
Submission instructions If you submit with a teammate, please make sure that both of you submit separately in canvas. No email submissions are accepted. No late submissions are accepted. General instructions and hints in those problems asking you to write a method, always call the method several times to test that it works properly with a variety of different values for the parameters. Test it on more examples than the ones shown in this handout. Even if the problem asks you for just one method, you can always write additional helper methods to simplify and organize your code. Make sure you write comments to explain your code. Comment requirements: Always comment the top of each method (what the method does, the meaning of the input parameters, the meaning of the output value). Write comments within the methods to explain the strategy you are using to solve the problem, and to clarify blocks of code that may be difficult to understand.
Problem 1: Valid password (7 points) Write a method named isValidPassword that takes a string as input parameter, and returns true if that string represents a valid password, or false otherwise. A password is considered valid if (and only if) its length is between 6 and 8 characters (inclusive), and all of the requirements below are satisfied. You must use regular expressions to check if the following conditions are met:
• The password starts either with an upper case letter or with one of the following special characters: @ #
• The password’s first character is followed by at least 5, and at most 7, word characters (i.e., letters, digits, or underscore)
• The password’s last character is not equal to any of the following special characters: * . %
• The password does not contain any whitespace characters Examples: isValidPassword(“Tr7s6d_”) returns true isValidPassword(“@abc2-bc”) returns false isValidPassword(“Alpha%”) returns false Rubric: programs that do not compile get zero points +7 correct implementation (up to 3 points for a partially correct solution. Zero points if the solution is far from correct) -2 incorrect method signature (method name, number of parameters, and types of parameters) -2 if there are no test cases -2 if there are no comments, insufficient comments, or bad usage of comments
Problem 2: Valid email address (5 points) Write a method named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:
user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter
• domain represents a sequence of alphanumeric characters (i.e., letters or digits) whose length is between 1 and 12 (inclusive), but the first character must be a letter
• Exactly one character “@” separates the user name from the domain name
• ext. is a sequence of lower case letters only whose length is between 1 and 3 (inclusive)
Examples: isValidEmail (“[email protected]”) returns true isValidEmail (“[email protected]”) returns false Rubric: programs that do not compile get zero points +5 correct implementation (up to 3 points for a partially correct solution. Zero points if the solution is far from correct) -1 incorrect method signature (method name, number of parameters, and types of parameters) -1 if there are no test cases -1 if there are no comments, insufficient comments, or bad usage of comments
Problem 3: Valid file name (5 points) Write a method isValidFilename(String filename, String sys) which returns true if filename represents a valid file name according to the rules of the operating system in the second parameter sys, or false otherwise. Use regular expressions to match the following rules for different systems. You can assume that filename will never be passed as an empty string and will always include a file extension (i.e., file type). If the operating system sys is “Windows”, then:
• The file name cannot contain any leading or trailing whitespace characters
• The file name cannot contain any of these special characters: /? < > \ : * | . “
• The file name cannot end with com1, com2, …, or com9 (i.e., com followed by exactly one digit between 1 and 9, inclusive)
• The file name is separated from the file extension by exactly one period character
• The file extension can only contain lower case alphabet letters
• The file extension length is between 2 and 6 characters (inclusive) If the operating system sys is “Mac” or “Linux”, then:
• The file name cannot contain a period “.” character or a colon “:” character
• The file name is separated from the file extension by exactly one period character
• The file extension can only contain alphabet letters (upper case or lower case)
• The file extension length is between 2 and 6 characters (inclusive) Examples: isValidFilename(“homework5.java”, “Linux”) returns true isValidFilename(“hamlet. Shakespeare”, “Mac”) returns false isValidFilename (“rom_com3.txt”, “Windows”) returns false Rubric: programs that do not compile get zero points +5 correct implementation (up to 3 points for a partially correct solution. Zero points if the solution is far from correct) -1 incorrect method signature (method name, number of parameters, and types of parameters) -1 if there are no test cases -1 if there are no comments, insufficient comments, or bad usage of comments
in text and replace them with their American-English equivalent, and vice versa. To know which words to match and replace, your method will use the third parameter dictionary. The content of the String dictionary should follow the exact format shown below, in which each line starts with a word spelled in American-English, followed by a tab character “\t”, followed by the equivalent Canadian-English spelling of the same word, and ending with a new line character “\n”: “AmericanWord1\tCanadianWord1\nAmericanWord2\tCanadianWord2\n” For example, below are all valid values for the parameter dictionary: “color\tcolour\n” “color\tcolour\nfavorite\tfavourite\n” “color\tcolour\nfavor\tfavour\nhonor\thonour\nhumor\thumour\nlabor\tlabour\ncenter\tcentre\nmeter\tmetre\n” You can utilize Java’s split method (available for any String variable), to separate the different lines (or words) inside dictionary.
Notice that your method should recognize both uppercase and lowercase characters in matching words that need to be replaced, but the new words replacing the matches can be taken as-is from the given dictionary. Your method should return a String containing the new modified text. Examples: convertText(“I do not have a favorite COLOUR nor a favorite ice-cream flavor”, “US”, “color\tcolour\flavor\tflavour\n”) returns: “I do not have a favorite color nor a favorite ice-cream flavor” convertText(“I do not have a favorite colour nor a favorite ice-cream flavour”, “CA”,
“color\tcolour\nflavor\tflavour\nfavorite\tfavourite\n”) returns: “I do not have a favourite colour nor a favourite ice-cream flavour” Rubric: programs that do not compile get zero points +7 correct implementation (up to 5 points for a partially correct solution. Zero points if the solution is far from correct) -2 for word matching being case-sensitive -1 incorrect method signature (method name, number of parameters, and types of parameters) -1 if there are no test cases -1 if there are no comments, insufficient comments, or bad usage of comments
Problem 6: Robust division (5 points) Write the method sumOfIntegerDiv(int[] a, int n) which takes an array of integers a and an integer n as input and returns an integer. The return value is calculated by summing up the values that occur when you divide each element by the preceding element, until you stop at the n-th element in the array. Your method should be resilient against possible exceptions, such as dividing by zero or attempting to access an invalid array index. Instead of terminating when these exceptions occur, your method will instead skip the array index that generated the exception, print a friendly message to the user informing them why this index will be skipped, then resume computation normally (if possible). Your method should be able to catch at least two types of exceptions:
• If an ArithmeticException occurred, your method should print the following message (before resuming computation normally): Cannot divide by zero. Skipping index: index_value
• If an ArrayIndexOutOfBoundsException occurred, your method should print the following message (before returning the result): Cannot access array at index: index_value
• If any other type of exception occurred, then your method should print: Something went wrong! Skipping index: index_value Notice that you should replace “index_value” above by the value of the actual array index. Examples: sumOfIntegerDiv({2, 4, 6, 0, 8, 16}, 4) returns 3 (4/2)+(6/4)+(0/6) sumOfIntegerDiv({2, 4, 6, 0, 8, 16}, 5) returns 5 (4/2)+(6/4)+(0/6)+(16/8) The second call skips (8/0) and prints a friendly error message to the user: “Cannot divide by zero. Skipping index: 4” Rubric: programs that do not compile get zero points +5 correct implementation (up to 3 points for a partially correct solution. Zero points if the solution is far from correct) -1 incorrect method signature (method name, number of parameters, and types of parameters) -1 if there are no test cases -1 if there are no comments, insufficient comments, or bad usage of comments
Problem 7: Swear filter using regular expressions (7 points) Remember your swear filter method from Homework 3? Your job now is to implement a swear word filter using regular expressions. Write a method named swearFilter (String text, String [] swear) that takes two parameters: A String
containing some text, and an array of Strings containing a list of “swear words”. Your method will return a String containing the text contained in the first String, where each “swear word” is replaced by its first character, followed by a number of stars equal to its number of characters minus two, followed by its last character. For example, if the swear words are “duck”, “ship”, and “whole”, and the text contains the following story: A duck was sailing on a ship shipping whole wheat bread. Duck that SHIP!!! Your method would return: A d**k was sailing on a s**p s**pping w***e wheat bread. D**k that S**P!!! Notice that your method should recognize both uppercase and lowercase characters in a swear word. You must use regular expressions to solve this problem while utilizing Java’s replaceAll method in class String. Rubric: programs that do not compile get zero points +7 correct implementation (up to 5 points for a partially correct solution. Zero points if the solution is far from correct) -2 for swear-word matching being case-sensitive -2 for not maintaining original upper/lower-case -2 for not matching strings that contain swear words as sub-strings -1 incorrect method signature (method name, number of parameters, and types of parameters) -1 if there are no test cases -1 if there are no comments, insufficient comments, or bad usage of comments Bonus points: Early submission If you submit the entire homework no later than 48 hours before the deadline, and the total score on the rest of this homework assignment is at least 20 points, you will receive 2 bonus points. The bonus points will be added to the total score of this homework assignment. Good luck and have fun!