Java程序設(shè)計-精編教程(第2版)習(xí)題解答(共26頁)_第1頁
Java程序設(shè)計-精編教程(第2版)習(xí)題解答(共26頁)_第2頁
Java程序設(shè)計-精編教程(第2版)習(xí)題解答(共26頁)_第3頁
Java程序設(shè)計-精編教程(第2版)習(xí)題解答(共26頁)_第4頁
Java程序設(shè)計-精編教程(第2版)習(xí)題解答(共26頁)_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上習(xí)題解答習(xí)題一(第1章)1James Gosling2需3個步驟:1) 用文本編輯器編寫源文件。 2) 使用javac編譯源文件,得到字節(jié)碼文件。3) 使用解釋器運行程序。3set classpath=D:jdkjrelibrt.jar;.;4. B5. Java源文件的擴展名是.java,Java字節(jié)碼的擴展名是.class。6D。 習(xí)題二(第2章)1Oxab187lader18.12height165.65bottomOxab187ractangle109.87width25.18height2Teacher.javapublic class Teacher do

2、uble add(double a,double b) return a+b; double sub(double a,double b) return a-b; Student.javapublic class Student public void speak() System.out.println("老師好"); MainClass.javapublic class MainClass public static void main(String args) Teacher zhang=new Teacher(); System.out.println(zhang.

3、add(12,236); System.out.println(zhang.add(234,120); Student jiang=new Student(); jiang.speak(); 3 如果源文件中有多個類,但沒有public類,那么源文件的名字只要和某個類的名字相同,并且擴展名是.java就可以了,如果有一個類是public類,那么源文件的名字必須與這個類的名字完全相同,擴展名是.java。4行尾風(fēng)格。習(xí)題三(第3章)1用來標(biāo)識類名、變量名、方法名、類型名、數(shù)組名、文件名的有效字符序列稱為標(biāo)識符。標(biāo)識符由字母、下劃線、美元符號和數(shù)字組成,第一個字符不能是數(shù)字。true不是標(biāo)識符。2

4、關(guān)鍵字就是Java語言中已經(jīng)被賦予特定意義的一些單詞,不可以把關(guān)鍵字作為名字來用。不是關(guān)鍵字。class implements interface enum extends abstract。3boolean,char,byte,short,int,long,float,double。4屬于操作題,解答略。5屬于操作題,解答略。6. public class E public static void main (String args ) char cStart='A',cEnd='Z' for(char c=cStart;c<=cEnd;c+) Syst

5、em.out.print(" "+c); 7不可以。習(xí)題四(第4章)1110。不規(guī)范。2新親親斤!。3public class JudgeAward void giveMess(int number) if(number=9|number=131|number=12) System.out.println(number+"是三等獎"); else if(number=209|number=596|number=27) System.out.println(number+"是二等獎"); else if(number=875|numbe

6、r=316|number=59) System.out.println(number+"是一等獎"); else System.out.println("未中獎"); 4import java.util.Scanner;public class Computer public static void main(String args) Scanner reader=new Scanner(System.in); double amount = 0; /存放電量 double price = 0; /用戶需要交納的電費 System.out.print(&

7、quot;輸入電量:"); amount =reader.nextDouble(); if(amount <= 90 && amount>=1) price = amount*0.6;/計算price的值 else if(amount <= 150 && amount>=91) price = 90*0.6+(amount-90)*1.1;/計算price的值 else if(amount>150) price = 90*0.6+(150-90)*1.1+(amount-150)*1.7;/計算price的值 else S

8、ystem.out.println("輸入電量:"+amount+"不合理"); System.out.printf("電費:%5.2f",price); 5public class E public static void main (String args ) char cStart='A',cEnd='Z' for(char c=cStart;c<=cEnd;c+) System.out.printf("%2c",c); System.out.println(); for

9、(char c=cStart;c<=cEnd;c+) System.out.printf("%2c",(c+32); 6public class Xiti5 public static void main(String args) int sum=0,i,j; for(i=1;i<=1000;i+) for(j=1,sum=0;j<i;j+) if(i%j=0) sum=sum+j; if(sum=i) System.out.println("完數(shù):"+i); 7public class E public static void mai

10、n(String args) int n=1,i=1,jiecheng=1; long sum=0; while(true) jiecheng=1; for(i=1;i<=n;i+) jiecheng=jiecheng*i; sum=sum+jiecheng; if(sum>9876) break; n+; System.out.println("滿足條件的最大整數(shù):"+(n-1); 習(xí)題五(第5章)1用類創(chuàng)建對象時。2一個類中可以有多個方法具有相同的名字,但這些方法的參數(shù)必須不同,即或者是參數(shù)的個數(shù)不同,或者是參數(shù)的類型不同??梢?。3可以。不可以。4不可以。5

11、一個類通過使用new運算符可以創(chuàng)建多個不同的對象,不同的對象的實例變量將被分配不同的內(nèi)存空間。所有對象的類變量都分配給相同的一處內(nèi)存,對象共享類變量。6CD。7【代碼1】【代碼4】。8sum=-100。9. 27。10100和20.0。習(xí)題六(第6章)1如果子類和父類在同一個包中,那么,子類自然地繼承了其父類中不是private的成員變量作為自己的成員變量,并且也自然地繼承了父類中不是private的方法作為自己的方法, 繼承的成員或方法的訪問權(quán)限保持不變。當(dāng)子類和父類不在同一個包中時,父類中的private和友好訪問權(quán)限的成員變量不會被子類繼承,也就是說,子類只繼承父類中的protected

12、和public訪問權(quán)限的成員變量作為子類的成員變量;同樣,子類只繼承父類中的protected和public訪問權(quán)限的方法作為子類的方法。如果所聲明的成員的變量的名字和從父類繼承來的成員變量的名字相同(聲明的類型可以不同),在這種情況下,子類就會隱藏掉所繼承的成員變量。2不可以。3abstract類。4A類是B類的父類,當(dāng)用子類創(chuàng)建一個對象b,并把這個對象b的引用放到父類的對象a中時,稱a是b的上轉(zhuǎn)型對象。5AD。6 15.08.0。7 98.012。習(xí)題七(第7章)1不能。2不能。3可以把實現(xiàn)某一接口的類創(chuàng)建的對象的引用賦給該接口聲明的接口變量中。那么該接口變量就可以調(diào)用被類實現(xiàn)的接口中的方

13、法。4 15.08。5 1815。習(xí)題八(第8章)1有效。2可以。3不可以。4大家好,祝工作順利!習(xí)題九(第9章)1ABD。2Love:Game。3 15abc我們。413579。59javaHello。6public class E public static void main (String args ) String s1,s2,t1="ABCDabcd" s1=t1.toUpperCase(); s2=t1.toLowerCase(); System.out.println(s1); System.out.println(s2); String s3=s1.con

14、cat(s2); System.out.println(s3); 7. public class E public static void main (String args ) String s="ABCDabcd" char cStart=s.charAt(0); char cEnd = s.charAt(s.length()-1); System.out.println(cStart); System.out.println(cEnd); 8.import java.util.*;public class E public static void main(Strin

15、g args) Scanner read=new Scanner(System.in); CalendarBean cb=new CalendarBean(); int year=2000,month=1; System.out.println("輸入年:"); year=read.nextInt(); System.out.println("輸入月:"); month=read.nextInt(); cb.setYear(year); cb.setMonth(month); String a= cb.getCalendar();/返回號碼的一維數(shù)組 c

16、har str="日一二三四五六".toCharArray(); for(char c:str) System.out.printf("%3c",c); for(int i=0;i<a.length;i+) /輸出數(shù)組a if(i%7=0) System.out.println(""); /換行 System.out.printf("%4s",ai); class CalendarBean String day; int year=0,month=0; public void setYear(int year

17、) this.year=year; public void setMonth(int month) this.month=month; public String getCalendar() String a=new String42; Calendar rili=Calendar.getInstance(); rili.set(year,month-1,1); int weekDay=rili.get(Calendar.DAY_OF_WEEK)-1; /計算出1號的星期 int day=0; if(month=1|month=3|month=5|month=7|month=8|month=1

18、0|month=12) day=31; if(month=4|month=6|month=9|month=11) day=30; if(month=2) if(year%4=0)&&(year%100!=0)|(year%400=0) day=29; else day=28; for(int i=0;i<weekDay;i+) ai=" " for(int i=weekDay,n=1;i<weekDay+day;i+) ai=String.valueOf(n) ; n+; for(int i=weekDay+day;i<a.length;i

19、+) ai=" " return a; 9. import java.util.*;public class E public static void main (String args ) Scanner read=new Scanner(System.in); int year1,month1,day1,year2,month2,day2; System.out.println("輸入第一個日期的年、月、日(用空格或回車分隔):"); year1=read.nextInt(); month1=read.nextInt(); day1=read.nex

20、tInt(); System.out.println("輸入第二個日期的年月日(用空格或回車分隔):"); year2=read.nextInt(); month2=read.nextInt(); day2=read.nextInt(); Calendar calendar=Calendar.getInstance(); calendar.set(year1,month1-1,day1); long timeYear1=calendar.getTimeInMillis(); calendar.set(year2,month2-1,day2); long timeYear2=

21、calendar.getTimeInMillis(); long 相隔天數(shù)=Math.abs(timeYear1-timeYear2)/(1000*60*60*24); System.out.println(""+year1+"年"+month1+"月"+day1+"日和"+ year2+"年"+month2+"月"+day2+"日相隔"+相隔天數(shù)+"天"); 10. import java.util.*;public class E

22、public static void main (String args ) double a=0,b=0,c=0; a=12; b=24; c=Math.asin(0.56); System.out.println(c); c=Math.cos(3.14); System.out.println(c); c=Math.exp(1); System.out.println(c); c=Math.log(8); System.out.println(c); 11public class E public static void main (String args ) String str = &

23、quot;ab123you你是誰?" String regex = "D+" str = str.replaceAll(regex,""); System.out.println(str); 12 import java.util.*;public class E public static void main(String args) String cost = "數(shù)學(xué)87分,物理76分,英語96分" Scanner scanner = new Scanner(cost); scanner.useDelimiter(&qu

24、ot;.+"); double sum=0; int count =0; while(scanner.hasNext() try double score = scanner.nextDouble(); count+; sum = sum+score; System.out.println(score); catch(InputMismatchException exp) String t = scanner.next(); System.out.println("總分:"+sum+"分"); System.out.println("

25、平均分:"+sum/count+"分"); 習(xí)題十(第10章)1使用FileInputStream。2FileInputStream按字節(jié)讀取文件,F(xiàn)ileReader按字符讀取文件。3不可以。4使用對象流寫入或讀入對象時,要保證對象是序列化的。5使用對象流很容易得獲取一個序列化對象的克隆,只需將該對象寫入到對象輸出流,那么用對象輸入流讀回的對象一定是原對象的一個克隆。6 import java.io.*;public class E public static void main(String args) File f=new File("E.java

26、"); try RandomAccessFile random=new RandomAccessFile(f,"rw"); random.seek(0); long m=random.length(); while(m>=0) m=m-1; random.seek(m); int c=random.readByte(); if(c<=255&&c>=0) System.out.print(char)c); else m=m-1; random.seek(m); byte cc=new byte2; random.readFully

27、(cc); System.out.print(new String(cc); catch(Exception exp) 7. import java.io.*;public class E public static void main(String args ) File file=new File("E.java"); File tempFile=new File("temp.txt"); try FileReader inOne=new FileReader(file); BufferedReader inTwo= new BufferedRead

28、er(inOne); FileWriter tofile=new FileWriter(tempFile); BufferedWriter out= new BufferedWriter(tofile); String s=null; int i=0; s=inTwo.readLine(); while(s!=null) i+; out.write(i+" "+s); out.newLine(); s=inTwo.readLine(); inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(

29、); catch(IOException e) 8. 屬于上機操作題,解答略。 9. import java.io.*;import java.util.*;public class E public static void main(String args) File file = new File("a.txt"); Scanner sc = null; double sum=0; int count = 0; try sc = new Scanner(file); sc.useDelimiter(".+"); while(sc.hasNext()

30、try double price = sc.nextDouble(); count+; sum = sum+price; System.out.println(price); catch(InputMismatchException exp) String t = sc.next(); System.out.println("平均價格:"+sum/count); catch(Exception exp) System.out.println(exp); 習(xí)題十一(第11章)1Frame容器的默認布局是BorderLayout布局。2不可以。3 import java.awt

31、.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class E public static void main(String args) Computer fr=new Computer(); class Computer extends JFrame implements DocumentListener JTextArea text1,text2; int count=1; double sum=0,aver=0; Computer() setLayout(new FlowL

32、ayout(); text1=new JTextArea(6,20); text2=new JTextArea(6,20); add(new JScrollPane(text1); add(new JScrollPane(text2); text2.setEditable(false); (text1.getDocument().addDocumentListener(this); setSize(300,320); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); public v

33、oid changedUpdate(DocumentEvent e) String s=text1.getText(); String a =s.split(".+"); sum=0; aver=0; for(int i=0;i<a.length;i+) try sum=sum+Double.parseDouble(ai); catch(Exception ee) aver=sum/count; text2.setText(null); text2.append("n和:"+sum); text2.append("n平均值:"+

34、aver); public void removeUpdate(DocumentEvent e) changedUpdate(e); public void insertUpdate(DocumentEvent e) changedUpdate(e); 4. import java.awt.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class E public static void main(String args) ComputerFrame fr=new Compute

35、rFrame(); class ComputerFrame extends JFrame implements ActionListener JTextField text1,text2,text3; JButton buttonAdd,buttonSub,buttonMul,buttonDiv; JLabel label; public ComputerFrame() setLayout(new FlowLayout(); text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10); label=n

36、ew JLabel(" ",JLabel.CENTER); label.setBackground(Color.green); add(text1); add(label); add(text2); add(text3); buttonAdd=new JButton("加"); buttonSub=new JButton("減"); buttonMul=new JButton("乘"); buttonDiv=new JButton("除"); add(buttonAdd); add(button

37、Sub); add(buttonMul); add(buttonDiv); buttonAdd.addActionListener(this); buttonSub.addActionListener(this); buttonMul.addActionListener(this); buttonDiv.addActionListener(this); setSize(300,320); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); public void actionPerfo

38、rmed(ActionEvent e) double n; if(e.getSource()=buttonAdd) double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1+n2; text3.setText(String.valueOf(n); label.setText("+"); catch(NumberFormatException ee) text3.setText("請輸入數(shù)字字符"); else i

39、f(e.getSource()=buttonSub) double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1-n2; text3.setText(String.valueOf(n); label.setText("-"); catch(NumberFormatException ee) text3.setText("請輸入數(shù)字字符"); else if(e.getSource()=buttonMul) doub

40、le n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1*n2; text3.setText(String.valueOf(n); label.setText("*"); catch(NumberFormatException ee) text3.setText("請輸入數(shù)字字符"); else if(e.getSource()=buttonDiv) double n1,n2; try n1=Double.parseDo

41、uble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1/n2; text3.setText(String.valueOf(n); label.setText("/"); catch(NumberFormatException ee) text3.setText("請輸入數(shù)字字符"); validate(); 5. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class E public

42、 static void main(String args) Window win = new Window(); win.setTitle("使用MVC結(jié)構(gòu)"); win.setBounds(100,100,420,260); class Window extends JFrame implements ActionListener Lader lader; /模型 JTextField textAbove,textBottom,textHeight; /視圖 JTextArea showArea; /視圖 JButton controlButton; /控制器 Wind

43、ow() init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); void init() lader = new Lader(); textAbove = new JTextField(5); textBottom = new JTextField(5); textHeight = new JTextField(5); showArea = new JTextArea(); controlButton=new JButton("計算面積"); JPanel pNorth=new JP

44、anel(); pNorth.add(new JLabel("上底:"); pNorth.add(textAbove); pNorth.add(new JLabel("下底:"); pNorth.add(textBottom); pNorth.add(new JLabel("高:"); pNorth.add(textHeight); pNorth.add(controlButton); controlButton.addActionListener(this); add(pNorth,BorderLayout.NORTH); add(

45、new JScrollPane(showArea),BorderLayout.CENTER); public void actionPerformed(ActionEvent e) try double above = Double.parseDouble(textAbove.getText().trim(); double bottom = Double.parseDouble(textBottom.getText().trim(); double height = Double.parseDouble(textHeight.getText().trim(); lader.setAbove(

46、above) ; lader.setBottom(bottom); lader.setHeight(height); double area = lader.getArea(); showArea.append("面積:"+area+"n"); catch(Exception ex) showArea.append("n"+ex+"n"); class Lader double above,bottom,height; public double getArea() double area = (above+bottom)*height/2.0; return area; public void setAbove(double a) above = a; public void setBottom(double b) bottom = b; public void setHeight(double c) height = c; 習(xí)題十二(第12章)14種狀態(tài):新建、運行、中斷和死亡。2有4種原

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論