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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

《Java语言程序设计基础篇》第10版 梁勇 著第十章练习题答案.docx

1、Java语言程序设计基础篇第10版 梁勇 著第十章练习题答案Java语言程序设计(基础篇)(第10版 梁勇 著)第十章 练习题答案10.1public class Exercise10_01 public static void main(String args) Time time1 = new Time(); System.out.println(Hour: + time1.getHour() + Minute: + time1.getMinute() + Second: + time1.getSecond(); Time time2 = new Time(555550000); Syst

2、em.out.println(Hour: + time2.getHour() + Minute: + time2.getMinute() + Second: + time2.getSecond(); class Time private int hour; private int minute; private int second; public Time() this(System.currentTimeMillis(); public Time(long elapsedTime) setTime(elapsedTime); public Time(int hour, int minute

3、, int second) this.hour = hour; this.minute = minute; this.second = second; public int getHour() return hour; public int getMinute() return minute; public int getSecond() return second; public void setTime(long elapsedTime) / Obtain the total seconds since the midnight, Jan 1, 1970 long totalSeconds

4、 = elapsedTime / 1000; / Compute the current second in the minute in the hour second = (int)(totalSeconds % 60); / Obtain the total minutes long totalMinutes = totalSeconds / 60; / Compute the current minute in the hour minute = (int)(totalMinutes % 60); / Obtain the total hours int totalHours = (in

5、t)(totalMinutes / 60); / Compute the current hour hour = (int)(totalHours % 24); 10.2public class Exercise10_02 public static void main(String args) BMI bmi1 = new BMI(John Doe, 18, 145, 5, 10); System.out.println(The BMI for + bmi1.getName() + is + bmi1.getBMI() + + bmi1.getStatus(); BMI bmi2 = new

6、 BMI(Peter King, 215, 5, 10); System.out.println(The BMI for + bmi2.getName() + is + bmi2.getBMI() + + bmi2.getStatus(); static class BMI private String name; private int age; private double weight; / in pounds private double height; / in inches public final double KILOGRAMS_PER_POUND = 0.45359237;

7、public final double METERS_PER_INCH = 0.0254; /* Construct a BMI with the specified name, age, weight, * feet and inches */ public BMI(String name, int age, double weight, double feet, double inches) this.name = name; this.age = age; this.weight = weight; this.height = feet * 12 + inches; public BMI

8、(String name, int age, double weight, double height) this.name = name; this.age = age; this.weight = weight; this.height = height; public BMI(String name, double weight, double height) this(name, 20, weight, height); public double getBMI() double bmi = weight * KILOGRAMS_PER_POUND / (height * METERS

9、_PER_INCH) * (height * METERS_PER_INCH); return Math.round(bmi * 100) / 100.0; public String getStatus() double bmi = getBMI(); if (bmi 16) return seriously underweight; else if (bmi 18) return underweight; else if (bmi 24) return normal weight; else if (bmi 29) return over weight; else if (bmi 35)

10、return seriously over weight; else return gravely over weight; public String getName() return name; public int getAge() return age; public double getWeight() return weight; public double getHeight() return height; 10.3 public class Exercise10_03 public static void main(String args) MyInteger n1 = ne

11、w MyInteger(5); System.out.println(n1 is even? + n1.isEven(); System.out.println(n1 is prime? + n1.isPrime(); System.out.println(15 is prime? + MyInteger.isPrime(15); char chars = 3, 5, 3, 9; System.out.println(MyInteger.parseInt(chars); String s = 3539; System.out.println(MyInteger.parseInt(s); MyI

12、nteger n2 = new MyInteger(24); System.out.println(n2 is odd? + n2.isOdd(); System.out.println(45 is odd? + MyInteger.isOdd(45); System.out.println(n1 is equal to n2? + n1.equals(n2); System.out.println(n1 is equal to 5? + n1.equals(5); class MyInteger private int value; public int getValue() return

13、value; public MyInteger(int value) this.value = value; public boolean isPrime() return isPrime(value); public static boolean isPrime(int num) if (num = 1) | (num = 2) return true; for (int i = 2; i = num / 2; i+) if (num % i = 0) return false; return true; public static boolean isPrime(MyInteger o)

14、return isPrime(o.getValue(); public boolean isEven() return isEven(value); public boolean isOdd() return isOdd(value); public static boolean isEven(int n) return n % 2 = 0; public static boolean isOdd(int n) return n % 2 != 0; public static boolean isEven(MyInteger n) return isEven(n.getValue(); pub

15、lic boolean equals(int anotherNum) return value = anotherNum; public boolean equals(MyInteger o) return value = o.getValue(); public static int parseInt(char numbers) / numbers consists of digit characters. / For example, if numbers is 1, 2, 5, the return value / should be 125. Please note that / nu

16、mbers0 is 1 / numbers1 is 2 / numbers2 is 5 int result = 0; for (int i = 0; i numbers.length; i+) result = result * 10 + (numbersi - 0); return result; / You may mention this when you covered Ch8 public static int parseInt(String s) / s consists of digit characters. / For example, if s is 125, the r

17、eturn value / should be 125. int result = 0; for (int i = 0; i s.length(); i+) result = result * 10 + (s.charAt(i) - 0); return result; 10.4 public class Exercise10_04 public static void main(String args) MyPoint p1 = new MyPoint(); MyPoint p2 = new MyPoint(10, 30.5); System.out.println(p1.distance(

18、p2); System.out.println(MyPoint.distance(p1, p2); class MyPoint private double x; private double y; public MyPoint() public MyPoint(double x, double y) this.x = x; this.y = y; public double distance(MyPoint secondPoint) return distance(this, secondPoint); public static double distance(MyPoint p1, My

19、Point p2) return Math.sqrt(p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); public double getX() return x; public double getY() return y; 10.5public class Exercise10_05 public static void main(String args) StackOfIntegers stack = new StackOfIntegers(2); java.util.Scanner input = new jav

20、a.util.Scanner(System.in); / Prompt the user to enter an integer System.out.print(Enter an integer: ); / Convert string to int int number = input.nextInt(); System.out.println(The factors for + number + is); / Find and store all the smallest factors of the integer int factor = 2; while (factor = num

21、ber) if (number % factor = 0) number = number / factor; stack.push(factor); else factor+; / Display factors while (!stack.empty() System.out.print(stack.pop() + ); 10.6public class Exercise10_06 public static void main(String args) final int LIMIT = 120; int count = 0; StackOfIntegers stack = new St

22、ackOfIntegers(); / Repeatedly find prime numbers for (int number = 2; number LIMIT; number+) if (isPrime(number) stack.push(number); count+; / Increase the prime number count / Print the first 30 prime numbers in decreasing order System.out.println(The prime numbers less than 120 are n); final int N

23、UMBER_PER_LINE = 10; while (!stack.empty() System.out.print(stack.pop() + ); if (stack.getSize() % NUMBER_PER_LINE = 0) System.out.println(); / advance to the new line public static boolean isPrime(int number) / Assume the number is prime boolean isPrime = true; / Exercise03_21 if number is prime fo

24、r (int divisor = 2; divisor = number / 2; divisor+) /If true, the number is not prime if (number % divisor = 0) / Set isPrime to false, if the number is not prime isPrime = false; break; / Exit the for loop return isPrime; 10.7public class Exercise10_07 / Create ten accounts private Account accounts

25、 = new Account10; public Exercise10_07() for (int i = 0; i accounts.length; i+) accountsi = new Account(); accountsi.setId(i); accountsi.setBalance(100); continueATM:while (true) System.out.print(Enter an id: ); id = input.nextInt(); if (id 10) System.out.println(Please enter a correct id); continue

26、; while (true) int choice = getAChoice(); switch (choice) case 1: System.out.println(The balance is + accountsid.getBalance(); break; case 2: withdraw(); break; case 3: deposit(); break; case 4: / You can rewrite the code without using the continue. / To do so, introduce a boolean variable to contro

27、l one / customer session continue continueATM; private static java.util.Scanner input = new java.util.Scanner(System.in); int id; public static void main(String args) new Exercise10_07(); public void withdraw() System.out.print(Enter an amount to withdraw: ); int amount = input.nextInt(); if (amount = 0) accountsid.deposit(amount); else System.out.print(The amount is negative, ignored); public int getAChoice() int choice; while (true) System.out.println(nMain menu); System.out.println(1: check balance); System.out.println(2: withdr

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1