云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Java-2实用教程(第5版)习题解答

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/6 11:25:35

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 (\

搜索更多关于: Java-2实用教程(第5版)习题解答 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

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

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com