欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOC文档下载  

    《JAVA2实用教程》(第四版)课后答案 第九章第十三章.doc

    • 资源ID:2387258       资源大小:282.50KB        全文页数:30页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    《JAVA2实用教程》(第四版)课后答案 第九章第十三章.doc

    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<a.length;i+) try sum=sum+Double.parseDouble(ai); catch(Exception ee) count=a.length; 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); 3.1.2 运行界面3.2.1程序代码import java.awt.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class kehou932 public static void main(String args) ComputerFrame fr=new ComputerFrame(); fr.setBounds(100,100,650,120); fr.setTitle("多功能计算器"); fr.setBackground(Color.blue); 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(); 3.2.2运行界面3.3.1 程序代码import java.awt.*;import java.awt.event.*;import javax.swing.*;public class kehou933 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; 3.3.2运行界面第十章4.1.1程序代码import java.io.*;public class E public static void main(String args) File f=new File("E.java"); 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(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()

    注意事项

    本文(《JAVA2实用教程》(第四版)课后答案 第九章第十三章.doc)为本站会员(仙人指路1688)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开