ANSWERS FOR SEQUENTIAL PROCESSING PROBLEMS // Answer for Problem 1. public static void main(String[] args) { // Initialize min to a value greater than // any possible input value x. int x, currentMin; String s; s = JOptionPane.showInputDialog( "Enter an int. Terminate list with Cancel."); min = Integer.parseInt(s); s = JOptionPane.showInputDialog( "Enter an int. Terminate list with Cancel."); while (s != null) { x = Integer.parseInt(s); if (x < curentMin) min = x; s = JOptionPane.showInputDialog( "Enter an int. Terminate list with Cancel."); } System.out.println("The minimum is " + currentMin + "."); } // Note: instead of using the two lines if (x < currentMin) currentMin = x; you can use currentMin = Math.min(x, currentMin); to update the current minimum. // Answer for Problem 2. public static void main(String[] args) { int sum = 0, count = 0; String s; double ave; s = JOptionPane.showInputDialog( "Enter a string."); while (s != null) { sum += s.length(); count++; s = JOptionPane.showInputDialog( "Enter a string."); } ave = (double) sum / count; System.out.println("Average string length is " + ave + "."); } // Answer for Problem 3. public static void main(String[] args) { String gender, s; int salary, sumForMen, sumForWomen, countForMen, countForWomen; gender = JOptionPane.showInputDialog("Enter gender."); while(s != null) { s = JOptionPane.showInputDialog("Enter gender."); salary = Integer.parseInt(s); if(gender.equals("F") { sumForWomen += salary; countForWomen++; } else if(gender.equals("M") { sumForMen += salary; countForMen++; } s = JOptionPane.showInputDialog("Enter gender."); } System.out.println("Ave. salary for men is " + (double) sumForMen / countForMen); System.out.println("Ave. salary for women is " + (double) sumForWomen / countForWomen); } // Answer for Problem 4. public static void main(String[] args) { String s; int x, max; s = JOptionPane.showInputDialog( "Enter an int."); while (s != null) { x = Integer.parseInt(s); if (x % 2 == 0 && x > max) max = x; s = JOptionPane.showInputDialog( "Enter an int."); } System.out.println("Maximum even int is " + max + "."); }