Java-2实用教程(第5版)习题解答 联系客服

发布时间 : 星期四 文章Java-2实用教程(第5版)习题解答更新完毕开始阅读cfb4749dd05abe23482fb4daa58da0116c171f28

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(\面积:\ }

catch(Exception ex) {

showArea.append(\ } } }

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(第10章)

一、问答题

1. 如果准备按字节读取一个文件的内容,应当使用FileInputStream流还是FileReader流? 2. FileInputStream流的read方法和FileReader流的read方法有何不同? 3. BufferedReader流能直接指向一个文件吗?

4. 使用ObjectInputStream和ObjectOutputStream类有哪些注意事项? 5. 怎样使用输入、输出流克隆对象?

1.使用FileInputStream。

2.FileInputStream按字节读取文件,FileReader按字符读取文件。 3.不可以。

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

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

1.下列哪个叙述是正确的? c A.创建File对象可能发生异常。

B.BufferedRead流可以指向FileInputStream流。 C.BufferedWrite流可以指向FileWrite流。

D.RandomAccessFile流一旦指向文件,就会刷新该文件。

2.为了向文件hello.txt尾加数据,下列哪个是正确创建指向hello.txt的流? b A.try { OutputStream out = new FileOutputStream (\ }

catch(IOException e){}

B.try { OutputStream out = new FileOutputStream (\ }

catch(IOException e){}

C.try { OutputStream out = new FileOutputStream (\ }

catch(IOException e){}

D.try { OutputStream out = new OutputStream (\