【正文】
法。24. 答:StringBuffer對象和StringBuilder對象都是可變字符串。25. 答:C 26. 答:blue 27. 答:ABCDE28. 答:AB,B 29. 答:B 30. 答:C31. 參考代碼如下:public static int countLetters(String s){ int n = 0。i ()。 if( c = 39。 amp。 c = 39。 || c =39。 amp。 c = 39。) n = n + 1。}32. 參考代碼如下:public static String toBinary(int value){ String s = 。 s = r + s。}return s。for(int i = 0。 i++){ for(int j = i + 1。 j++){ if(ch[i] ch[j]){ char t = ch[j]。 ch[i] = t。return str。public class Encrypt{public static void main(String[]args){Scanner sc = new Scanner()。 String str = ()。 StringBuilder ss = new StringBuilder(str)。i ()。 if(c==39。 || c==39。){ c = (char)(c 25)。 } (i,c)。 }}35. 參考程序如下:import 。 (請輸入加密字符串:)。 (加密字符串是 :+str)。 for(int i = 0。 i++){ char c = (i)。a39。A39。 }else{ c = (char)(c 1)。 } (解密后的字符串是:+ss)。String[] words = ([, ])。 i 。 }}37. 參考程序如下:public class Test{ public static void main(String args[]){ for(int i=0。 i++) for(int j = i + 1。 j++) if(args[i].pareTo(args[j]) 0)){ String t = new String()。 args[j] = args[i]。} for(int i = 0。 i++) (args[i])。 (0)。 char op=39。 op1 = (args[0])。 op2 = (args[2])。+39。break。39。break。*39。break。/39。break。 }}39. 答:,如Object、Class、Math、Integer、String、StringBuilder等類。Scanner類定義了nextShort()、nextInt()、nextDouble()、nextLine()等方法。public class DateDemo{public static void main(String[]args){ Date d = new Date()。 }} 第5章習(xí)題解答1. 答:。3. 答:在子類中定義與超類中的名字、參數(shù)列表、返回值類型都相同的方法,這時(shí)子類的方法就叫做覆蓋。4. 答:(1)在子類中訪問超類中被隱藏的成員變量。(3)在子類中調(diào)用超類的構(gòu)造方法。6. 答:final修飾符可以修飾類、方法和變量。7. 答:對象類型轉(zhuǎn)換分為自動(dòng)類型轉(zhuǎn)換和強(qiáng)制類型轉(zhuǎn)換。缺省訪問修飾符的類只能被同一個(gè)包中的類訪問,public修飾符的類可以被任何類訪問。10. 答:抽象類中可以定義非抽象方法,接口中不可以。注意,接口中定義的方法修飾符都是public。內(nèi)部類被編譯成單獨(dú)的類文件,名稱為:外部類名$。15. 輸出結(jié)果: m = 1000 m = 48016. 答:A子類的構(gòu)造方法中沒有調(diào)用超類的構(gòu)造方法,故自動(dòng)調(diào)用超類的默認(rèn)構(gòu)造方法,而超類中又沒有定義默認(rèn)的構(gòu)造方法,找不到Super(),產(chǎn)生編譯錯(cuò)誤。19. 答:A, D20. 答: I am parent I am child I am child21. 程序輸出: Inside BB39。s metoo().22. 答:D 23. 答:A24. 輸出結(jié)果為:He is a Manager.He is a Secretary.He is a Programmer.25. 答:(1)抽象類不能實(shí)例化。(2)若方法定義為抽象方法,類應(yīng)該定義為抽象類。26. 答:B 27. 答:C,E28. 參考程序如下:public class Student extends Person{ String sno。 public void setSno(String sno){ = sno。 } public String getSno(){ return sno。 } public static void main(String[] args){ Student stud = new Student()。 (20)。 (Information Science)。 (())。 (())。 public void start(){ (The auto is started.)。 (The auto is speed up to +speed+kilo/h.)。 (The auto is stoped.)。 public void gotOn(int n){ passenger = passenger+n。 } public void gotOff(int n){ passenger = passengern。 }}public class BusTest{ public static void main(String []args){ Bus bus = new Bus()。 (60)。 (10)。 }}30. 參考程序如下:public abstract class Shape{ private String name。} public void setName(String name){ = name。 } public abstract double perimeter()。}public class Triangle extends Shape{double a,b,c。 = 0。}public Triangle(double a, double b, double c){ = a。 = c。 return (s * (sa) * (sb) * (sc))。}public static void main(String[] args){ Triangle ta = new Triangle(3, 4, 5)。}}31. 參考程序如下:public class Cuboid extends Rectangle{ private double height。 = height。 } public void setHeight(double height){ = height。 } public double volume(){ return area() * height。 (10)。 (2)。 }}32. 參考程序如下:public abstract class CompareObject{public abstract int pareTo(Object obj)。 private int y。 = y。 }public void setY(int y){ = y。}public int getY(){return y。double dist1 = (x*x + y*y)。 return (int)(dist1dist2)。 Position pos2 = new Position(3,4)。 }}33. 參考程序如下:import 。 private int month。 public MyDate() { year = 1970。 day = 1。 = month。 } public int getYear(){ return year。 } public int getDay(){ return day。 if(== amp。 == amp。 == ) return true。 } public int pareTo(MyDate md){ //這里使用Date類計(jì)算兩個(gè)日期差的天數(shù) Date d = new Date(year,month,day)。 long days =(()())/1000/60/60/24。 } public static void main(String []args){ MyDate md1 = new MyDate(2007,12,20)。 ((md1))。(3)① 3 ② 5 ③ 無 ④ 4 ⑤ 無 ⑥ 535. 答:設(shè)汽車Auto定義為抽象類,玩具Toy定義為接口,玩具汽車ToyAuto定義為具體類。代碼如下:// 汽車類的定義public abstract class Auto{}// 玩具接口的定義public interface Toy{}// 玩具汽車的定義public class ToyAuto extends Auto implements Toy{}// 飛機(jī)類的定義public abstract class Plane{}// 玩具飛機(jī)的定義public class ToyPlane extends Plane implements Toy{}// 阿帕奇直升機(jī)的定義public class ApacheHelicopter extends Plane{}36. 參考程序:public enum TrafficLight{GREEN, RED, YELLOW。 for(TrafficLight light:tl){ (light)。}TrafficLight red = 。 break。 break。 break。public interface Author{ String name()。}38. 參考程序如下:public interface Flyable{ public abstract void takeoff()。 public abstract void fly()。} public void land(){ (Plane land.)。}} public abstract class Animal{ public abstract void eat()。} public void land(){ (Bird land.)。}public void eat(){ (Bird eating.)。}public void layEggs(){ (Bird lying eggs.)。} public void land(){ (Superman land.)。}public void eat(){ (Superman eating.)。}public void stopBullet(){ (Superman stop bullet.)。 ()。 Flyable bird = new Bird()。 ()。(2)若將Instrument定義成抽象類,則子類需要實(shí)現(xiàn)Instrument類的方法。實(shí)現(xiàn)代碼如下:public interface I