Exercises
1 Create a string named String1 with the following contents: This is the first string.
2. Create a second string named String2 with the following contents: This is the second string.
3. Create a third string named CombinedString that contains String1 concatenated with String2.
4. Print all three strings, appropriately labeled.
Notes for this lab
1.
Use the technique discussed in class (using the Scanner class) to prompt the user for input.*2. When printing output, use the System.out.println() method.
Requirements
Prompt the user to enter 2 separate strings. Name the strings FirstString and SecondString.
1. When prompted, enter
A roach is shorter than a mouse for FirstString
A Chicken is a dumb animal for SecondString.
2. Print the two strings, appropriately labeled.
3. Convert FirstString to upper case and print the result, appropriately labeled.
4. Find and print, appropriately labeled, the index of the first occurrence of the character c in FirstString.
1. Prompt the user to enter a sentence that states the user's name (My name is George, for example).
2. Prompt the user to enter a character shift from 1 to 4.
3. Print the sentence and the amount of the shift, appropriately labeled.
4. Encrypt the sentence using Caesar's shift.
5. Print the encrypted sentence, appropriately labeled.
6. Recover and print the original sentence, appropriately labeled.
Another version of Caesar's shift and one which is harder to crack requires the use of 2 shifts.
Same requirements as above except prompt the user to enter 2 shifts. The code will then alternate use
of the shifts provided.
For example, if the numbers entered are 2 and 3, then the first character will be shifted 2, the second will
be shifted 3, the next will be shifted 2, etc.
Assignment:
1. Create a class with two public static methods.
One will receive an int number and return a String with that int converted into Roman Numerals.
The other
method will receive a
String of Roman Numerals and
return the int
value of the Roman Numerals.
2.
These two methods are
static because there is no
reason to create an object just to run these calculations.
3.
Think of using helper methods to reuse algorithms needed to solve these
problems.
4.
Assume that your client gives you valid Roman Numerals and the Arabic
numbers are positive and less than 4000.
Instructions:
Roman Numerals work
differently than our normal Arabic number system.
Roman Numerals have symbols, all in capital letters (and sometimes in
lower case), which represent Arabic numbers.
Roman Numerals have been used for identifying movie sequels (i.e.,
The Godfather: Part II), for
publication copyright dates, for numbering monarchs such as Queen Elizabeth
II, and for numbering Super Bowls.
See the following table for the Roman Numerals symbols up to 1000.
|
Roman
Numeral |
Arabic Number |
|
I or i |
1 |
|
V |
5 |
|
X |
10 |
|
L |
50 |
|
C |
100 |
|
D |
500 |
|
M |
1000 |
Usually, numbers
are formed by stringing the Roman numerals together and adding them up to
make the required number (i.e., II = 2, or XII = 12). If smaller numbers
follow larger numbers, the numbers are added (i.e., VIII = 5 + 3 or 8), but
if a smaller number precedes a larger number, the smaller number is
subtracted from the larger number (i.e., IX = 10 - 1 or 9).
There is shorthand for
the case when there are four of the same symbols in a row.
Instead of IIII for 4, it is written as IV or 5 - 1 = 4.
This only applies to symbols that represent powers of ten.
Since our numbers will be less than 4000, this only makes sense for
I, X and C. Some people think
this means you can write IC for 99 but that is not going to be allowed.
When using this shortcut, a symbol can only precede a symbol whose
value is 5 or 10 times its own value.
For example, X (10)
can only precede L (50) or C (100).
So XL (40) is acceptable, but XD (490?) is not.
|
Roman Numeral |
Arabic Number |
|
XLVI |
46 |
|
XCIX |
99 |
|
MDCCCXIX |
1819 |
|
DCXLIX |
649 |
|
MCMLXXXIII |
1983 |
Fill in the Blanks
1. Strings are objects of the ________________________ class.
2. The operator used for concatenation of strings is ___________________________
3. If a reference to an object is no longer needed then the memory it used is reclaimed by the Java ______________________________
4. A reference variable set to ___________________ means that it does not refer to an object.
5. A String is a _______________ data type and a char is a __________________ data type
6. The ________________ operator will check the address of where the object is being stored.
Write Code
1. Given 2 strings stringOne and stringTwo that have been initialized with 1 sentence
each.
a. Write code that will determine if stringOne comes before stringTwo alphabetically.
If so, then an appropriate message will be printed. Alternatively, if they are equal
or if stringOne comes after stringTwo, then an appropriate message will be
printed.
b. Write code that will print the substring of stringTwo beginning at index 1 and ending
at index 3.
Multiple Choice
Circle the correct answer
1.
Which of the following statements is not true?
A.
Every object has an equals method.
B. If a
programmer does not write an equals method for a new class, the default
method will return true if and only
if all fields of the two instances of the class contain the same
values.
C. The equals
method for Strings returns true if and only if the two Strihngs contain the
same sequence of characters.
D. The equals
method for arrays returns true if and only if the two arrays point to the
same chunk of memory.
E. The equals
method for Strings will return false if one String is shorter than the
other.
2. Assume that the type of variable strList is ArrayList<String>. Consider the following two code segments, both of which are intended to add an exclamation point to the end of
each string in the list.
|
Segment
1
for (int k = 0, k<strList.size(); k++)
{
Str tmp = strList.remove(k);
strList.add(k, tmp + "!");
} |
Segment 2
for (int k = 0; k<strList.size(); k++)
{
String tmp = strList.get(k);
strList.set(k, tmp + "!");
} |
Which of the
following statements about these two code segments is true?
A.
Both will work as intended, and they will be equally efficient.
B.
Both will work as intended; Segment 1 will be more efficient
than Segment 2.
C. Both will work as intended; Segment 2 will be more efficient than segment 1.
D.
Only Segment 1 will work as intended.
E.
Only Segment 2 will work as intended.
3.
Assume the following declarations have been made.
private String
s;
private int n;
public void
changer(String x, int y)
{
x = x +
"peace";
y = y*2;
}
Assume s has the
value "world" and n is 6. What are the values of s and n after the call
changer(s, n) ?
|
|
s |
n
|
|
a |
world |
6 |
|
b |
worldpeace |
6 |
|
c |
world |
12 |
|
d |
worldpeace |
12 |
|
e |
peace |
12 |