// This code sets things up for a general java program with input import java.util.Scanner; public class Arrays2Ex { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // done with setup, main code starts here int[] grades = new int[]{54, 99, 87, 68, 75, 98, 74, 82, 90, 49}; double average = 0; double total = 0; for (int i = 0; i < grades.length; i = i + 1){ total = total + grades[i]; } average = total / 10; System.out.println("Average: " + average); //---------------------------- String names[] = new String[]{"Abe", "Bob", "Cab", "Dee", "Eve"}; for (int i = 0; i < names.length; i=i+1) { System.out.println((i+1) + ". " + names[i]); } //---------------------------- int howmany = promptInt("How many temperatures"); double[] temps = new double[howmany]; for (int i = 0; i < temps.length; i = i + 1){ temps[i] = promptDouble("What is the #" +(i+1) + " temperature?"); } double highest = temps[0]; // start at first temperature int position = 0; // could also start loop at 1, since we already set to 0th for (int i = 0; i < temps.length; i = i + 1) { if (temps[i] > highest) { highest = temps[i]; position = i; } } System.out.println("highest was " + highest + " at position " + position); // ---------------------------- howmany = promptInt("How many temperatures"); temps = new double[howmany]; for (int i = 0; i < temps.length; i = i + 1){ temps[i] = promptDouble("What is the #"+ (i+1) + " temperature?"); } position = 0; // start at position 0 for (int i = 0; i < temps.length; i = i + 1) { if (temps[i] > temps[position]) { position = i; } } System.out.println("highest was " + temps[position] + " at position " + position); // main code ends (still need the below two lines!!) } /* Simple Utility methods that prompt the user and return input If you enter the wrong type of input, the program will end with an error! */ // utility for reading in integer input public static int promptInt(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); int response = scan.nextInt(); return response; } // utility for reading in double input public static double promptDouble(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); double response = scan.nextDouble(); return response; } // utility for reading in String input public static String promptString(String promptText) { System.out.print(promptText + " "); Scanner scan = new Scanner(System.in); String response = scan.nextLine(); return response; } // utility for reading in boolean input public static boolean promptBoolean(String promptText) { System.out.print(promptText + " (y/n) "); Scanner scan = new Scanner(System.in); String response = scan.nextLine(); return response.equalsIgnoreCase("y"); } }