Before beginning any work, do a Team...Pull in your repository.
Important! Today you must rotate who is doing the typing as you move from one method to the next. Every person in your group must have a chance to be the lead person at the keyboard, for at least one method described below.
All group members are encouraged to help the lead person at the keyboard.
In preparation for the exercises below, open in eclipse the following, found in the studios source folder of your repository:
You can run the JUnit test even before all of the methods are written into your Methods class file. If challenged, persevere and run the JUnit test anyway. The output you should expect is described below.
In the work you see below, you should be asking yourselves the following as you write code:
- What is the name of the method I must create? The name is important, and if you get it wrong, the test code won't be able to test your code. The case of every character must agree with what you are asked to write. The method Sum is a different method to Java than the method named sum.
- What are the types of the input paramters? These must also agree with the specification.
- What is the type of the return value, if any? This must agree with the specification.
This has already been completed for you to serve as an example.
- To run the JUnit test, go to the Package window, right- (control-) click on MethodsTest, drag down to Run as, and select JUnit Test.
- The tests will now run and this should not take much time.
- When the tests have completed, look at the the JUnit window, which should now be open on the left of your screen.
- You should now see a a reddish stripe, indicating that at least one test failed.The tests that failed did so because you have not yet written the methods for those tests. So don't worry (yet).
- You should see just in front of the testSum entry a happy green check mark, because that test did pass.
- Take a look at MethodsTest and the testSum method therein. See how it usesassertEquals(7, Methods.sum(3,4));to test the sum method?
- Add another line in testSum that ensures the sum method works on negative values as well.
If the code you are given works for thisTestShouldFail, change it so that it does fail: change it so that the first parameter of assertEquals is 1 instead of 0 or 10.
Make sure you understand what you see. The trace shows you the value that was expected and the value that was computed.
It is easy to do this through eclipse. Above your method declaration, simply type /** and hit enter, and eclipse will automatically generate the JavaDoc block for you!
Hint Call the method you already wrote to compute the sum instead of rewriting or copying code.
Give your method a name and a specification (input types and return type), and implement it in the Methods class.
In the MethodsTest class, insert testing code like what you see for the other methods.
You must precede the test method by a @Test directive. Get help if necessary.
Some helpful information:
- If s is a String, then s.substring(0,1) returns the first character of s.
- If s is a String, then s.substring(1) returns all but the first character of s.
- For more information, see substring.
Before you write the code, look at the test! All you have to do to satisfy the test is to return a string that begins with the input's second character, appends the input's first character, and then appends "ay" at the end.
This is not really pig latin, but it does satisfy the test I gave you.
If you want to go for the real thing, read the article and recode so that your code does a true pig latin transformation. Add some more tests to show off your improvement. For example: scram → amscray.
We strongly suggest that you proceed one method at a time through the work below, testing each time to be sure what you have written so far is correct.
If you show up for help with a bunch of methods that don't work because you typed them all in before testing anything, we may not be able to help you until you clean up your code and focus on one method at a time.
sumDownBy2with the following specification.
PARAMETERS: an integer n RETURN VALUE: the sum of the positive integers n + (n-2) + (n-4) + ... EXAMPLES: sumDownBy2(7) is 7+5+3+1 = 16 sumDownBy2(8) is 8+6+4+2 = 20 sumDownBy2(0) is 0 sumDownBy2(-1) is 0
At this point you should have run the JUnit test and determined whether your sumDownBy2 works properly. Don't push ahead until you pass that part of the JUnit test.
Continue the lab in this fashion: write code, test the code, and then move forward.
harmonicSumwith the following specification.
PARAMETERS: a positive integer, n RETURN VALUE: the sum 1 + 1/2 + 1/3 + ... + 1/(n-1) + 1/n
Warning: Think carefully about this method's return type!
geometricSumwith the following specification.
PARAMETERS: a non-negative integer, k RETURN VALUE: the sum 1 + 1/2 + 1/4 + 1/8 + ... + 1/Math.pow(2,k)
multPosthat takes in two positive integers and returns their product.
PARAMETERS: positive integers j and k RETURN VALUE: the product j*k
You must accomplish this without using the multiplication operator. Use iteration and repeated addition to form the product.
multthat takes in two integers and returns their product. Each integer could be positive, negative, or zero.
PARAMETERS: integers j and k RETURN VALUE: the product j*k
Do this by calling your multPos method:
- Pass multPos the absolute values of j and k. It should return the product of those positive values.
- Compute this method's return value based on the result of multPos and based on the signs of j and k.
exptwith the following specification. Use repeated multiplication. (Do not use the built-in exponentiation method.)
PARAMETERS: integers n and k, where k >= 0 RETURN VALUE: the value of n to the power k EXAMPLES: expt(3,2) is 9 expt(5,0) is 1 expt(2,5) is 32
Before you demo, go back to your methods and type in JavaDoc comments above each method declaration. Recall that eclipse helps you do this if you start out by typing /** above a method declaration.
When you are done with this studio, you must be cleared by the TA to receive credit.
- Do a Team…Pull to update your repository. You must do this or the commit/push below may fail.
- Commit and push all your work to your repository.Make certain this has worked by logging into bitbucket. There you will see the commit(s) in your news feed if it was successful. You can also check the Source page to locate and ensure your code was received.
It is your responsibility to make certain the code has been pushed. Some of your work receives credit through testing of your pushed code. You will receive no credit for such work if you failed to push. We generally reserve the right to revoke credit for any of your work that has not been pushed on-time.
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in the TA's name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for studio 5