1、Java语言程序设计编程题programming exercises答案第7章7.1 import java.util.Scanner;public class Exercise01 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(Enter a 4-by-4 matrix row by row:); int matrix = new int44; for (int i = 0; i 4; i+) for (int j = 0; j 4; j+) ma
2、trixij = input.nextInt(); System.out.println(Sum of the matrix is + sumMatrix(matrix); public static int sumMatrix(int m) int sum = 0; for (int i = 0; i m.length; i+) for (int j = 0; j mi.length; j+) sum += mij; return sum; 7.2 import java.util.Scanner;public class Exercise02 public static void main
3、(String args) Scanner input = new Scanner(System.in); System.out.println(Enter a 4-by-4 matrix row by row:); int matrix = new int44; for (int i = 0; i matrix.length; i +) for (int j = 0; j matrixi.length; j+) matrixij = input.nextInt(); System.out.println(Sum of the elements in the major diagonal is
4、 + sumMajorDiagonal(matrix); public static int sumMajorDiagonal(int m) int sum = 0; for (int i = 0, j = 0; i m.length & j m0.length; i+, j+) sum += mij; return sum; 7.3 public class Exercise03 public static void main(String args) /Students answers to the questions char answers = A, B, A, C, C, D, E,
5、 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 ; /Key to the questions char keys = D, B, D, C, C, D, A, E, A, D; int countOfCorr
6、ect = new intanswers.length2; /Grand all answers for (int i = 0; i answers.length; i+) /Grade one student int correctCount = 0; for (int j = 0; j answersi.length; j+) if (answersij = keysj) correctCount+; countOfCorrecti1 = correctCount; countOfCorrecti0 = i; for (int i = 0; i countOfCorrect.length;
7、 i+) int currentStudent = countOfCorrecti0; int min = countOfCorrecti1; int minIndex = i; for (int j = i + 1; j countOfCorrect.length; j+) if (countOfCorrectj1 min) currentStudent = countOfCorrectj0; min = countOfCorrectj1; minIndex = j; if (minIndex != i) countOfCorrectminIndex0 = countOfCorrecti0;
8、 countOfCorrectminIndex1 = countOfCorrecti1; countOfCorrecti0 = currentStudent; countOfCorrecti1 = min; for (int j = 0; j countOfCorrect.length; j+) System.out.println(Student + countOfCorrectj0 + s correct count is + countOfCorrectj1); 7.4 public class Exercise04 public static void main(String args
9、) int workHours = 2, 4, 3, 4, 5, 8, 8, 7, 3, 4, 3, 3, 4, 4, 9, 3, 4, 7, 3, 4, 1, 3, 5, 3, 4, 6, 3, 8, 3, 4, 4, 6, 3, 4, 4, 3, 7, 4, 8, 3, 8, 4, 6, 3, 5, 9, 2, 7, 9 ; int totalWorkHours = new intworkHours.length2; for (int i = 0; i workHours.length; i+) int sum = 0; for (int j = 0; j workHoursi.lengt
10、h; j+) sum += workHoursij; totalWorkHoursi0 = i; totalWorkHoursi1 = sum; int sortTotalWorkHours = sortArray(totalWorkHours); for (int j = 0; j sortTotalWorkHours.length; j+) System.out.println(Employee + sortTotalWorkHoursj0 + s total work hours is + sortTotalWorkHoursj1); public static int sortArra
11、y(int array) for (int i = 0; i array.length; i+) int currentEmployee = arrayi0; int currentMaxWorkHours = arrayi1; int currentMaxIndex = i; for (int j = i + 1; j currentMaxWorkHours) currentEmployee = arrayj0; currentMaxWorkHours = arrayj1; currentMaxIndex = j; if (currentMaxIndex != i) arraycurrent
12、MaxIndex0 = arrayi0; arraycurrentMaxIndex1 = arrayi1; arrayi0 = currentEmployee; arrayi1 = currentMaxWorkHours; return array; 7.5 import java.util.Scanner;public class Exercise05 public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(Enter a 4-by-4 matrix1:);
13、 int matrix1 = new int44; int matrix2 = new int44; for (int i = 0; i matrix1.length; i+) for (int j = 0; j matrix1i.length; j+) matrix1ij = input.nextInt(); System.out.println(Enter a 4-by-4 matrix2:); for (int i = 0; i matrix2.length; i+) for (int j = 0; j matrix2i.length; j+) matrix2ij = input.nex
14、tInt(); int sumOfMatrix = addMatrix(matrix1, matrix2); printMatrix(matrix1, matrix2, sumOfMatrix); public static int addMatrix(int a, int b) int sum = new inta.lengtha0.length; for (int i = 0; i a.length; i+) for (int j = 0; j ai.length; j+) sumij = aij + bij; return sum; public static void printMat
15、rix(int matrix1, int matrix2, int sumOfMatrix) for (int i = 0; i matrix1.length; i+) for (int j = 0; j matrix1i.length; j+) System.out.print(matrix1ij 10 ? : ) + matrix1ij + ); if (i != Math.ceil(matrix1.length / 2) System.out.print( ); else System.out.print(+ ); for (int j = 0; j matrix2i.length; j
16、+) System.out.print(matrix2ij 10 ? : ) + matrix2ij + ); if (i != Math.ceil(matrix1.length / 2) System.out.print( ); else System.out.print(= ); for (int j = 0; j matrix1i.length; j+) System.out.print(sumOfMatrixij 10 ? : ) + sumOfMatrixij + ); System.out.println(); 7.6 import java.util.Scanner;public
17、 class Exercise06 public static void main(String args) Scanner input = new Scanner(System.in); double matrix1 = new double33; double matrix2 = new double33; System.out.println(Enter a 3-by-3 matrix1:); for (int i = 0; i matrix1.length; i+) for (int j = 0; j matrix1i.length; j+) matrix1ij = input.nex
18、tDouble(); System.out.println(Enter a 3-by-3 matrix2:); for (int i = 0; i matrix2.length; i+) for (int j = 0; j matrix2i.length; j+) matrix2ij = input.nextDouble(); double multiplyMatrix = multiplyMatrix(matrix1, matrix2); for (int i = 0; i matrix1.length; i+) for (int j = 0; j matrix1i.length; j+)
19、System.out.print(matrix1ij + ); if (i != Math.ceil(matrix1.length / 2) System.out.print( ); else System.out.print(* ); for (int j = 0; j matrix2i.length; j+) System.out.print(matrix2ij + ); if (i != Math.ceil(matrix1.length / 2) System.out.print( ); else System.out.print(= ); for (int j = 0; j matri
20、x1i.length; j+) System.out.print(int)(multiplyMatrixij * 10) / 10.0 + ); System.out.println(); public static double multiplyMatrix(double a, double b) double multiply = new doublea.lengtha0.length; for (int i = 0; i a.length; i+) for (int j = 0; j multiplyi.length; j+) for (int m = 0; m a.length; m+
21、) multiplyij += aim * bmj; return multiply; 7.7 public class Exercise07 public static void main(String args) double points = -1, 0, 3, -1, -1, -1, 4, 1, 1, 2, 0.5, 9, 3.5, 2, -1, 3, 1.5, 3, -1.5, 4, 2, 5.5, 4, -0.5; /p1 and p2 are the indices in the points array int p1 = 0, p2 = 1; /Initial two poin
22、ts double shortestDistance = distance(pointsp10, pointsp11, pointsp12, pointsp20, pointsp21, pointsp22); /Initialize shortestDistance /Compute distance for every two points for (int i = 0; i points.length; i+) for (int j = i + 1; j points.length; j+) double distance = distance(pointsi0, pointsi1, po
23、intsi2, pointsj0, pointsj1, pointsj2); /Find distance if (distance shortestDistance) p1 = i; /Update p1 p2 = j; /Update p2 shortestDistance = distance; /Update shortestDistance /Display result System.out.println(The closest two points are + ( + pointsp10 + , + pointsp11 + pointsp12 + ) and ( + point
24、sp20 + , + pointsp21 + pointsp22 + ); public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) return Math.sqrt(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1); 7.8 import java.util.Scanner;public class Exercise08 public static void main(St
25、ring args) double points = -1, 3, -1, -1, 1, 1, 0, 0.5, 2, 0.5, 2, -1, 3, 3, 4, 2, 4, -0.5; /p1 and p2 are the indices in the points array int p1 = 0, p2 = 1; /Initial two points double shortestDistance = distance(pointsp10, pointsp11, pointsp20, pointsp21); /Initialize shortestDistance int indices = new intpoints.length2; int k = 0; ind
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1