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