下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. 习题解答 习题一(第 1 章) 1. James Gosling 2 .需 3 个步骤: 1) 用文本编辑器编写源文件。 2) 使用 javac 编译源文件,得到字节码文件。 3) 使用解释器运行程序。 3. set classpath=D:jdkjrelibrt.jar;.; 4. B 5. Java 源文件的扩展名是.java , Java 字节码的扩展名是.class 6. Do 习题二(第 2 章) 1. racta ng 109.87 widt lader 18 12 heig Oxab187 1 25 18 heig 、165 65 bott 2 Oxab187 Teache
2、r.java public class Teacher double add(double a,double b) return a+b; double sub(double a,double b) return a-b; Student.java public class Student public void speak() MainClass.java public class MainClass public static void main(String args) Teacher zhang=new Teacher(); Student jiang=new Student(); j
3、iang.speak(); 3 如果源文件中有多个类,但没有 public 类,那么源文件的名字只要和某个类的名字相 同,并且扩展名是 .java 就可以了,如果有一个类是 public 类,那么源文件的名字必须与 这个类的名字完全相同,扩展名是 .java 。 4行尾风格。 习题三(第 3 章) 1用来标识类名、 变量名、方法名、类型名、数组名、 文件名的有效字符序列称为标识符。 标识符由字母、 下划线、 美元符号和数字组成, 第一个字符不能是数字。 true 不是标识符 2关键字就是 Java 语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来 用。不是关键字。 class im
4、plements 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+) 7不可以。 习题四(第 4 章) 1110。不规范。 2新亲亲斤 ! 。 3 public class JudgeAward void g
5、iveMess(int number) if(number=9|number=131|number=12) else if(number=209|number=596|number=27) else if(number=875|number=316|number=59) price = 90*0.6+(amount-90)*1.1;/ 计算 price 的值 else 4 public class Computer public static void main(String args) Scanner reader=new Scanner(System.in); double amount
6、= 0; / 存放电量 double price = 0; / 用户需要交纳的电费 amount =reader.nextDouble(); if(amount =1) price = amount*0.6;/ 计算 price 的值 else if(amount =91) else if(amount150) price = 90*0.6+(150-90)*1.1+(amount-150)*1.7;/ 计算 price 的值 else 5public class E public static void main (String args ) char cStart=A,cEnd=Z; fo
7、r(char c=cStart;c=cEnd;c+) for(char c=cStart;c=cEnd;c+) 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) 7 public class E public static void main(String args) int n=1,i=1,jiecheng=1; long sum=0; while(true)
8、 jiecheng=1; for(i=1;i9876) break; 9. 27。 n+; 习题五(第 5 章) 1用类创建对象时。 2一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同,即或者是参数 的个数不同,或者是参数的类型不同。可以。 3可以。不可以。 4不可以。 5. 个类通过使用 new运算符可以创建多个不同的对象,不同的对象的实例变量将被分配 不同的内存空间。所有对象的类变量都分配给相同的一处内存,对象共享类变量。 6. CD。 7. 【代码 1】【代码 4】。 8. sum=-100。10100 和 20.0。 习题六(第 6 章) 12 1如果子类和父类在同一个包
9、中,那么,子类自然地继承了其父类中不是 private 的成员 变量作为自己的成员变量,并且也自然地继承了父类中不是 private 的方法作为自己的方 法 , 继承的成员或方法的访问权限保持不变。当子类和父类不在同一个包中时,父类中的 private 和友好访问权限的成员变量不会被子类继承,也就是说,子类只继承父类中的 protected 和 public 访问权限的成员变量作为子类的成员变量;同样,子类只继承父类中 的 protected 和 public 访问权限的方法作为子类的方法。 如果所声明的成员的变量的名字 和从父类继承来的成员变量的名字相同 (声明的类型可以不同 ) ,在这种情
10、况下,子类就会 隐藏掉所继承的成员变量。 2不可以。 3abstract 类。 4. A 类是 B 类的父类,当用子类创建一个对象 b,并把这个对象 b 的引用放到父类的对象 a 中时,称 a 是 b 的上转型对象。 5. AD。 6. 15.0 8.0 。 7. 98.01ABD。 习题七(第 7 章) 1不能。 2不能。 3可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中 变量就可以调用被类实现的接口中的方法。 4 15.0 8。 5 18 15。 习题八(第 8 章) 1有效。 2可以。 3不可以。 4大家好,祝工作顺利! 习题九(第 9 章) 2Love:Game。 那
11、么该接口 3 15 abc 我们。 413579。 59javaHello 。 6public class E public static void main (String args ) String s1,s2,t1=ABCDabcd; s1=t1.toUpperCase(); s2=t1.toLowerCase(); String s3=s1.concat(s2); 7. public class E public static void main (String args ) String s=ABCDabcd; char cStart=s.charAt(0); char cEnd =
12、 s.charAt(s.length()-1); 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; year=read.nextInt(); month=read.nextInt(); cb.setYear(year); cb.setMonth(month); String a= cb.getCalenda
13、r();/ 返回号码的一维数组 char str= 日一二三四五六 .toCharArray(); for(char c:str) for(int i=0;ia.length;i+) / 输出数组 a if(i%7=0) 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 S
14、tring42; 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)
15、|(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) else m=m-1; random.seek(m); byte cc=new byte2; random.readFully(cc); catch(Exception
16、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
17、 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 cl
18、ass 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); while(sc.hasNext() try double price = sc.nextDouble(); count+; sum = sum+price; catch(InputMismatchException exp) String t = sc.next(); catch(Exception
19、exp) 习题十一(第 11 章) 1Frame 容器的默认布局是 BorderLayout 布局。 2不可以。 3 import java.awt.*; import javax.swing.*; 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; Compu
20、ter() 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.DI
21、SPOSE_ON_CLOSE); public void changedUpdate(DocumentEvent e) String s=text1.getText(); 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(Do
22、cumentEvent e) changedUpdate(e); public void insertUpdate(DocumentEvent e) changedUpdate(e); 4. import java.awt.*; import javax.swing.*; public class E public static void main(String args) ComputerFrame fr=new ComputerFrame(); class ComputerFrame extends JFrame implements ActionListener JTextField t
23、ext1,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)
24、; 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
25、); 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(tex
26、t2.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
27、); 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)
28、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.
29、awt.*; 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 showAr
30、ea; / 视图 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 JBu
31、tton( 计算面积 ); 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);
32、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.setAb
33、ove(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
34、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 各 CPL 资源从当前线程切换给其他线程, 使本线程让出 CPU 的使用权处于中断状态。(2)线程使用 CPU 资源期间,执行了 sleep(int millsecond) 方 法,使当前线程进入休眠状态。(3)线程使用 CPU 资源期间,执行了 wait(
35、)方法,使得当 前线程进入等待状态。(4)线程使用 CPU 资源期间,执行某个操作进入阻塞状态,比如执 行读/ 写操作引起阻塞。 3死亡状态,不能再调用 start() 方法。 4新建和死亡状态 5两种方法:用 Thread 类或其子类。 6使用 setPrority(int grade) 方法。 7 Java 使我们可以创建多个线程,在处理多线程问题时,我们必须注意这样一个问题: 当两个或多个线程同时访问同一个变量,并且一个线程需要修改这个变量。我们应对这样 的问题作出处理,否则可能发生混乱。 8当一个线程使用的同步方法中用到某个变量, 而此变量又需要其它线程修改后才能符合 本线程的需要,那
36、么可以在同步方法中使用 wait() 方法。使用 wait 方法可以中断方法的 执行,使本线程等待,暂时让出 CPU 的使用权,并允许其它线程使用这个同步方法。其它 线程如果在使用这个同步方法时不需要等待, 那么它使用完这个同步方法的同时, 应当用 notifyAll() 方法通知所有的由于使用这个同步方法而处于等待的线程结束等待。 9不合理。 10. “吵醒”休眠的线程。一个占有 CPU 资源的线程可以让休眠的线程调用 interrupt 方 法“吵醒”自己,即导致休眠的线程发生 InterruptedException 异常,从而结束休眠,重 新排队等待 CPU资源。 11. public
37、 class E public static void main(String args) Cinema a=new Cinema(); class TicketSeller / 负责卖票的类。 int fiveNumber=3,tenNumber=0,twentyNumber=0; public synchronized void sellTicket(int receiveMoney) if(receiveMoney=5) fiveNumber=fiveNumber+1; 给我 5 元钱,这是您的 1 张入场卷); else if(receiveMoney=10) while(fiveNu
38、mber1) wait(); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber+1;给我 10 元钱,找您 5 元,这是您的 1 张入场卷); else if(receiveMoney=20) while(fiveNumber1|tenNumber1) wait(); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber-1; twentyNumber=twentyNumber+1; 给 20 元钱,
39、找您一张 5 元和一张 10 元,这是您的 1 张入场卷); notifyAll(); class Cinema implements Runnable Thread zhang,sun,zhao; TicketSeller seller; Cinema() zhang=new Thread(this); sun=new Thread(this); zhao=new Thread(this); zhang.setName( 张小有 ); sun.setName( 孙大名 ); zhao.setName( 赵中堂 ); seller=new TicketSeller(); public void
40、 run() if(Thread.currentThread()=zhang) seller.sellTicket(20); else if(Thread.currentThread()=sun) seller.sellTicket(10); else if(Thread.currentThread()=zhao) seller.sellTicket(5); public class E public static void main(String args) ClassRoom room6501 = new ClassRoom(); try Thread.sleep(1000); catch
41、(Exception exp) class ClassRoom implements Runnable Thread student1,student2,teacher; ClassRoom() teacher = new Thread(this); student1 = new Thread(this); 12. student2 = new Thread(this); teacher.setName( 王教授 ); student1.setName( 张三); student2.setName( 李四); public void run() if(Thread.currentThread(
42、)=student1) Thread.sleep(1000*60*10); catch(InterruptedException e) errupt(); / 吵醒 student2 else if(Thread.currentThread()=student2) Thread.sleep(1000*60*60); catch(InterruptedException e) else if(Thread.currentThread()=teacher) for(int i=1;i=3;i+) try Thread.sleep(500); catch(Interrupte
43、dException e) errupt(); / 吵醒 student1 13. public class E public static void main(String args) JoinWork work=new JoinWork(); work. 司机 .start(); class JoinWork implements Runnable Thread 司机 ; Thread 装运工 ; Thread 管理员 ; JoinWork() 司机 =new Thread(this); 装运工=new Thread(this); 管理员=new Thread(th
44、is); public void run() if(Thread.currentThread()= try 装运工 .start(); 装运工 .join(); catch(InterruptedException e)司机 ) else if(Thread.currentThread()= 装运工) try 管理员 .start(); 管理员 .join(); catch(InterruptedException e) else if(Thread.currentThread()= 管理员 ) 14. BA 习题十三(第 13 章) 1. URL 对象调用InputStream openSt
45、ream()方法可以返回一个输入流, 该输入流指向 URL 对象所包含的资源。通过该输入流可以将服务器上的资源信息读入到客户端。 2. 客户端的套接字和服务器端的套接字通过输入、输出流互相连接后进行通信。 3. 使用方法 accept() , accept() 会返回一个和客户端 Socket 对象相连接的 Socket 对象。 accept 方法会堵塞线程的继续执行,直到接收到客户的呼叫。 。 else if(Thread.currentThread()= 装运工) 4. 域名/IP 。 5. (1)客户端 import .*; import java.io.*; import java.a
46、wt.*; import javax.swing.*; public class Client public static void main(String args) new ComputerClient(); class ComputerClient extends Frame implements Runnable,ActionListener Button connection,send; TextField inputText,showResult; Socket socket=null; DataInputStream in=null; DataOutputStream out=n
47、ull; Thread thread; ComputerClient() socket=new Socket(); setLayout(new FlowLayout(); Box box=Box.createVerticalBox(); connection=new Button( 连接服务器 ); send=new Button( 发送 ); send.setEnabled(false); inputText=new TextField(12); showResult=new TextField(12); box.add(connection); box.add(new Label( 输入三
48、角形三边的长度 , 用逗号或空格分隔 :); box.add(inputText); box.add(send); box.add(new Label( 收到的结果: ); box.add(showResult); connection.addActionListener(this); send.addActionListener(this); thread=new Thread(this); add(box); setBounds(10,30,300,400); setVisible(true); validate(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) if(e.getSource()=connection) try / 请求和服务器建立套接字连接: if(socket.isConnected() else InetSocke
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《GB-T 39119-2020综合能源 泛能网协同控制总体功能与过程要求》专题研究报告
- 《GB-T 24717-2009道路预成形标线带》专题研究报告
- 2026年抚顺师范高等专科学校单招职业倾向性测试题库及参考答案详解1套
- 《正常人体功能》课件-骨骼肌的收缩原理
- 运维效率提升服务协议
- 终端销售主管岗位招聘考试试卷及答案
- 2025年安管人员考试题目及答案
- 2025届中国土木工程集团有限公司高校毕业生招聘笔试参考题库附带答案
- 2025年压实机械项目合作计划书
- 冠脉支架术后的随访要点
- 玉米质押合同范本
- 2025西部机场集团航空物流有限公司招聘笔试考试参考题库及答案解析
- 2025四川成都东部新区招聘编外工作人员29人笔试考试参考试题及答案解析
- 《11845丨中国法律史(统设课)》机考题库
- 2025年消防设施操作员中级理论考试1000题(附答案)
- 2026年日历表含农历(2026年12个月日历-每月一张A4可打印)
- GA 38-2021银行安全防范要求
- Mill准则-吉林大学课程中心课件
- 猫(猫的形态、习性、繁殖)-课件
- 仔猪腹泻综合防治(多图详解)课件
- 混沌学园106正式版PPT!李善友:《本体论:每个人都需要的哲学思维训练》
评论
0/150
提交评论