CS 151 Fall 2007 Lab 2:

Types, Variables, and Methods

 

The goal for this lab is to gain more experience manipulating object methods, using variables, and understanding types.  We will also learn about the Random method and see how to use and understand the online Java class documentation.

 

  1. Open the BlueJ environment either by selecting it from the Dock or navigating to it in the Applications folder and double clicking it.

  2. Mount your network directory/folder so that you can access the lab after you leave Olin 323.  Go to the Finder and from the Go menu select Connect to Server....  Type or choose afp://fileserver1/Personal and enter your password if prompted.

  3. From the Project menu select New Project.  Select your computer from the drop down menu.  Then scroll down and open the Volumes folder and open Personal inside it.  You should see a folder with your login name.  Create the new project in that directory and call it "Lab2".

  4. Create a class called "TypeTester" and create the public interface/declaration for the standard main method we have been discussing in lecture and lab.  Remove the default instance variable and method that BlueJ created in the class by default.  Precede the class declaration with the statement: import java.util.*;

  5. In your main method declare and initialize several variables of type int.  Display the sum, difference, product, and ratio of two numbers in the terminal window.  Compile and run your programs to make sure they do what you expect.

  6. Look at the documentation for the Random class.  Create a new object of that class using its constructor.  Then use that object's nextInt() method (the version that doesn't require any parameters) to get individual random integers.  Add, subtract, multiply, divide using a combination of your previously defined integer variables and the random numbers you generate.

  7. Create a new class called "DieSimulator".  Your class should have a single method called "rollDie" that returns an integer between 1 and 6.  Use the version of the nextInt() method in the Random class that lets you specify the maximum integer generated.  Methods that return a value have as their last line the statement:          return value; where value is a variable, constant, expression, etc. of the appropriate type.

  8. Using BlueJ you can create a new object of any class in your project by control-clicking (right-click in Windows) on the box for that class in the project window and selecting one of its constructors.  Choose a name for your object and click the OK button.  Then, control-click on the object you just created and select the desired method.

 

  1. In the same class, create another method of the same that takes a parameter called "maxValue".  This method should simulate rolling a single die with maxValue faces or sides.  Common dice from the Dungeons and Dragons days include 4-, 6-, 8-, 10-, 12-, and 20-sided dice.  In a real implementation, the version of the rollDie method that takes no parameters, would call the more general version of the method that does take a parameter.  What would be the reason(s) for and advantage(s) of doing this?

 

  1. In the main method of the TypeTester class, create several variables of type String.  Initialize some of them using the constructor for the String class.  Try to initialize a String variable using one of your int variables as a parameter to the String class constructor.  What happens?

 

  1. Suppose you want to convert the value contained in one of your integer variables into a String variable.  How might you accomplish this?  Hint: see the Integer class documentation.  You invoke static methods using the syntax: ClassName.methodName();

  2. Look at the documentation for the String class and experiment using some of its methods, e.g., toUpperCase(), toLowerCase(), both versions of replace() and substring(), and replaceAll().  Display your results in either the terminal window or a graphical dialog box.  If you are interested, find out about regular expressions and experiment with them in your replace() calls.

 

  1. Create a new class called "StringEncrypter".  Test each method you create before creating the next one.  You can test using the technique described above for DieSimulator, or add a main method to StringEncrypter, or create a new class called StringEncrypterTester that has a single main method.

a)        Pig Latin is an example of a simple code.  Of course it's not too hard a code to break once you know the key.  To convert a word to Pig Latin, move the first letter of the word to the end of the word and add "ay" to the end of the new "word".  For example, Java becomes avaJay.  Create method called "toPigLatin" that takes as a parameter and returns the Pig Latin version of that word.  What happens when you call your method on a two word phrase like "Good day"?  How might you parse (separate) a string of two words into (2) one-word strings?

b)        Add two versions of a method called "shiftLeft" that has the same return type as toPigLatin.  Note that for objects with multiple of the same name, all versions of that method must have the same return type.  One version should take a single string parameter and return the same string cyclically rotated leftward by a random number of characters (between 0 and the length of the string minus 1).  For example shiftLeft applied to "Hello" with a shift amount of 3 returns "loHel".  The other version should take a second integer parameter giving the shift amount.  What happens to your implementation if the given shift amount is more than the length of the word?  Create a similar pair of "shiftRight" methods if desired.  Might it be possible to combine shiftLeft and shiftRight methods into a single shift method?  What are the advantages and disadvantages to each approach?

c)        Add two versions of a method called "transpose".  Both versions should take a string and return a string.  One generates two random numbers and the other takes the two numbers as parameters.  The method should exchange the characters from the two positions.  For example, depending on your implementation invoking transpose with "Monday", 3, 4 would return either "Modnay" or "Monady" depending on whether you choose to start counting from 0 or 1. 

 

d)        Add a method called "substitute" that takes a string and two characters.  The method should replace all occurrences of the first character in the string with the second character.  What happens if the string contains mixed upper and lower case letters?  How should you handle the issue of case?   How might we generalize this method?  What potential errors might people make in using this method?  How could we randomize this method, i.e. make a version of this method that chooses a random character to substitute and a random character to replace it with?

 

e)        Create your own version of Pig Latin using one or more of your shift, transpose, and substitute methods.

 

Well done, or as the Japanese would say "Otsukaresama deshita".