JAVA实验6答案
java答案第六章

Java语言程序设计第六章课后习题答案1.将本章例6-1至6-18中出现的文件的构造方法均改为使用File类对象作为参数实现。
个人理解:File类只能对整文件性质进行处理,而没法通过自己直接使用file.Read()或者是file.write()类似方法对文件内容进行写或者读取。
注意:是直接;下面只提供一个例2变化,其他的你自己做,10几道啊,出这题的人真他妈有病。
import java.io.*;public class test6_2{public static void main(String[] args) throws IOException { String fileName = "D:\\Hello.txt";File writer=new File(fileName);writer.createNewFile();BufferedWriter input = new BufferedWriter(newFileWriter(writer));input.write("Hello !\n");input.write("this is my first text file,\n");input.write("你还好吗?\n");input.close();}}运行结果:(电脑系统问题,没法换行,所以一般使用BuffereWriter中newLine()实现换行)2.模仿文本文件复制的例题,编写对二进制文件进行复制的程序.// CopyMaker类import java.io.*;class CopyMaker {String sourceName, destName;BufferedInputStream source;BufferedOutputStream dest;int line;//打开源文件和目标文件,无异常返回trueprivate boolean openFiles() {try {source = new BufferedInputStream(newFileInputStream( sourceName ));}catch ( IOException iox ) {System.out.println("Problem opening " + sourceName );return false;}try {dest = new BufferedOutputStream(newFileOutputStream( destName ));}catch ( IOException iox ){System.out.println("Problem opening " + destName );return false;}return true;}//复制文件private boolean copyFiles() {try {line = source.read();while ( line != -1 ) {dest.write(line);line = source.read();}}catch ( IOException iox ) {System.out.println("Problem reading or writing" );return false;}return true;}//关闭源文件和目标文件private boolean closeFiles() {boolean retVal=true;try { source.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + sourceName );retVal = false;}try { dest.close(); }catch ( IOException iox ) {System.out.println("Problem closing " + destName );retVal = false;}return retVal;}//执行复制public boolean copy(String src, String dst ) {sourceName = src ;destName = dst ;return openFiles() && copyFiles() && closeFiles();}}//test6_2public class test6_2{public static void main ( String[] args ) {String s1="lin.txt",s2="newlin.txt";if(new CopyMaker().copy(s1, s2))S ystem.out.print("复制成功");elseS ystem.out.print("复制失败");}}运行前的两个文本:lin.txt和newlin.txt(为空)运行后:3.创建一存储若干随机整数的文本文件,文件名、整数的个数及范围均由键盘输入。
Java实验指导书答案

课 程 :Java面向对象程序设计语言 院 (部): 专 业 :电子商务 班 级: 学生姓名:
学 号: 指导教师: 完成时间:2014年6月
目录
实验一:JDK安装与配置 3 实验二:Java基本语法练习 4 实验三:数组和字符串练习 5 实验四:类和对象程序设计 6 实验五:接口、内部类与包练习 7 实验六:图形界面设计 8 实验七:线程设计 9 实验八:输入输出流设计 10 实验九:Applet编程 10 实验十: 网络程序设计 10
public class e33 {
public static void main(String[] args) {
BufferedReader str=new BufferedReader( new InputStreamReader(System.in)); try {
String a=str.readLine(); } catch (IOException e) {
/**
* @param args */ public static void main(String[] args) { System.out.println(new CnUpperCaser("123456789.12345").getCnString()); System.out.println(new CnUpperCaser("123456789").getCnString()); System.out.println(new CnUpperCaser(".123456789").getCnString()); System.out.println(new CnUpperCaser("0.1234").getCnString()); System.out.println(new CnUpperCaser("1").getCnString()); System.out.println(new CnUpperCaser("12").getCnString()); System.out.println(new CnUpperCaser("123").getCnString()); System.out.println(new CnUpperCaser("1234").getCnString()); System.out.println(new CnUpperCaser("12345").getCnString()); System.out.println(new CnUpperCaser("123456").getCnString()); System.out.println(new CnUpperCaser("1234567").getCnString()); System.out.println(new CnUpperCaser("12345678").getCnString()); System.out.println(new CnUpperCaser("123456789").getCnString()); } }
最新实验10--java异常(答案)

实验六Java异常处理【实验目的】1)掌握Java异常的概念及工作机制2)掌握异常的分类3)掌握抛出异常语句的使用(throw)4)掌握抛出捕获处理异常语句的使用(try…catch…finally)5)掌握上抛异常语句的使用(throws)6)掌握创建自定义异常【实验环境】JDK1.6+Eclpise3.2【实验准备】1)复习课件中理论知识2)练习课堂所讲的例子【实验内容】1、编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。
测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。
package SIX;import java.io.IOException;import java.io.InputStreamReader;import java.io.BufferedReader;public class CIRCLE {public static void main(String[] args) throws IOException { System.out.print("请输入半径: ");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));try{System.out.println("圆的面积为:"+computerArea(Double.parseDouble(br.readLine())));}catch(NumberFormatException e){System.out.println("您输入的不是数值,请重新输入");}}public static double computerArea(double r){r eturn Math.PI*r*r;}}2、计算两个正数之和,当任意一个数超出范围时,抛出自己定义的异常(NumberRangeException)。
Java6程序设计实践教程实验指导课后答案

第1章:习题集:一、填空题1.多态2.java.exe 3.jdb.exe 4.标准字节码5.Java 6.独立于平台二、选择题1.B 2.A 3.B 4.A 5.A 6.C 7.C 8.D 9.C第2章:实验指导:.第一处需要的代码:yourGuess>realNumber第二处需要的代码:yourGuess=input.nextInt();第三处需要的代码:yourGuess<realNumber第四处需要的代码:yourGuess=input.nextInt();.第一处需要的代码:iArray[i] < iArray[j]第二处需要的代码:iTemp = iArray[i];iArray[i] = iArray[j];iArray[j] = iTemp;.第一处需要的代码:continue lable;第二处需要的代码:System.out.print(" "+(i+j));.第一处需要的代码:System.out.print("\t");第二处需要的代码:System.out.print(j + "*" + i + "=" + (i*j) + "\t");习题集:一、填空题1.i=i+1 sum=sum+1 i<1002.while do while for 3.94.The symbol is an a.The symbol is a b.The symbol is c.The symbol is not a,b,or c.Switch is completed5.sum=0 pos++二、选择题1.D 2.A 3.D 4.A 5.D 6.A 7.B 8.B 9.C三、简答题2、there is no this value 3.Message four i is 1,2,3 i is 1,2,3 i is 1,2,3 i is 4第3章实验指导:.第一处需要的代码:balance+=saveAccount;第二处需要的代码:balance+=calculateInterst(Days);第三处需要的代码:ba.setMoney(save_value);ba.setInterstRate(interst_rate);第四处需要的代码:ba.accountInterst(365);.第一处需要的代码:minute=this.m_Minute;second=this.m_Second;第二处需要的代码:s=d.getMinutes()+":"+d.getSeconds()+":"+d.getTime();.第一处需要的代码:super(number,pass);balance=bal;第二处需要的代码:connect.第一处需要的代码:super(pass, motor);this.make=make;this.model=model;第二处需要的代码:am.run();习题集:一、填空题1.类2.代码和数据3.点4.实例变量5.Test te=new Test();6.对象实例化7.值、引用8.the original is data is:-1now the data is:109.I am parentI am childI am child二、选择题1.B 2.A 3.C 4.C 5.A 6.A 7.C 8.A 9.D 10.C 11.A 12.C 13.C 三、简答题6. A abstract和final修饰符不能同时使用B 未给出类型定义,final int MAX_NUMBER=10;C 常量不能修改D 静态方法值能访问静态变量,static int data。
解析JAVA程序设计第六章课后答案

第6章习题解答1.简述Java中设计图形用户界面程序的主要步骤。
对于设计图形用户界面程序而言,一般分为两个步骤:第一步,设计相应的用户界面,并根据需要对相关的组件进行布局;第二步,添加相关的事件处理,如鼠标、菜单、按钮和键盘等事件。
2.试说明容器与组件之间的关系。
组件(component)是图形用户界面中的各种部件(如标签、按钮、文本框等等),所有的组件类都继承自JComponent类。
容器(container)是用来放置其他组件的一种特殊部件,在java中容器用Container类描述。
3.阅读下面程序,说明其运行结果和功能。
//filename:MyFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class MyFrame{public static void main(String agrs[]){JFrame f=new JFrame("简单窗体示例");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JLabel l=new JLabel("习题1");f.getContentPane().add(l,BorderLayout.CENTER);f.pack();f.setVisible(true);}}程序的运行结果如下:4.阅读下面程序,说明其运行结果和功能。
//filename:TestButton.javaimport java.awt.*;import javax.swing.*;public class TestButton extends JFrame{JButton b1,b2;TestButton(String s){super(s);b1=new JButton("按钮1");b2=new JButton("按钮2");setLayout(new FlowLayout());add(b1);add(b2);setSize(300,100);setVisible(true);}public static void main(String args[]){ TestButton test;test=new TestButton("测试按钮"); }}程序的运行结果如下:5.阅读下面程序,说明其运行结果和功能。
java 课后答案06review

Chapter 6 Single-dimensional Arrays1. See the section "Declaring and Creating Arrays."2. You access an array using its index.3. No memory is allocated when an array is declared. The memory is allocated whencreating the array.x is 60The size of numbers is 304. Indicate true or false for the following statements:1. Every element in an array has the same type.Answer: True2. The array size is fixed after it is declared.Answer: False3. The array size is fixed after it is created.Answer: True4. The element in the array must be of primitive data type.Answer: False5. Which of the following statements are valid array declarations?int i = new int(30);Answer: Invaliddouble d[] = new double[30];Answer: Validchar[] r = new char(1..30);Answer: Invalidint i[] = (3, 4, 3, 2);Answer: Invalidfloat f[] = {2.3, 4.5, 5.6};Answer: Validchar[] c = new char();Answer: Invalid6. The array index type is int and its lowest index is 0.a[2]7.(a)double[] list = new double[10];(b)list[list.length – 1] = 5.5;(c)System.out.println(list[0] + list[1]);(d)double sum = 0;for (int i = 0; i < list.length; i++)sum += list[i];(e)double min = list[0];for (int i = 1; i < list.length; i++)if (min > list[i]) min = list[i];(f)System.out.println(list[(int)(Math.random() * list.length));(g)double[] = {3.5, 5.5, 4.52, 5.6};8. A runtime exception occurs.9. Line 3: the array declaration is wrong. It should be double[]. The array needs to becreated before its been used. e.g. new double[10]Line 5: The semicolon (;) at the end of the for loop heading should be removed.Line 5: r.length() should be r.length.Line 6: random should be random()Line 6: r(i) should be r[i].10. System.arraycopy(source, 0, t, 0, source.length);11. The second assignment statement myList = new int[20] creates a new array andassigns its reference to myList.12. False. When an array is passed to a method, the reference value of the array ispassed. No new array is created. Both argument and parameter point to the same array. 13.numbers is 0 and numbers[0] is 3 14.15. Only one variable-length parameter may be specified ina method and this parameter must be the last parameter. The method return type cannot be a variable-length parameter.16.The last oneprintMax(new int[]{1, 2, 3});is incorrect, because the array must of the double[] type.17. Omitted18. Omitted19. Omitted20 Simply change (currentMax < list[j]) on Line 10 to (currentMax > list[j])21 Simply change list[k] > currentElement on Line 9 tolist[k] < currentElement22.You can sort an array of any primitive types except boolean. The sort method is void,so it does not return a new array.23. To apply java.util.Arrays.binarySearch(array, key), the array must be sorted in increasing order.24. Line 1: list is {2, 4, 7, 10}Line 2: list is {7, 7, 7, 7}Line 3: list is {7, 8, 8, 7}Line 4: list is {7, 8, 8, 7}。
AnjoyoJava06课后习题带答案

AnjoyoJava06课后习题一、选择题:1.下面关于异常的说法正确的一项是()。
A、异常就是在程序的运行过程中所发生的不正常的事件,但它不会中断正在运行的程序。
B、Error类和Exception类都是Throwable类的子类。
C、Exception处理的是Java运行环境中的内部错误或者硬件问题,比如,内存资源不足、系统崩溃等。
D、Error处理的是因为程序设计的瑕疵而引起的问题或者外在的输入等引起的一般性问题,例如:在开平方的方法中输入了一个负数,对一个为空的对象进行操作以及网络不稳定引起的读取网络问题等。
2.引起RuntimeException异常的原因不包括下面哪一项()。
A、错误的类型转换B、试图从文件结尾处读取信息C、试图访问一个空对象D、数组越界访问3.引起IOException异常的原因不包括下面哪一项()。
A、试图从文件结尾处读取信息B、试图打开一个不存在或者格式错误的URL。
C、数学计算错误D、用Class.forName()来初始化一个类的时候,字符串参数对应的类不存在。
4.下面哪一项不是RuntimeException异常中的一类()。
A、ClassNotFoundException:无法找到需要的类文件异常B、NumberFormatException:数字转化格式异常C、IllgalArgumentException:非法参数值异常D、IllegalStateException:对象状态异常,如对未初始化的对象调用方法5.IOException异常不包括下面哪一项()。
A、EOFException:读写文件尾异常B、InterruptedException:线程中断C、SocketException:Socket通信异常D、MalformedURLException:URL格式错误异常6.下列关于try-catch-finally处理异常描述有误的一项是()。
A、异常处理可以定义在方法体、自由块或构造方法中。
Java实战经典(第六章课后题答案)

public float getFoot(){ return this.foot ;
} public float getHeight(){
return this.height ; } } class Cycle extends Shape{ private float radius ; private static final float PI = 3.1415926f ; public Cycle(){} public Cycle(float radius){
4
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
System.out.pri nt("狗遇到同伴:"); yellowDog.setState(new MeetAnotherDog()); yellowDog.show();
public void showState(){
System.out.println(”晃动尾巴,表示欢迎");
}
}
class MeetA no therDog impleme nts DogState{
public void showState(){ System.out.pri ntl n("嬉戏");
truck.getTotalWeigths());
goods=new ComputerWeight[68];
for(int i=0;i<goods.length;i++){
if(i%2==0)
goods[i]=new Televisi on();
else
goods[i]=new WashMachi ne();
}
}
运行结果:
9.668
73.09
练习:
JAVAC
Estimator.java:27:错误:School不是抽象的,并且未覆盖CompurerAverage中的抽
象方法average(double[])
class School impleme nts CompurerAverage{
A
1个错误
实验2
}
}
class Dog{
DogState state;
public void show(){
state.showState();
}
public void setState(DogState s){ state=s;
}
}
public class CheckDogState{
public static void main( Stri ng[] args){
System.out.pri ntl n("气态");
}
class Water{
WaterState state;
public void show(){
state.showState();
}
public void setState(WaterState s){ state=s;
}
}
public class CheckWaterState{
三、实验仪器设备和材料
安装有J2SE开发工具的PC机。
四、实验内容和步骤
实验1
代码如下:
Estimator.java
in terface CompurerAverage{
public double average(double x[]);
}
class Gymn astics impleme nts CompurerAverage{ public double average(double x[]){ int coun t=x .len gth;
Dog yellowDog=new Dog();
System.out.pri nt("狗在主人面前:"); yellowDog.setState( new SoftlyStateO);
yellowDog.show();
System.out.pri nt("狗遇到敌人:"); yellowDog.setState( new MeetE nemyState()); yellowDog.show();
return 10.0;}
}
class Computer impleme nts ComputerWeight{
public double computerWeight(){
return 8.0;}
}
class WashMach ine impleme nts ComputerWeight{
public double computerWeight(){
this.goods=goods;
}
public double getTotalWeigths(){
totalWeihts=0;
for(int i=0;i<goods.length;i++){
totalWeihts+=goods[i].computerWeight();
}
return totalWeihts;
double a[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97};
double b[]={89,56,78,90,100,77,56,45,36,79,98};
CompurerAverage computer;
computer =new Gymn astics();
double result=computer.average(a);
return 10.5;}
}
不需修改Truck类
实验3
代码如下:
CheckDogState.java
in terface DogState{
public void showState();
}
class SoftlyState impleme nts DogState{
public void showState(){
System.out.println(”听主人的命令");
}
}
class MeetE nemyState impleme nts DogState{
public void showState(){
System.out.println(”狂叫,并冲向去狠咬敌人");
}
}
class MeetFrie ndState impleme nts DogState{
System.out.pri ntl n("固态");
}
}
class Ordin aryState impleme nts WaterState{ public void showState(){
System.out.pri ntl n("液态");
}
}
class HotState impleme nts WaterState{ public void showState(){
}
}
public class CheckCarWeight{
public static void main( Stri ng[] args){
ComputerWeight[] goods=new ComputerWeight[650]; for(int i=0;i<goods.length;i++){
return 35.5;}
}
class Truck{
ComputerWeight[] goods;
double totalWeihts=0;
Truck(ComputerWeight[] goods){
this.goods=goods;
}
public void setGoods(ComputerWeight[] goods){
double aver=0,temp=0;
for(int i=0;i<count;i++){ for(i nt j=i;j<co un t;j++){ if(x[j]<x[i]){ temp=x[j]; x[j]=x[i]; x[i]=temp;
}
}
}
for(i nt i=1;i<co un t-1;i++){
}
}
运行结果:
狗在主人面前:听主人的命令
狗遇到敌人:狂叫,并冲向去狠咬敌人
狗遇到朋友:晃动尾巴,表示欢迎 狗遇到同伴;嬉戏
请按任意键继续
练习:
CheckWaterState.java
in terface WaterState{
public void showState();
}
class ColdState impleme nts WaterState{ public void showState(){
System.out.pri ntf("% n");
System.out.pri ntf("体操选手最后得分:%5.3f\n",result);
computer =new School();
result=computer.average(b);
System.out.pri ntf("班级考试平均分数:%-5.2f\n",result);
}
truck.setGoods(goods);
System.out.pri ntf("货车装载的货物重量:%-8.5f kg\n",
truck.getTotalWeigths());
}
}
运行结果:
货车装载的货物重量:
练习:
class Refrigerrator impleme nts ComputerWeight{ public double computerWeight(){
public static void main( Stri ng[] args){
Water water =new Water();