September 30, 2020

CS 170: Introduction to Computer Science I – Summer 2020 Homework Assignment #3

7 min read

1
CS 170: Introduction to Computer Science I – Summer 2020
Homework Assignment #3
Submission instructions
Submit your assignment as a zip file and save each program as .java file. Name the program based on problem
numbers. If you submit with a teammate, select team size 2. No emailsubmissions 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: Mid 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.
Problem 1 rubric:
2 points for either the individual, or if a team, both team mates complete the survey.
No points if any member did not complete the survey.
Problem 2: Average length (4 points)
Write a method named avgLength which takes an array of Strings as an input parameter and returns a double. The
method should calculate and return the average length of all the strings in the array.
Examples:
avgLength(new String[]{“Hello”, “Q”}) returns 3.0
avgLength(new String[]{}) returns 0.0
avgLength(new String[]{“Hello”, “Goodbye”}) returns 6.0
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 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
2
-1 if there are no comments, insufficient comments, or bad usage of comments
Problem 3: Sum of diffs (4 points)
Write a method named sumOfDiffs which takes an array of integers as input and returns an integer. The return value
is calculated by summing up the values that occur when you subtract each element from the preceding element.
Examples:
sumOfDiffs(new int[]{3, 4, 5}) returns -2 (3-4 + 4-5)
sumOfDiffs(new int[]{4, 1, 19, 6}) returns -2 (4-1 + 1-19 + 19-6)
sumOfDiffs(new int[]{}) returns 0
sumOfDiffs(new int[]{3, 0, -1}) returns 4 (3-0 + 0-(-1))
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 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 essay papers
comments, or bad usage of comments
Problem 4: Without duplicates (4 points)
Write a method named withoutDuplicatesthat takes an array of integers and returns a copy of the array without any
duplicate elements.
Examples:
withoutDuplicates(new int[]{1, 2, 3}) returns {1, 2, 3}
withoutDuplicates(new int[]{1, 2, 1, 1, 3, 2, 3}) returns {1, 2, 3}
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 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: Reverse copy (4 points)
Write a method named reverseCopy that takes an array of integers and returns a copy of the array with its elements
in reverse order.
Example:
reverseCopy(new int[]{1, 2, 3}) returns {3, 2, 1}
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 points for a partially correct solution. Zero points if the solution is far from
3
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: Reverse in place (4 points)
Write a method named reverse that takes an array of integers and reverses the order of its elements. The original
array is modified and the method doesn’t return anything.
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 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: Tally vowels (4 points)
Write a method named tally that takes a String and returns an array of 5 integers containing the frequencies of the 5
vowels (a, e, i, o, u) in the input string. Uppercase and lowercase vowels are counted in the same way.
Example:
tally(“HEY! Apples and bananas!”) will return: {5, 2, 0, 0, 0}
Rubric:
programs that do not compile get zero points
+4 correct implementation (2 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 8: Student averages (4 points)
Write a method named studentAverages which takes a 2D array of integers as input. Each column in the 2D array
is an assignment and each row is composed of grades for a particular student (see below for an example). Your
method should return an array of doublesrepresenting the grades for each student. Youmay assume each assignment
is scored out of 100 points.
Quiz 1 Quiz 2 Quiz 3
Maggie Simpson 50 100 0
Lisa Simpson 100 100 80
studentAverages(new int[][]{{50,100,0}, {100,100,80}}) returns {50.0, 93.33333}
Rubric:
programs that do not compile get zero points
4
+4 correct implementation (2 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 9: Credit card checksum (5 points)
Most e-commerce websites these days take credit cards. Users must enter their credit card number and the merchant
verifies that the number is valid. Then Visa, Mastercard, or AmEx process the payment to the merchant and pass
the bill along to the user. However, users often mistype their credit card number by one or two digits. These
common errors are why credit cards are designed with a secret. Using just the credit card number, we can detect
(most) mistakes and errors caused by user mistakes/mistypes. The credit card number contains an error control
code called a “checksum”. Specifically, the credit card number is formatted to comply with a Luhn-10 checking
algorithm.
The Luhn-10 algorithm is a weighted algorithm. Each digit in the credit card number is multiplied by a weight.
These weights are then summed, forming the checksum. The checksum is divided by 10. If the remainder is 0, the
credit card number is valid. If the remainder is NOT 0, the user made an error and can be prompted to re-enter their
credit card data. The weighting for the Luhn-10 algorithm is as follows:
Beginning with the first (i.e., leftmost) digit in the credit card, every other number is multiplied by 2. If the product
results in a 2 digit number (e.g., 6 x 2 = 12) then the individual digits (e.g., 1 and 2) are added to the checksum. The
remaining digits of the credit card number are simply added to the checksum. That is, their weight is 1.
Some examples are given below, but this algorithm will work with your Visa or Mastercard number. Try it!
Write a method named luhnChecksum which takes an array of integers as an input parameter and returns the integer
checksum computed by the above algorithm.
Examples:
luhnChecksum(new int[]{4,5,6,3,9,2}) returns 30 (see below for full calculation)
luhnChecksum(new int[]{4,9,9,1,6,5,7}) returns 40 (see below for full calculation)
Example 1: 456392
digit: 4 5 6 3 9 2
multiplied by: 2 1 2 1 2 1
product: 8 5 12 3 18 2
checksum: 8 + 5 + 1+2 +3 + 1+8+ 2 = 30
Conclusion: This is a valid number since 30 % 10 == 0
Example 2: 4991657
digit: 4 9 9 1 6 5 7
multiplied by: 2 1 2 1 2 1 2
Product: 8 9 18 1 12 5 14
checksum: 8 + 9 +1+8 + 1 + 1+2 +5 + 1+4 = 40
Conclusion: This is a valid number since 40 % 10 == 0
Rubric:
programs that do not compile get zero points
5
+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 10: Swear word filter (5 points)
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.
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)
-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 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 24 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!

Copyright © All rights reserved. | Newsphere by AF themes.