Question: 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
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
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
Submit this/these files:
- ReverseLines.java
package l08; import java.util.Scanner; /** * This program reverses lines of text entered by the user. * */ public class ReverseLines { /** the maximum number of lines I will reverse */ public static final int MAX_LINES = 6; public static void main(String[] args) { // create variables Scanner kbd = new Scanner(System.in); String[] lines = new String[MAX_LINES]; String line; int numLines = 0; // introduce yourself System.out.print(" " + "A program to reverse up to " + MAX_LINES + " lines entered by the user. "); // read the lines System.out.println("Enter up to " + MAX_LINES + " lines below."); System.out.println("Enter a blank line to end input."); line = kbd.nextLine(); while (!line.isEmpty()) { lines[numLines] = line; if (++numLines >= MAX_LINES) { break; } line = kbd.nextLine(); } // print them out in reverse order System.out.println(" In reverse order they are: "); for (int i = numLines - 1; i >= 0; --i) { System.out.println(lines[i]); } // print some blank lines to make the ending less abrupt System.out.print(" "); // ADD CODE TO REVISE THE LIST (Activity 3) BELOW THIS LINE // don't forget to print it out! } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
