Java2实用教程(第5版)最新答案-耿祥义 联系客服

发布时间 : 星期三 文章Java2实用教程(第5版)最新答案-耿祥义更新完毕开始阅读0e48a01ff56527d3240c844769eae009591ba25e

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

System.out.println(\平均价格:\ }

catch(Exception exp){ System.out.println(exp); } } }

习题十一(第11章)

一、问答题