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.
Go menu select Connect to Server.... Type or choose afp://fileserver1/Personal and enter your
password if prompted.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.*;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.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. 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.
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?
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?
String variable. How might you accomplish this? Hint: see the Integer class
documentation. You invoke static methods using the
syntax: ClassName.methodName();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.
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".