08 Jun ASSIGNMENT 2 Loops, loops, and more loops! COMP-202B, Winter
Question
ASSIGNMENT 2
Loops, loops, and more loops!
COMP-202B, Winter 2013, All Sections
Due: February 25th, 2013 (23:30)
Please read the entire pdf before starting.
You must do this assignment individually and, unless otherwise specified, you must follow all the general
instructions and regulations for assignments. Graders have the discretion to deduct up to 10% of the value
of this assignment for deviations from the general instructions and regulations. These regulations are posted
on the course website. Be sure to read them before starting.
Part
Part
Part
Part
1:
2a, Question 1:
2a, Question 2:
2b, Question 1:
0 points
50 points
15 points
35 points
100 points total
It is very important that you follow the directions as closely as possible. The directions, while
perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting
them run your assignment through automated tests. While these tests will not determine your entire grade,
it will speed up the process significantly, which will allow the TAs to provide better feedback and not waste
time on administrative details. Plus, if the TA is in a good mood while he or she is grading, then that
increases the chance of them giving out partial marks 🙂
Part 1 (0 points): Warm-up
Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do
the second part of the assignment, which will be graded. If you have difficulties with the questions of Part
1, then we suggest that you consult the TAs or instructors during their office hours; they can help you and
work with you through the warm-up questions.
Some of these questions we haven’t yet gotten to in class as they rely on loops. The notes for loops are
posted currently and the notes for arrays will be posted shortly.
Warm-up Question 1 (0 points)
Write a method printTypeOfChar. This method should take as input a char and print the following.
If the char is upper case, your method should print UPPERCASE. If the char is lower case, your method
should print LOWERCASE. If the char is a numerical digit, your method should print NUMBER. If the char is
any other symbol, your method should print SYMBOL. Note that it is not necessary to make 52 different
cases if you consider the order of the Unicode chart and the fact that you can use the < and > operators
on them.
Warm-up Question 2 (0 points)
Write a method countUppercase. Your method should take as input a String and return an int
1
representing the number of upper case letters in the String. Now add a main method where you use
the Scanner class to read a String from the user and call your method, outputting the returned result.
To access a character within a String named foo at a given position, you can write foo.charAt(int
expression) where int expression is any expression that evaluates to an int. Note that the first symbol
of a String is at position 0.
String words = "howdy";
char c = words.charAt(0); //c is now ’h’
char d = words.charAt(2 + 1); //d is now ’d’
char e = words.charAt(5); // error: program crashes because no such value
//since the counting starts from 0, the last letter is actually at position 4.
char f = words.charAt(words.length()); // same problem at charAt(5) above
Warm-up Question 3 (0 points)
A prime number is a positive number whose only even divisors are 1 and itself. Write a method isPrime
that takes as input an integer n and returns a boolean representing whether n is prime or not. Make
sure to handle cases where the integer is negative. (Your method should return false in these cases.)
Warm-up Question 4 (0 points)
x is a factor of y if y is a multiple of x. Write a method calculateFactorsSum. The method should
take as input an int n and return an int representing the sum of the factors of the number n.
Warm-up Question 5 (0 points)
x is a factor of y if y is a multiple of x. Write a method calculateFactors. The method should take
as input an int n and return an int containing all the factors of the number n.
Warm-up Question 6 (0 points)
Rewrite your question from the assignment 1 so that you can calculate the inverse of any student ID
instead of just a 4 digit student id. Put this code inside a method called reverseDigit. Note that to
store a large number you will want to use the type long instead of int.
Warm-up Question 7 (0 points)
Write a method lengthOfVector that takes as input an int vector and returns a double calculated
by first squaring all the terms in vector, then calculating the sum of this and finally taking the square
root of the sum.
Warm-up Question 8 (0 points)
Write a method dotProduct which takes as input two int and calculates the dot product of these
two arrays. (Remember you can take the dot product of 2 vectors by multiplying each coordinate by
the corresponding element and then summing the results.)
Part 2
The questions in this part of the assignment will be graded.
We have provided you with code that will AID you in testing your code. This is not meant as a substitute
for testing the code on your own!
Your code must at least compile with the test cases. If your code compiles with the test cases but
does not pass all the test cases, you will lose some points on the question, but it will be penalized as in other
mistakes–that is, you’ll lose a few points for the specific cases it fails, but that will be all. However, you’ll
lose significant points if it does not compile with the test cases since in this case your code would not have
passed any test cases. The reason for this requirement is the test cases will make things much easier for the
TAs to grade. If your code doesn’t even run with the test cases then it is difficult to grade. If you are
having trouble getting your code to compile with the test cases, please see an instructor or a
TA and they will help you!
Page 2
You should not use Scanner or have print statements in any of the required methods with the
exception of System.out.println statements being allowed inside of the main method of part
2a. It is recommended that you add print statements temporarily to help you debug your program when
you get the wrong result, but you should remove these before handing the assignment in. You may choose
to put Scanner into your ungraded main method for the second question if you like, but another potentially
faster option is to hard code the numbers into your main method. If you are finding yourself putting
Scanner into the methods other than main, please see a TA or instructor for help and review
the notes on method calling.
Part 2a: Approximating sin(x)
The following question should be put into a class called TaylorSin.
The Math library contains a method that allows you to calculate the trigonometric function sin(x). You can
execute this method by writing Math.sin(e) for some expression e of type double. In this question, you will
initially pretend that this method does NOT exist and write your own approximation of sin(x) that does
not use any library methods. Then, to check your work at the end, you will call M ath.sin(x) and compare
the results.
Methodology: Due to properties of something called a Maclaurin series or Taylor series, you can approximate the function sin(x) using the following formula:
a
sin(x) ≈
(−1)n
n=0
x3
x5
x7
x(2a+1)
x(2n+1)
=x−
+
−
+ …
(2n + 1)!
3!
5!
7!
(2a + 1)!
where a is chosen to be a large number. We will use this formula to approximate sin(x).
Question 1: Writing the sin(x) method (50 points)
Write a method called sin that takes as input a double and returns a double. Your method should
approximate sin(x) using the above formula by setting the value of a to be a class constant with value
10. You may not use any library method (such as Math.pow) for this. You must use at least one loop
in your method.
Hint: If you are having trouble figuring out how to get started, start by making a smaller version of
this problem and creating a method out of it. In this case, you are asked to take the sum of many
terms. A simpler version of the question would be to calculate just one term. You could write a method
calculateTerm that took as input a double x and an int n and returned the nth term in the sequence.
After you write this method, test it, by calling it from the main method and verify that it works. To
calculate the sum, you can then call your method several times. Note that the method calculateTerm
might be further divided. (For example, you may want to write a method for calculating power or
factorial.)
Note: The approximation will only be close to the actual value with a=10 for small values
of x (between -1 and 1). For larger values, you would need to increase a. However, this is
not required for the assignment. Doing so in fact may cause numerical computation errors
related to overflow.
Question 2: Checking your sin(x) method (15 points)
In order to test your sin(x) method, you will do an experiment. To perform this experiment you should
do the following.
Write a main method in which you produce a table comparing, for every value of x from
-1 to 1 in increments of .01, the values of x, Math.sin(x), sin(x) (your version) and the
absolute value of the difference between the value returned by Math.sin and your version.
Page 3
The first column of your results should be x, the 2nd column should be Math.sin(x), the third column
should be your results, and the 4th column the absolute value of the difference. (The idea is we are
assuming that Math.sin(x) returns the correct value so if our results match it, then we must have coded
sin(x) correctly)
A sample run of the program and what your output should be is posted on the course webpage with the
file name SampleRunSin.txt. Your numbers may differ slightly due to rounding issues. (Notice that in
the sample output there are several rounding issues where the results don’t show up ”perfectly” in the
x column for example. This is fine.)
Part 2b: Writing an automated plagiarism detector!
In this question, you will write code that compares two Strings and looks for similarities. This would be
used to detect cheating. A sample main method is provided for you inside the file PlagiarismDetector.java
downloadable on the course webpage which you may use if you like or change as you desire. However, your
main method for this question will not be graded and should be used simply for testing. The provide code
will ask the user for the 2 files to read from and load them into Strings. Note that in order for it to work,
these two files must be inside the same folder as your .class file. If you are using eclipse you may need to
manually find the .class file and copy the .txt files into that folder. The program will only work on .txt files
(not .doc or .pdf for example).
You should write the following method:
Question 1: Plagiarism Detection (35 points)
Write a method compareStrings which takes as input two String and returns a boolean representing
whether your program concludes that the two strings are very similar to each other. A boolean result
of true indicates the Strings are very close to each other and false indicates otherwise. Your method
should determine this is based on the following rule:
If at least 90% of the words in the first String appear in the second String AND at least
90% of the words in the second String appear in the first String, your method should
return true. Otherwise your method should return false.
For the purpose of this assignment, a “word” is any sequence of characters other than newlines, tabs,
spaces, and other “white space” characters. For example the String ”abc d3; 33” is considered to have
3 words: abc, d3;, and 33. You should perform the check in a case sensitive fashion meaning that the
words hello and HELLO will be treated as different words.
Important hint: There is a library method defined on all Strings which allows you to produce a String
based on a delimiter by splitting the String into several pieces. To split a String into several pieces
based on “whitespace” (new lines, tabs, space, etc), you can call split in the following way. Assume the
String you want to split is called foo.
String pieces = foo.split("s");
For example, if foo contains the contents a bc def ghi then writing foo.split("s") will produce
a String with the contents a, bc, def, and ghi (remember that is the code for storing a new line
in a char or String). Note that the double backslash is necessary (as opposed to just one backslash)
because the method split is supposed to take as input a String of length 2, with the first character being
a backslash. Since backslash denotes special characters, a backslash itself is a special character!
Here are a few examples along with the expected results:
• ”a3 bb cc dd” vs ”bb cc dd a3” yields True
• ”aa bb cc” vs ”aa aa aa” yields False because only 1 of 3 of the words from the second String are
in the first
Page 4
• ”a b c d e f g h i j a a a a a a” vs ”a b c d e f g h i k” yields True since at least 90 percent of the
words from each String are included in the other.
• ”a a a a a a a a a a a a b” vs ”b b b b b b b b b b a” yields True because at least 90 percent of the
words from each String are included in the other. Even though a appears multiple times in the
first String the checker will only check that each at least 90 percent of the letters in one String
occur in the other. The Strings ”a” and ”b” both appear in both Strings and so it is a match.
• ”a a a a a a a a a b c” vs ”a c” yields True since there are 11 words in the first String. The 9 as
10
are all found in the second String, the b is not and the c is. So 11 of the words in the first String
occur in the second. (The reverse match is trivially true since all words in the second String occur
in the first.)
Note finally that you may make the following two assumptions: (That is, you do NOT need to handle
these cases)
• Neither of the String variables passed as input will be null
• Neither Strings will have consecutive white space characters. That is, you do not need to handle
cases with 2 spaces in a row for example.
Verifying your code
Run your code against the test programs to verify it. To do this, you should make sure that TaylorSin.java,
PlagiarismDetector.java, and AssignmentTwoTests.java are all in the same folder.
Then, compile all of them together by typing
javac *.java
OR
javac AssignmentTwoTests.java PlagiarismDetector.java TaylorSin.java
In Eclipse, Dr. Java, or the command prompt, you should make sure that the four files are inside the same
folder.
If the program does not compile, it means you are either missing a public method or one of the required
public methods does not take the correct arguments. Your code must compile with these test cases. If you
are not able to do so, you should ask a TA or instructor for help. After this, you may run the test program
by typing
java AssignmentTwoTests
As you are writing your code, you may want to test things immediately rather than waiting until you’ve
finished the assignment. In this case, a good thing to do is write the method headers and “skeleton methods”
with simple return expressions for the required methods. The test cases for the methods for which you have
only skeletons will still fail in most cases, but you’ll be able to run the test cases on the working ones.
What To Submit
You should submit your assignment on MyCourses. In order to do this, you will need to make a zip of the
file. You can do this on windows by following the instructions at this link: http://condor.depaul.edu/
slytinen/instructions/zip.html. On a mac or linux, you can find instructions at http://osxdaily.
com/2012/01/10/how-to-zip-files-in-mac-os-x/
You should submit a zip file called Assignment2.zip with the following files inside of it.
Page 5
TaylorSin.java
PlagiarismDetector.java
Confession.txt (optional) In this file, you can tell the TA about any issues you ran into doing
this assignment. If you point out an error that you know occurs in your problem, it may lead
the TA to give you more partial credit. On the other hand, it also may lead the TA to notice
something that otherwise he or she would not.
Page 6
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
