77范文网 - 专业文章范例文档资料分享平台

java2实用教程第4版答案(4)

来源:网络收集 时间:2018-12-29 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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

一、问答题

1.使用FileInputStream。

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

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

5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。 二、选择题 1.C。2.B。 三、阅读程序 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(\

try{ RandomAccessFile random=new RandomAccessFile(f,\ 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(\

File tempFile=new File(\

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+\ 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(\ Scanner sc = null; double sum=0; int count = 0;

try { sc = new Scanner(file);

sc.useDelimiter(\ while(sc.hasNext()){

try{ double price = sc.nextDouble(); count++; sum = sum+price; System.out.println(price); }

catch(InputMismatchException exp){ String t = sc.next(); } }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java2实用教程第4版答案(4)在线全文阅读。

java2实用教程第4版答案(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/396732.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: