《java2实用教程》(第四版)课后答案 第九章-第十三章.doc_第1页
《java2实用教程》(第四版)课后答案 第九章-第十三章.doc_第2页
《java2实用教程》(第四版)课后答案 第九章-第十三章.doc_第3页
《java2实用教程》(第四版)课后答案 第九章-第十三章.doc_第4页
《java2实用教程》(第四版)课后答案 第九章-第十三章.doc_第5页
免费预览已结束,剩余22页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java作业4(第九章-第十三章)第九章3.1.1程序代码import java.awt.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class kehou31 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;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) 4.1.2运行界面4.2.1程序代码 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) 4.3.1程序代码import java.io.*;import java.util.*;public class kehou1043 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); 4.3.2运行界面第十一章2.1.1程序代码import java.sql.*;import java.util.*;public class kehou1121 public static void main(String args) Query query=new Query(); String dataSource=myData; String tableName=goods; Scanner read=new Scanner(System.in); System.out.print(输入数据源名:); dataSource = read.nextLine(); System.out.print(输入表名:); tableName = read.nextLine(); query.setDatasourceName(dataSource); query.setTableName(tableName); query.setSQL(SELECT * FROM +tableName); query.inputQueryResult(); class Query String datasourceName=; /数据源名 String tableName=; /表名 String SQL; /SQL语句 public Query() try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(ClassNotFoundException e) System.out.print(e); public void setDatasourceName(String s) datasourceName = s.trim(); public void setTableName(String s) tableName = s.trim(); public void setSQL(String SQL) this.SQL = SQL.trim(); public void inputQueryResult() Connection con; Statement sql; ResultSet rs; try String uri = jdbc:odbc:+datasourceName; String id = ; String password = ; con = DriverManager.getConnection(uri,id,password); DatabaseMetaData metadata = con.getMetaData(); ResultSet rs1 = metadata.getColumns(null,null,tableName,null); int 字段个数 = 0; while(rs1.next() 字段个数+; sql = con.createStatement();/创建SQL语句 rs = sql.executeQuery(SQL);/执行SQL语句 while(rs.next() for(int k=1;k=字段个数;k+) System.out.print( +rs.getString(k)+ ); System.out.println(); con.close(); catch(SQLException e) System.out.println(请输入正确的表名+e); 2.1.2运行界面2.2.1程序代码import java.sql.*;import java.util.*;public class kehou1122 public static void main(String args) Q query = new Q(); String dataSource = myData; String tableName = goods; query.setDatasourceName(dataSource); query.setTableName(tableName); String name = ; Scanner read=new Scanner(System.in); System.out.print(商品名:); name = read.nextLine(); String str=%+name+%; String SQL = SELECT * FROM +tableName+ WHERE name LIKE +str; query.setSQL(SQL); System.out.println(tableName+表中商品名是+name+的记录); query.inputQueryResult(); class Q String datasourceName=; /数据源名 String tableName=; /表名 String SQL; /SQL语句 public Q() try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(ClassNotFoundException e) System.out.print(e); public void setDatasourceName(String s) datasourceName = s.trim(); public void setTableName(String s) tableName = s.trim(); public void setSQL(String SQL) this.SQL = SQL.trim(); public void inputQueryResult() Connection con; Statement sql; ResultSet rs; try String uri = jdbc:odbc:+datasourceName; String id = ; String password = ; con = DriverManager.getConnection(uri,id,password); DatabaseMetaData metadata = con.getMetaData(); ResultSet rs1 = metadata.getColumns(null,null,tableName,null); int 字段个数 = 0; while(rs1.next() 字段个数+; sql = con.createStatement(); rs = sql.executeQuery(SQL); while(rs.next() for(int k=1;k=字段个数;k+) System.out.print( +rs.getString(k)+ ); System.out.println(); con.close(); catch(SQLException e) System.out.println(请输入正确的表名+e); 2.2.2运行界面2.3.1程序代码import java.sql.*;import java.util.*;public class kehou1123 public static void main(String args) Q1 query = new Q1(); String dataSource = myData; String tableName = goods; query.setDatasourceName(dataSource); query.setTableName(tableName); String SQL = SELECT * FROM +tableName+ ORDER BY madeTime; query.setSQL(SQL); System.out.println(tableName+表记录按商品生产日期前后排序是: ); query.inputQueryResult(); class Q1 String datasourceName=; /数据源名 String tableName=; /表名 String SQL; /SQL语句 public Q1() try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(ClassNotFoundException e) System.out.print(e); public void setDatasourceName(String s) datasourceName = s.trim(); public void setTableName(String s) tableName = s.trim(); public void setSQL(String SQL) this.SQL = SQL.trim(); public void inputQueryResult() Connection con; Statement sql; ResultSet rs; try String uri = jdbc:odbc:+datasourceName; String id = ; String password = ; con = DriverManager.getConnection(uri,id,password); DatabaseMetaData metadata = con.getMetaData(); ResultSet rs1 = metadata.getColumns(null,null,tableName,null); int 字段个数 = 0; while(rs1.next() 字段个数+; sql = con.createStatement(); rs = sql.executeQuery(SQL); while(rs.next() for(int k=1;k=字段个数;k+) System.out.print( +rs.getString(k)+ ); System.out.println(); con.close(); catch(SQLException e) System.out.println(请输入正确的表名+e); 2.3.2运行界面第十二章4.1.1程序代码public class kehou1241 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()+靠边等); wait(); System.out.println(Thread.currentThread().getName()+结束等待); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber-1; twentyNumber=twentyNumber+1; System.out.println(Thread.currentThread().getName()+给20元钱,找您一张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 run() if(Thread.currentThread()=zhang) seller.sellTicket(20); else if(Thread.currentThread()=sun) seller.sellTicket(10); else if(Thread.currentThread()=zhao) seller.sellTicket(5); 4.1.2运行界面4.2.1程序代码public class Example12_6 public static void main(String args) ClassRoom room6501=new ClassRoom();room6501.student1.start();room6501.teacher.start();room6501.student2.start(); class ClassRoom implements RunnableThread student1,teacher,student2;ClassRoom()teacher=new Thread(this);student1=new Thread(this);student2=new Thread(this);teacher.setName(王教授);student1.setName(张三);student2.setName(赵四);public void run()if(Thread.currentThread()=student1)trySystem.out.println(student1.getName()+正在睡觉,不听课);Thread.sleep(1000*60*60);catch(InterruptedException e)System.out.println(student1.getName()+被老师叫醒了);System.out.println(student1.getName()+开始听课);errupt();else if(Thread.currentThread()=teacher)for(int i=1;i=3;i+)System.out.println(上课!);tryThread.sleep(500);catch(InterruptedException e)errupt();else if(Thread.currentThread()=student2)trySystem.out.println(s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论