Java程序设计精编教程(第2版)习题解答

上传人:ch****o 文档编号:158136304 上传时间:2022-10-03 格式:DOC 页数:26 大小:111.01KB
返回 下载 相关 举报
Java程序设计精编教程(第2版)习题解答_第1页
第1页 / 共26页
Java程序设计精编教程(第2版)习题解答_第2页
第2页 / 共26页
Java程序设计精编教程(第2版)习题解答_第3页
第3页 / 共26页
点击查看更多>>
资源描述
习题解答习题一(第1章)1James Gosling2需3个步骤:1) 用文本编辑器编写源文件。 2) 使用javac编译源文件,得到字节码文件。3) 使用解释器运行程序。3set classpath=D:jdkjrelibrt.jar;.;4. B5. Java源文件的扩展名是.java,Java字节码的扩展名是.class。6D。 习题二(第2章)1Oxab187lader18.12height165.65bottomOxab187ractangle109.87width25.18height2Teacher.javapublic class Teacher double 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.add(12,236); System.out.println(zhang.add(234,120); Student jiang=new Student(); jiang.speak(); 3 如果源文件中有多个类,但没有public类,那么源文件的名字只要和某个类的名字相同,并且扩展名是.java就可以了,如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同,扩展名是.java。4行尾风格。习题三(第3章)1用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。true不是标识符。2关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。不是关键字。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+) System.out.print( +c); 7不可以。习题四(第4章)1110。不规范。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|number=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(输入电量:); amount =reader.nextDouble(); if(amount =1) price = amount*0.6;/计算price的值 else if(amount =91) price = 90*0.6+(amount-90)*1.1;/计算price的值 else if(amount150) price = 90*0.6+(150-90)*1.1+(amount-150)*1.7;/计算price的值 else System.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(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;ji;j+) if(i%j=0) sum=sum+j; if(sum=i) System.out.println(完数:+i); 7public class E public static void main(String args) int n=1,i=1,jiecheng=1; long sum=0; while(true) jiecheng=1; for(i=1;i9876) break; n+; System.out.println(满足条件的最大整数:+(n-1); 习题五(第5章)1用类创建对象时。2一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同,即或者是参数的个数不同,或者是参数的类型不同。可以。3可以。不可以。4不可以。5一个类通过使用new运算符可以创建多个不同的对象,不同的对象的实例变量将被分配不同的内存空间。所有对象的类变量都分配给相同的一处内存,对象共享类变量。6CD。7【代码1】【代码4】。8sum=-100。9. 27。10100和20.0。习题六(第6章)1如果子类和父类在同一个包中,那么,子类自然地继承了其父类中不是private的成员变量作为自己的成员变量,并且也自然地继承了父类中不是private的方法作为自己的方法, 继承的成员或方法的访问权限保持不变。当子类和父类不在同一个包中时,父类中的private和友好访问权限的成员变量不会被子类继承,也就是说,子类只继承父类中的protected和public访问权限的成员变量作为子类的成员变量;同样,子类只继承父类中的protected和public访问权限的方法作为子类的方法。如果所声明的成员的变量的名字和从父类继承来的成员变量的名字相同(声明的类型可以不同),在这种情况下,子类就会隐藏掉所继承的成员变量。2不可以。3abstract类。4A类是B类的父类,当用子类创建一个对象b,并把这个对象b的引用放到父类的对象a中时,称a是b的上转型对象。5AD。6 15.08.0。7 98.012。习题七(第7章)1不能。2不能。3可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。4 15.08。5 1815。习题八(第8章)1有效。2可以。3不可以。4大家好,祝工作顺利!习题九(第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.concat(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(String 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();/返回号码的一维数组 char str=日一二三四五六.toCharArray(); for(char c:str) System.out.printf(%3c,c); for(int i=0;ia.length;i+) /输出数组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) 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=10|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;iweekDay;i+) ai= ; for(int i=weekDay,n=1;iweekDay+day;i+) ai=String.valueOf(n) ; n+; for(int i=weekDay+day;i=0) m=m-1; random.seek(m); int c=random.readByte(); if(c=0) System.out.print(char)c); else m=m-1; random.seek(m); byte cc=new byte2; random.readFully(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 BufferedReader(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(); 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(0123456789.+); while(sc.hasNext() 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); 习题十一(第11章)1Frame容器的默认布局是BorderLayout布局。2不可以。3 import java.awt.*;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 FlowLayout(); 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 void changedUpdate(DocumentEvent e) String s=text1.getText(); String a =s.split(0123456789.+); sum=0; aver=0; for(int i=0;ia.length;i+) try sum=sum+Double.parseDouble(ai); catch(Exception ee) aver=sum/count; text2.setText(null); text2.append(n和:+sum); text2.append(n平均值:+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 ComputerFrame(); 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=new 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(buttonSub); 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 actionPerformed(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(请输入数字字符); else if(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(请输入数字字符); else if(e.getSource()=buttonMul) 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(请输入数字字符); else if(e.getSource()=buttonDiv) 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(请输入数字字符); validate(); 5. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class E public static void main(String args) Window win = new Window(); win.setTitle(使用MVC结构); win.setBounds(100,100,420,260); class Window extends JFrame implements ActionListener Lader lader; /模型 JTextField textAbove,textBottom,textHeight; /视图 JTextArea showArea; /视图 JButton controlButton; /控制器 Window() 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 JPanel(); 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(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(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; 习题十二(第12章)14种状态:新建、运行、中断和死亡。2有4种原因的中断:(1)JVM将CPU资源从当前线程切换给其他线程,使本线程让出CPU的使用权处于中断状态。(2)线程使用CPU资源期间,执行了sleep(int millsecond)方法,使当前线程进入休眠状态。(3)线程使用CPU资源期间,执行了wait()方法,使得当前线程进入等待状态。(4)线程使用CPU资源期间,执行某个操作进入阻塞状态,比如执行读/写操作引起阻塞。3死亡状态,不能再调用start()方法。4新建和死亡状态。5两种方法:用Thread类或其子类。6使用setPrority(int grade)方法。7Java使我们可以创建多个线程,在处理多线程问题时,我们必须注意这样一个问题:当两个或多个线程同时访问同一个变量,并且一个线程需要修改这个变量。我们应对这样的问题作出处理,否则可能发生混乱。8当一个线程使用的同步方法中用到某个变量,而此变量又需要其它线程修改后才能符合本线程的需要,那么可以在同步方法中使用wait()方法。使用wait方法可以中断方法的执行,使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。其它线程如果在使用这个同步方法时不需要等待,那么它使用完这个同步方法的同时,应当用notifyAll()方法通知所有的由于使用这个同步方法而处于等待的线程结束等待。9不合理。10“吵醒”休眠的线程。一个占有CPU资源的线程可以让休眠的线程调用interrupt 方法“吵醒”自己,即导致休眠的线程发生InterruptedException异常,从而结束休眠,重新排队等待CPU资源。11. public class E public static void main(String args) Cinema a=new Cinema(); a.zhang.start(); a.sun.start(); a.zhao.start(); class TicketSeller /负责卖票的类。 int fiveNumber=3,tenNumber=0,twentyNumber=0; public synchronized void sellTicket(int receiveMoney) if(receiveMoney=5) fiveNumber=fiveNumber+1; System.out.println(Thread.currentThread().getName()+给我5元钱,这是您的1张入场卷); else if(receiveMoney=10) while(fiveNumber1) try System.out.println(Thread.currentThread().getName()+靠边等); wait(); System.out.println(Thread.currentThread().getName()+结束等待); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber+1; System.out.println(Thread.currentThread().getName()+给我10元钱,找您5元,这是您的1张入场卷); else if(receiveMoney=20) while(fiveNumber1|tenNumber1) try System.out.println(Thread.currentThread().getName()+靠边等);
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!