ImageVerifierCode 换一换
格式:DOCX , 页数:51 ,大小:25.46KB ,
资源ID:25414439      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/25414439.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java语言程序设计编程题programming exercises答案第7章.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java语言程序设计编程题programming exercises答案第7章.docx

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