java語(yǔ)言程序設(shè)計(jì) 第二版 (叢書主編:譚浩強(qiáng) 編著:李尊朝 蘇軍 著) 中國(guó)鐵道出版社 課后答案
作業(yè)參考答案習(xí)題一4、如何建立和運(yùn)行Java程序?首先啟動(dòng)文本編輯器,如記事本、UltraEdit等,編輯程序代碼,并以.Java作為文件擴(kuò)展名保存程序源代碼;然后進(jìn)入dos環(huán)境利用javac編譯源程序,生成擴(kuò)展名為.class的字節(jié)碼文件;再利用命令java運(yùn)行字節(jié)碼文件,得到程序的運(yùn)行結(jié)果。在集成開發(fā)環(huán)境Jbuilder、Eclipse下,可以完成程序的編輯、編譯、調(diào)試及運(yùn)行等所有任務(wù)。5、public class LikeJava public static void main(String args) System.out.println(“I Like Java Very much!”); 習(xí)題二5、(1) 45 (2) false (3) 14 (4) 14 (5)6 (6) true (7) 12 9、public class Volume public static void main(String args) double r=0,v=0; r=double.parseDouble(args0); v=4*3.14159/3*r*r*r; System.out.println(“球體積為:”+v); 習(xí)題三8、public class Factorials public static void main(String args) int i, j;long s=0, k;i=1;do /外循環(huán)開始k = 1;j=1;do/內(nèi)循環(huán)開始k = k * j; /內(nèi)循環(huán)體j+;while(j<=i);/內(nèi)循環(huán)結(jié)束System.out.println(i + "!=" + k);s = s + k;i+;while(i<=20); /外循環(huán)結(jié)束System.out.println("Total sum=" + s);10、public class Num public static void main(Stringargs) int i,j,k,n; for (n=100;n<1000;n+) i=n/100; j=(n-i*100)/10; k=n%10; if (i*i*i+j*j*j+k*k*k=n) System.out.print(n+" "); 習(xí)題四5、import java.util.Scanner;class Factorlong fac(int m)if(m=0|m=1)return 1; else return m*fac(m-1); public static void main(String args) int i,n; long sum=0; String s="" Scanner input=new Scanner(System.in); System.out.print("Please input n: "); n=input.nextInt(); Factor f=new Factor(); for(i=1;i<=n;i+) System.out.println(f.fac(i); sum=sum+f.fac(i); s=s+i+"!+" System.out.println(s.substring(0,s.length()-1)+"="+sum); 習(xí)題五2、import java.io.*;public class YangHuiOk public static void main (String args) throws IOException int max,a,i,j; char x; System.out.print("請(qǐng)輸入楊輝三角要顯示的行數(shù): "); x=(char)System.in.read(); max = Integer.parseInt(String.valueOf(x); a=new intmax; for (i=0;i<max;i+) ai=new inti+1; a00=1; for (i=1;i<max;i+) ai0=1; aiai.length-1=1; for (j=1;j<ai.length-1;j+) aij=ai-1j-1+ai-1j; for(i=0;i<max;i+) /for(j=0;j<=max-i;j+) System.out.print(" "); for(j=0;j<=ai.length-1;j+) System.out.print(aij+" "); System.out.println(); 5、import java.util.Scanner;public class MatrixTurn public static void main (String args) int m,n;Scanner input=new Scanner(System.in); System.out.print("請(qǐng)輸入矩陣的行數(shù): "); m=input.nextInt(); System.out.print("請(qǐng)輸入矩陣的列數(shù): "); n=input.nextInt(); Matrix t=new Matrix(m,n); for(int i=1;i<=m;i+)/為矩陣各元素賦值 for (int j=1;j<=n;j+) t.setElement(Math.random(),i,j); System.out.println("轉(zhuǎn)置前的矩陣如下: "); for(int i=1;i<=m;i+) for (int j=1;j<=n;j+) /System.out.print(t.matrixij+" "); System.out.print(t.getElement(i,j)+" ");/訪問(wèn)矩陣元素方法1 System.out.println(); Matrix z;/聲明轉(zhuǎn)置矩陣 z=t.turn(t); System.out.println("轉(zhuǎn)置后的矩陣如下: "); for(int i=0;i<n;i+) for (int j=0;j<m;j+) System.out.print(z.matrixij+" ");/訪問(wèn)矩陣元素方法2,前提是matrix前無(wú)private System.out.println(); 習(xí)題六9、public class Vehicle String color, kind; int speed; Vehicle() color=”Red”; kind=”卡車”; speed=0;public void setColor(String color1) color=color1;public void setSpeed(String speed1) speed=speed1;public void setKind(String kind1) kind=kind1;public String getColor( ) return color;public String getKind( ) return kind;public int getSpeed( ) return speed;public static void main(String args) Vehicle che=new Vehicle ();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑車”);System.out.println(“有一輛”+che.getColor()+”的”+che.getKind()+”行駛在高速公路上”);System.out.println(“時(shí)速”+che.getSpeed()+”km/h”);習(xí)題七7、public class Vehicle String color, kind; int speed; Vehicle() color=” ”; kind=” ”; speed=0;public void setColor(String color1) color=color1;public void setSpeed(String speed1) speed=speed1;public void setKind(String kind1) kind=kind1;public String getColor( ) return color;public String getKind( ) return kind;public int getSpeed( ) return speed;public class Car extends Vehicle int passenger; public Car() super(); passenger=0; public void setPassenger(int passenger) this. passenger = passenger;public int getPassenger( ) return passenger;public static void main(String args) Car benz=new Car();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz: “);System.out.println(“Color “+benz.getColor();System.out.print(“Speed (km/h)“);System.out.println(benz.getSpeed();System.out.println(“Kind: “+benz.getKind();System.out.print(“Passenger: “);System.out.println(benz.getPassenger();習(xí)題九4、import java.io.*;public class UseException public static void main(String args) System.out.println("請(qǐng)輸入一個(gè)整數(shù)字符串"); try BufferedReader in=new BufferedReader(new InputStreamReader(System.in); int a=Integer.parseInt(in.readLine(); System.out.println("您輸入的整數(shù)是:"+a); catch(IOException e) System.out.println("IO錯(cuò)誤"); catch(NumberFormatException e1) System.out.println("您輸入的不是一個(gè)整數(shù)字符串"); 習(xí)題十7、import java.io.*;public class SaveName public static void main(String args) try BufferedReader br=new BufferedReader(new InputStreamReader(System.in); BufferedWriter bw=new BufferedWriter(new FileWriter("name.txt"); String s; while(true) System.out.println("請(qǐng)輸入姓名:"); s=br.readLine(); if(s.length()=0) break; bw.write(s); bw.newLine(); br.close(); bw.close(); catch(FileNotFoundException e) System.out.println(e.toString(); catch(IOException e1) System.out.println(e1.toString(); 8、import java.io.*;public class SaveGrade public static void main(String args) try BufferedReader br=new BufferedReader(new InputStreamReader(System.in); BufferedWriter bw=new BufferedWriter(new FileWriter("grade.txt"); String s,ss; while(true) System.out.println("請(qǐng)輸入姓名:"); s=br.readLine(); if(s.length()=0) break; bw.write(s); bw.newLine(); System.out.println("請(qǐng)輸入學(xué)號(hào):"); s=br.readLine(); bw.write(s); bw.newLine(); System.out.println("請(qǐng)輸入成績(jī):"); s=br.readLine(); bw.write(s); bw.newLine(); br.close(); bw.close(); int max=0,min=100,total=0,num=0; BufferedReader bf=new BufferedReader(new FileReader("grade.txt"); while(true) ss=bf.readLine(); if(ss=null) break; ss=bf.readLine(); ss=bf.readLine(); int grade=Integer.parseInt(ss); total+=grade; num+=1; if(grade>max)max=grade; if(grade<min)min=grade; System.out.println("學(xué)生成績(jī)中最高為:"+max+",最低為:"+min+",平均分為:"+total*1.0/num); bf.close(); catch(FileNotFoundException e) System.out.println(e.toString(); catch(IOException e1) System.out.println(e1.toString(); 習(xí)題十一6、import java.awt.*;import java.awt.event.*;public class ChangeColor extends Frameprivate Button red=new Button("紅");private Button green=new Button("綠");private Button blue=new Button("藍(lán)");private TextField text=new TextField();public ChangeColor()super("改變顏色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.RED););green.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.GREEN););blue.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)text.setBackground(Color.BLUE);); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); setSize(300,200); setVisible(true); public static void main (String args) ChangeColor color=new ChangeColor(); 習(xí)題十二5、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Goods extends JFrameprivate JComboBox list;private JTextArea info;private String names="請(qǐng)選擇你要查詢的商品","A商品","B商品","C商品","D商品","E商品","F商品"private String goods= "","","", "A商品","北京","¥300", "B商品","上海","¥400", "C商品","廣州","¥500", "D商品","長(zhǎng)沙","¥600", "E商品","武漢","¥700", "F商品","天津","¥800"public Goods()super("商品信息");Container pane=this.getContentPane();pane.setLayout(new BorderLayout();list=new JComboBox(names);info=new JTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(new ItemListener()public void itemStateChanged(ItemEvent e)int index=list.getSelectedIndex();info.setText("商品名:"+goodsindex0+"n");info.append("產(chǎn) 地:"+goodsindex1+"n");info.append("價(jià) 格:"+goodsindex2+"n"););this.setSize(250,300);this.setVisible(true); public static void main (String args) Goods ccc=new Goods(); ccc.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); );