Lab 5

Complete the quiz (still <5 minute requirement), then read the following quick tutorial.  Then pair up, and work on the lab.

This week we'll be using the application written in Project 2 as a basis for exploring and becoming comfortable with method declaration, creation, invocation, and parameter passing.

To put it more simply, calling functions with arguments.  You may remember this from CS 101, but if you don't, or didn't take CS 101, this should get you up to speed.

When you define a function, it has a return type, a name, and a list of parameters which the function takes in.  This definition takes the form:

public [return type] [function name]([list of parameters])

For example, public int max(int a, int b) is a function which takes two integer parameters, naming them a and b for the function to use, and returns a single integer value.  If the name is indicative of the functionality implemented, it probably returns the value of the larger of the two numbers passed in.

To call this function then, as long as the function is in scope (we'll talk about this more next week), we can simply use max(x,y), where x and y are two integer values.  They can be variables or constants, or even other functions.  As long as when the calls are resolved, max only has to deal with two integers, it will be happy.

As an example, if I wanted to find the maximum of 4 integers, a, b, c, and d, I could use the following expression to do so.

max( max(a,b), max(c,d) )

The larger of a and b is compared with the larger of c and d, so the larger of these two is the largest of the four.

If it's still confusing, ask the TA.

So, for the lab, write the following functions.  I've provided sample code at the bottom of the page which demonstrates how I expect the functions to be used. 

In your submission, each of the 3 functions (fewer if you can't complete them all within the time span of the lab) should appear, as well as your own example for how each should be used.

1. For one point, write a function with 2 parameters (one string, one int), that returns a string containing the string sent as a parameter encoded with the transposition value sent as a parameter.

2. For another point, write a function taking in 4 parameters, that functions as follows:

The final parameter will be either -1 or 1, -1 for decoding, and +1 for encoding.  Note that encoding and decoding are inverse functions, so a string encoded with transposition value 8 should return the original string when decoded with transposition value 8.

The third parameter represents the direction in which the encoding/decoding should proceed, with 0 meaning to simply transpose each character, and 1 representing transposing the characters as well as reversing the string, as you did in Project 2.

The first and second parameters should be as they were for the first point, a string to encode/decode, and a transposition value.

See the sample code for an example of what these function calls should look like.

3. For a point of extra credit, write a function that takes in a String and an integer.  This function should, when given a string filled with words separated by spaces, return the nth word, where n is the integer passed to the function.  There is an example of what a function call to this function should look like as well.

Since the functionality of this class is so close to that of a StringTokenizer object, full credit will not be given for implementations which make use of the java.util.StringTokenizer class.  However, such an implementation may provide a good prototype or first try at writing the function, so doing so is encouraged.  Once you have it working with StringTokenizer, you can comment out that code and write your own implementation using String and character manipulation.

Note as well that though this extra credit does not rely as heavily on the earlier steps, it recommended that you complete the steps of the lab in the order given.

Submission:

You may work with a partner, and we recommend you do so in order to complete the lab during the lab session.

Be sure to include all authors' names, as well as the hour of the lab.

Submit the .java file (not the .class file) to blackboard within one hour of the start of your lab session.  

We understand it is tough to work under a time crunch, but work together with your partner, keep your cool, and plan ahead.  Work through one task at a time, and remember what you have learned.