Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Today you are commissioned to write a program that will accept a single word from a user, which you store in a String
variable. With the use of loops and nested loops you are to produce a specific pattern with the characters of the word as
illustrated in the figures below.
So after initializing your variables, you are to retrieve a single word String input from the user and then find the
appropriate combination of for loops to display each character exactly as displayed in the two samples below. Your code
must work no matter what word the user enters.
Based on the previous specifications your program should look and behave exactly as displayed in the cases below:
REMEMBER in the output: ◦ is a space, is a new line and is a tab. Text in green is user
input.
Enter◦a◦string:◦COMP248 Enter◦a◦string:◦test
8 t
8 4 t s
8 4 2 t s e
8 4 2 P t s e t
8 4 2 P M t s e
8 4 2 P M O t s
8 4 2 P M O C t
8 4 2 P M O
8 4 2 P M
8 4 2 P
8 4 2
8 4
8
Note:
1. String methods you may find useful are length(), charAt() and/or substring().
2. When displaying the triangle, the space between each character is exactly equivalent to 1 tab in java; in other
words, each character is followed by one tab including the last character on each line.
3. You may need more than one nested loop to solve the problem.
4. You are to expect a perfect user who will always enter a valid one-word string; that is, do not verify the validity of
the user input.
5. The use of libraries other than java.util.Scanner is prohibited.
6. Final thought, remember that your solution is case-sensitive and space-sensitive.
Reminder:
When submitting your solution to the lab system, make sure there is no package statement at
the top of your .java file as this will result in a grade of 0 (restriction of this system).