|
|
Strings Lab
Requirements:
- Create a Java Program called SimpleStringManip.
- This program should have a String variable named simple,
which is set to "Welcome to Java!"
- Print the length of the string.
- Print the character at location 6.
- Print the substring with the parameters 2 and 5.
The output should look similar to this:
Length: 16
Character at 6: e
Substring at positions 2, 5: lco
Hints:
- You can do this with one class or two. Two classes, one main
and one not main class, is the preferred method, but I've provided
examples for both options.
- I created a new project, called StringExercises
- When I created the project, I created a Main class called
StringRunner.
- I made another class, called StringExamples, with a
method called exercise. I put all of the logic in this method.
- The first line of this exercise method has the declaration and
initialization of the String variable
simple.
- The next three lines should be System.out.println()
statements that have the simple variable and the appropriate method.
- The public static void main method of the StringExcercises
class creates an object of type StringExamples, then it calls the exercise
method on that object.
Still stuck?
See the solution, using two classes.
See the solution, using one Main class.
|