CSE 131 Module 5: Methods


If necessary, review studio procedures before starting.



In this studio you will explore the following two concepts: If necessary, review the slides 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:


In the work you see below, you should be asking yourselves the following as you write code:
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 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 uses
       assertEquals(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.
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.
Write a method sumArray that takes in an array of doubles and returns their sum as a double.
Write a method average that takes in an array of doubles and returns their average as a double.
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 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.

Pig Latin
OK here's a fun one. Write a method named pig that takes ina String and returns a String. That method should return the pig latin transformation of its input.
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: scramamscray.

Submitting your work (read carefully)

Last modified 08:59:37 CST 09 January 2015
When you done with this studio, you must be cleared by the TA to receive credit.

Last name WUSTL Key Propagate?
(or your numeric ID) Do not propagate
e.g. Smith j.smith
1 Copy from 1 to all others
2 Copy from 2 to all others
3 Copy from 3 to all others
4 Copy from 4 to all others

TA: Password: