Studio 5: Writing and Testing Methods
Warmup
- First, form a group:
- All but one member of your group should have this web page open so you can follow along and see the instructions as you work
- Plan to have one computer at which your team does its work. Initially, one of you will be in charge of typing at that computer.
- Throughout the studio, you should trade who is in charge of the keyboard. Before doing so, commit your work to make sure your work is saved
Overview
In this studio you will explore the following two concepts:
- Writing methods that compute results of interest.
- Testing methods to develop assurance that the methods compute their results properly.
If necessary, review the videos and text material in chapter 2.1 concerning methods. Once your group is ready, move on to the next section where you will write and test some methods.
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:
Methodsis a file in which you will type the methods described below.MethodsTestis the JUnit test that you will run to see if your methods are working properly.
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.
Methods
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 parameters? These must also agree with the specification.
- What is the type of the return value, if any? This must agree with the specification.
sum
- Define a method that accepts two integers and returns their sum as an integer.
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 toRun as, and selectJUnit 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 uses
assertEquals(7, Methods.sum(3,4));to test the
summethod? * Add another line intestSumthat ensures thesummethod works on negative values as well.
mpy
This one is also done for you, but a mistake has been made on purpose, so you can see what happens when JUnit finds that your code does not pass a test.
-
When you run JUnit, it runs all the tests in the specified file.
-
A check mark appears next to the ones that work, and an X appears next to those that did not.
-
Click in the JUnit window on the test
thisTestShouldFail. -
Below in the Failure Trace window, you will see an explanation of why the test failed.
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.
-
Fix the problem, rerun the test, and verify that the test passes.
avg
-
Write a method
avg3that takes in 3 integers and returns their average, as andouble. -
Make sure it passes the unit test.
-
Fill in a JavaDoc block above your code to document what you have done. You will see the methods provided to you have this already.
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!
sumArray
- Write a method
sumArraythat takes in an array ofdoublesand returns their sum as adouble.
average
- Write a method
average** that takes in an **array** ofdoublesand returns their average as adouble`.
Hint Call the method you already wrote to compute the sum instead of rewriting or copying code.
Your call
-
As a team, think of a simple method. The method must return some kind of value so it can be tested.
-
Give your method a name and a specification (input types and return type), and implement it in the
Methodsclass. -
In the
MethodsTestclass, insert testing code like what you see for the other methods.
You must precede the test method by a
@Testdirective. Get help if necessary.
Pig Latin
- OK here's a fun one. Write a method named
pigthat takes in aStringand returns aString. That method should return the pig latin transformation of its input.
Some helpful information:
If
sis aString, thens.substring(0,1)returns the first character ofs.If
sis aString, thens.substring(1)returns all but the first character ofs.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