thinking injava习题答案2.docVIP

  • 11
  • 0
  • 约1.52万字
  • 约 12页
  • 2018-03-08 发布于河南
  • 举报
thinking injava习题答案2

这是第3章的答案: Chapter 3 Exercise 1 //: c03:E01_Precedence.java //+M java E01_Precedence /****************** Exercise 1 ****************** * There are two expressions in the section * labeled precedence early in this chapter. * Put these expressions into a program and * demonstrate that they produce different * results. ***********************************************/ public class E01_Precedence { static int a, x = 40, y = 60, z = 10; public static void main(String[] args) { a = x + y - 2/2 + z; System.out.println(a); a = x + (y - 2)/(2 + z); System.out.println(a); } } ///:~ Results are 109 44. The difference is because the default order of evaluation is changed by the use of the parentheses. Exercise 2 //: c03:E02_Ternary.java //+M java E02_Ternary /****************** Exercise 2 ****************** * Put the methods ternary() and alternative() * into a working program. ***********************************************/ public class E02_Ternary { static int ternary(int i) { return i 10 ? i * 100 : i * 10; } static int alternative(int i) { if (i 10) return i * 100; else return i * 10; } public static void main(String[] args) { System.out.println(ternary(9)); System.out.println(ternary(11)); System.out.println(alternative(9)); System.out.println(alternative(11)); } } ///:~ Exercise 3 //: c03:E03_IfElse3.java //+M java E03_IfElse3 /****************** Exercise 3 ****************** * From the sections labeled if-else and * return, modify the two test() methods so * that testval is tested to see if it is within * the range between (and including) the * arguments begin and end. (This is a change to * the exercise in the printed version of the * book, which was in error). ***********************************************/ public class E03_IfElse3 { static boolean test(int testval, int begin, int end) { boolean result = false; if(testval = begin testval = end)

文档评论(0)

1亿VIP精品文档

相关文档