import java.util.Scanner; /** Solution to lab - lots of loops. */ public class LoopsLab { /** The main (and only) method. @param args not used */ public static void main(String[] args) { Scanner kb = new Scanner(System.in); String nextLine = null; Scanner readLine; int transaction = -1; char first, last; // for letters operation int rows, cols; // for rectangle operation char ch; // for rectangle operation int value; // for graph operation String time; int hours, mins, secs; final int sixty = 60; int where; double seconds; final int quit = 4; final int graph = 3; do { System.out.println("\n"); System.out.println("Please choose an operation: "); System.out.println("0) time"); System.out.println("1) letters"); System.out.println("2) rectangle"); System.out.println("3) graph"); System.out.println("4) quit"); System.out.print("choice -> "); transaction = kb.nextInt(); System.out.println(); switch (transaction) { case quit: System.out.println("BYE!"); break; case 0: System.out.print("enter time of day in HH:MM:SS format: "); time = kb.next(); where = time.indexOf(':'); hours = Integer.parseInt(time.substring(0, where)); time = time.substring(where + 1); where = time.indexOf(':'); mins = Integer.parseInt(time.substring(0, where)); time = time.substring(where + 1); secs = Integer.parseInt(time); seconds = (hours * sixty) + mins; seconds = (seconds * sixty) + secs; System.out.printf("%.0f seconds since midnight%n", seconds); break; case 1: // System.out.println("print letters transaction"); System.out.print("enter two letters: "); first = kb.next().toUpperCase().charAt(0); last = kb.next().toUpperCase().charAt(0); if (first > last) { // switch them char temp = first; first = last; last = temp; } for (char c = first; c <= last; c++) { System.out.print(c + " "); } System.out.println(); break; // end letters case case 2: // System.out.println("rectangle transaction"); System.out.print("enter height, width & display character: "); rows = kb.nextInt(); cols = kb.nextInt(); ch = kb.next().charAt(0); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { System.out.print(ch); } System.out.println(); } break; // end rectangle case case graph: // System.out.println("bar graph transaction"); System.out.print("enter the numbers to process on one line: "); kb.nextLine(); // skip end of line character after menu choice nextLine = kb.nextLine(); readLine = new Scanner(nextLine); while (readLine.hasNext()) { value = readLine.nextInt(); for (int i = 0; i < value; i++) { System.out.print("*"); } System.out.println(); } break; // end graph case default: System.out.println("ERROR: invalid transaction code"); } //end switch } while (transaction != quit); // end do loop } //end main }