Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 170: Introduction to Computer Science I – Spring 2022

Homework Assignment #5

Submission instructions

Submit your assignment through the QTest system, using exam ID: CS170.hw5.

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: End of the semester survey (2 points)

Go to the following website and complete the survey. The survey should be done individually: each team member will fill in a separate survey. To earn credit for this survey, all members of the team should submit it. If one of the teammates does not submit, the whole team will lose the points.

The answers to the survey will not be graded: you will receive credit just for completing it.

https://docs.google.com/forms/d/e/                               1FAIpQLSeTUxI2344vmp7BentJN__7EZ0ROdjJix3mIcJQNrpynCVKJg/viewform

Rubric:

2 points for completing the survey.

 

Problem 2: Valid password (7 points)

Using regular expressions, 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("ALphaa%") returns false


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)

-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 email address (7 points)

Using regular expressions, write a method named  isValidEmail that takes a string as input parameter, and re- turns  true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format“user123@domain.ext”, 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 ("user_123@gmail .com") returns true

isValidEmail ("user123alpha@gmail .com") returns false

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)

-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 4: Extract movie title (7 points)

Using regular expressions, write a method extractTitle that takes a string as input parameter representing IMDB movie information (written in XML style), and extracts all of the text between the tags <title> and </ title>. Your method can assume that there will be at most one movie title inside the input string.

Example:

extractTitle("<item><title>Split (2017)</title><meta><imdb>6375308</imdb></meta>")

returns "Split (2017)"

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)

-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 5: Robust division (7 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 resum- ing 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:

(4/2)+(6/4)+(0/6)

(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

+7 correct implementation (up to 5 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 6: Swear filter using regular expressions (10 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 follow - ing 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

+10 correct implementation (up to 7 points for a partially correct solution. Zero points if the solution is far from correct)

-3 for swear-word matching being case-sensitive

-3 for not maintaining original upper/lower-case

-3 for not matching strings that contain swear words as substrings

-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.