1 package chapter7; 2 3 public class PassTwoDimensionalArray { 4 public static void main (String[] args) { 5 java.util.Scanner input = new java.util.Scanner(System.in); 6 7 int[][] m = new int[3][4]; 8 System.out.println("Enter " + m.length + " rows and " + 9 m[0].length + " columns: "); 10 for (int i = 0; i < m.length; i++) 11 for(int j = 0; j < m[i].length; j++) 12 m[i][j] = input.nextInt(); 13 14 System.out.println("\nSum of elements is " + sum(m)); 15 } 16 17 public static int sum (int[][] m) { 18 int total = 0; 19 for (int row = 0; row < m.length; row++) { 20 for (int column = 0; column < m[row].length; column++) { 21 total += m[row][column]; 22 } 23 } 24 25 return total; 26 } 27 }
package chapter7; public class GradeExam { public static void main (String[] args) { char[][] answer = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}}; char[] key = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'}; for (int i = 0; i < answer.length; i++) { int correctCount = 0; for (int j = 0; j < answer[i].length; j++) { if (answer[i][j] == key[j]) correctCount++; } System.out.println("Student " + i + "'s correct count is " + correctCount); } } }
1 package chapter7; 2 3 public class FindNearestPoints { 4 public static void main (String[] args) { 5 java.util.Scanner input = new java.util.Scanner(System.in); 6 System.out.print("Enter the number of points: "); 7 int numberOfPoints = input.nextInt(); 8 9 double[][] points = new double [numberOfPoints][2]; 10 System.out.print("Enter " + numberOfPoints + " points: "); 11 for (int i = 0; i < points.length; i++) { 12 points[i][0] = input.nextDouble(); 13 points[i][0] = input.nextDouble(); 14 } 15 16 int p1 = 0, p2 = 1; 17 double shortestDistance = distance(points[p1][0], points[p1][1], 18 points[p2][0], points[p2][1]); 19 20 for (int i = 0; i < points.length; i++) { 21 for (int j = i + 1; j < points.length; j++) { 22 double distance = distance(points[i][0], points[i][1], 23 points[j][0], points[j][1]); 24 25 if (shortestDistance > distance) { 26 p1 = i; 27 p2 = j; 28 shortestDistance = distance; 29 } 30 } 31 } 32 33 System.out.println("The closest two points are " + "(" + 34 points[p1][0] + ", " + points[p1][1] + ") and (" + 35 points[p2][0] + ", " + points[p2][1] + ")"); 36 } 37 38 public static double distance ( 39 double x1, double y1, double x2, double y2) { 40 41 return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 42 } 43 } 44
1 package chapter7; 2 3 import java.util.Scanner; 4 5 public class CheckSudokuSolution { 6 public static void main(String[] args) { 7 int[][] grid = readASolution(); 8 9 System.out.println(isValid(grid) ? "valid solution" 10 : "Invalid solution"); 11 } 12 13 public static int[][] readASolution() { 14 Scanner input = new Scanner(System.in); 15 16 System.out.println("Enter a Sudoku puzzle solution: "); 17 int[][] grid = new int[9][9]; 18 for (int i = 0; i < 9; i++) 19 for (int j = 0; j < 9; j++) 20 grid[i][j] = input.nextInt(); 21 22 return grid; 23 } 24 25 public static boolean isValid(int[][] grid) { 26 for (int i = 0; i < 9; i++) 27 if (!is1To9(grid[i])) 28 return false; 29 30 for (int j = 0; j < 9; j++) { 31 int[] column = new int[9]; 32 for (int i = 0; i < 9; i++) { 33 column[i] = grid[i][j]; 34 } 35 36 if (!is1To9(column)) 37 return false; 38 } 39 40 for (int i = 0; i < 3; i++) { 41 for (int j = 0; j < 3; j++) { 42 int k = 0; 43 int[] list = new int[9]; 44 for (int row = i * 3; row < i * 3 + 3; row++) 45 for (int column = j * 3; column < j * 3 + 3; column++) 46 list[k++] = grid[row][column]; 47 48 if (!is1To9(list)) 49 return false; 50 } 51 } 52 return true; 53 } 54 55 public static boolean is1To9(int[] list) { 56 int[] temp = new int[list.length]; 57 System.arraycopy(list, 0, temp, 0, list.length); 58 59 java.util.Arrays.sort(temp); 60 61 for (int i = 0; i < 9; i++) 62 if (temp[i] != i + 1) 63 return false; 64 65 return true; 66 } 67 }
1 package chapter7; 2 3 import java.util.Scanner; 4 5 public class GuessBirthdayUsingArray { 6 public static void main(String[] args) { 7 int day = 0; 8 int answer; 9 10 int[][][] dates = { 11 { { 1, 3, 5, 7 }, { 9, 11, 13, 15 }, { 17, 19, 21, 23 }, 12 { 25, 27, 29, 31 } }, 13 { { 2, 3, 6, 7 }, { 10, 11, 14, 15 }, { 18, 19, 22, 23 }, 14 { 26, 27, 30, 31 } }, 15 { { 4, 5, 6, 7 }, { 12, 13, 14, 15 }, { 20, 21, 22, 23 }, 16 { 28, 29, 30, 31 } }, 17 { { 8, 9, 10, 11 }, { 12, 13, 14, 15 }, { 24, 25, 26, 27 }, 18 { 28, 29, 30, 31 } }, 19 { { 16, 17, 18, 19 }, { 20, 21, 22, 23 }, { 24, 25, 26, 27 }, 20 { 28, 29, 30, 31 } } }; 21 22 Scanner input = new Scanner(System.in); 23 24 for (int i = 0; i < 5; i++) { 25 System.out.println("Is your birthday in Set" + (i + 1) + "?"); 26 for (int j = 0; j < 4; j++) { 27 for (int k = 0; k < 4; k++) 28 System.out.printf("%4d", dates[i][j][k]); 29 System.out.println(); 30 } 31 32 System.out.print("\nEnter 0 for No and 1 for Yes: "); 33 answer = input.nextInt(); 34 35 if (answer == 1) 36 day += dates[i][0][0]; 37 } 38 39 System.out.println("Your birth day is " + day); 40 } 41 }
1 package chapter7; 2 3 import java.util.Scanner; 4 5 public class Weather { 6 public static void main (String[] args) { 7 final int NUMBER_OF_DAYS = 10; 8 final int NUMBER_OF_HOURS = 24; 9 double [][][] data 10 =new double [NUMBER_OF_DAYS][NUMBER_OF_HOURS][2]; 11 12 Scanner input = new Scanner(System.in); 13 for (int k = 0; k < NUMBER_OF_DAYS * NUMBER_OF_HOURS; k++) { 14 int day = input.nextInt(); 15 int hour = input.nextInt(); 16 double temperature = input.nextDouble(); 17 double humidity = input.nextDouble(); 18 data[day - 1][hour - 1][0] = temperature; 19 data[day - 1][hour - 1][1] = humidity; 20 } 21 22 for (int i = 0; i < NUMBER_OF_DAYS; i++) { 23 double dailyTemperatureTotal = 0, dailyHumidityTotal = 0; 24 for (int j = 0; j < NUMBER_OF_HOURS; j++) { 25 dailyTemperatureTotal += data[i][j][0]; 26 dailyHumidityTotal += data[i][j][1]; 27 } 28 System.out.println("Day " + i + "'s average temperature is " 29 + dailyTemperatureTotal / NUMBER_OF_HOURS); 30 System.out.println("Day " + i + "'s average humidity is " 31 + dailyHumidityTotal / NUMBER_OF_HOURS); 32 } 33 } 34 }