- Activity 1
-
Download the file
ReverseLines.java.
It is a program that reads up to six lines entered by the user
and prints them back in reversed order.
Test the program by entering three lines
(followed by a blank line).
Enter up to 6 lines below.
Enter a blank line to end input.
One
Two
Three
In reverse order they are:
Three
Two
One
Test it again by trying to add seven lines.
(NOTE: it will not let you enter the seventh line;
it will immediately jump to printing the lines in reverse order
after the sixth line has been entered.)
Enter up to 6 lines below.
Enter a blank line to end input.
One
Two
Three
Four
Five
Six
In reverse order they are:
Six
Five
Four
Three
Two
One
Revise the code
so that it uses a List<String>
instead of a String[].
Don't change any of its other behaviour (yet).
It should still stop reading lines after six lines of input.
- Activity 2
-
Revise the program again
so that it allows the user to enter
an (essentially) unlimited number of lines.
Enter up to 6 lines below.
Enter a blank line to end input.
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
In reverse order they are:
Ten
Nine
Eight
Seven
Six
Five
Four
Three
Two
One
Oh!
Better get rid of the mentions of limits...
A program to reverse lines entered by the user.
Enter lines below.
Enter a blank line to end input.
- Activity 3
-
Add a loop to the program
that goes thru the list of lines the user entered
and removes any line that has the letter-string "sex" in it,
and also changes to UPPER CASE all lines that have "North" in them.
If a line happens to have BOTH "sex" and "North" in it,
it should be deleted.
In that case it does not have to be changed to UPPER CASE.
Note that "sex" or "North" can be anywhere in the line,
not just at the start.
There is a String method that lets you check
whether one String contains another.
Use it.
Have it print the revised list of lines
(in the original order)
to confirm that the changes have been made.
A program to reverse lines entered by the user.
Enter lines below.
Enter a blank line to end input.
Cornwall
Somerset
Middlesex
Essex
Wessex
Kent
York
Northumberland
Durham
In reverse order they are:
Durham
Northumberland
York
Kent
Wessex
Essex
Middlesex
Somerset
Cornwall
Cornwall
Somerset
Kent
York
NORTHUMBERLAND
Durham