知ing

Java2实用教程(第4版)

耿祥义、张跃平 / 清华大学出版社

頑張って! 上传

查看本书

习题九(第9章)

一、问答题

1Frame容器的默认布局是BorderLayout布局。

2.不可以。

3ActionEvent

4DocumentEvent

55个。

6MouseMotionListener

二、选择题

1C2A3A4D5C

三、编程题

1 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;i<a.length;i++) {

        try { sum=sum+Double.parseDouble(a[i]);

        }

        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);

   }

}

2.   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();

  }

}

3.   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;

   }

}

习题十(第10章)

一、问答题

1.使用FileInputStream

2FileInputStream按字节读取文件,FileReader按字符读取文件

3.不可以。

4.使用对象流写入或读入对象时,要保证对象是序列化的。

5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。

二、选择题

1C2B

三、阅读程序

1.【代码1】:51。【代码2】:0

2.【代码1】:3。【代码2】:abc代码3】:1。【代码4】:dbc

3上机实习题,解答略。

四、编程题

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 byte[2];

                    random.readFully(cc);

                    System.out.print(new String(cc));

                  }

              }

       }

       catch(Exception exp){}

    }

}

2.   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){}

   }

}

3.   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);

      }

   }

}


查看更多