// LargestSmallest4.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-12 File created. // 2006-02-12 Last modification. // Solution to the second tast of Exercise 6-1. import java.util.* ; class LargestSmallest4 { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n This program can find the largest" + "\n and the smallest of four integers" + "\n you enter from the keyboard. \n\n") ; System.out.print( " Please, enter the first integer: " ) ; int first_integer = keyboard.nextInt( ) ; System.out.print( " Please, enter the second integer: " ) ; int second_integer = keyboard.nextInt( ) ; System.out.print( " Please, enter the third integer: " ) ; int third_integer = keyboard.nextInt( ) ; System.out.print( " Please, enter the fourth integer: " ) ; int fourth_integer = keyboard.nextInt( ) ; int found_largest_integer, found_smallest_integer ; if ( first_integer > second_integer ) { found_largest_integer = first_integer ; found_smallest_integer = second_integer ; } else { found_largest_integer = second_integer ; found_smallest_integer = first_integer ; } if ( third_integer > found_largest_integer ) { found_largest_integer = third_integer ; } if ( fourth_integer > found_largest_integer ) { found_largest_integer = fourth_integer ; } if ( third_integer < found_smallest_integer ) { found_smallest_integer = third_integer ; } if ( fourth_integer < found_smallest_integer ) { found_smallest_integer = fourth_integer ; } System.out.print( "\n The largest integer is " + found_largest_integer + ".\n" ) ; System.out.print( "\n The smallest integer is " + found_smallest_integer + ".\n" ) ; } }