《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()