JAVA实验6答案
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答案第六章

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实战经典(第六章课后题答案)

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
JAVA实验答案全

4.编写一个 Java Application 程序,输出区间[200,300]上的所有素数,要求写出程序的运 行结果。
public class primePrint{ public static void main(String args[])
{ int Leabharlann ,j; System.out.println("区间[200,300]上的所有素数:"); for(i=200;i<=300;i++){ for(j=2;j<=Math.sqrt(i);j++) if(i%j==0) break; if(j>Math.sqrt(i)) System.out.print(i+" "); } } }
} } 3.按下面的要求完成 Java Application 程序,写出程序的运行结果。 (1)定义一个 Java 类 Point,用来描述平面直角坐标系中点的坐标,该类应该能描述点的 横、纵坐标信息及一些相关操作,包括获取点的横、纵坐标,修改点的坐标,显示点的当前 位置等。 (2)定义一个测试类 JavaTest,创建 Point 类的对象并对其进行有关的操作。
} public class JavaTest {
public static void main(String args[]){ Point P; P=new Point(); P.set_Location(1.1f,1.2f); System.out.print(P.getLocation()); P.changeLocation(1.3f,1.4f); System.out.print(P.getLocation()); } }
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、异常处理可以定义在方法体、自由块或构造方法中。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
GDOU-B-11-112广东海洋大学学生实验报告书(学生用表)实验名称 实验六.Java 的接口与实现 课程名称JaVa程序设计与开发 课程号16232204 技术实验地点钟海楼实验日期2015年04019 10 月 26 日 、实验目的(1) 学习掌握Java 中类怎样实现接口、接口回调技术;(2) 学习掌握Java 程序中面向接口的编程思想。
二、实验任务完成实验六指导上实验1、实验2、实验3的实验任务。
三、实验仪器设备和材料安装有J2SE 开发工具的PC 机。
四、实验内容和步骤实验1代码如下:Estimator.javain terface Compu rerAverage{p ublic double average(double x[]);}class Gymn astics imp leme nts Compu rerAverage{ p ublic double average(double x[]){int coun t=x.le ngth;double aver=0,te mp=0;for(i nt i=0;i<co un t;i++){for(i nt j=i;j<co un t;j++){if(x[j]<x[i]){tem p=x[j];x[j]=x[i]; x[i]=temp;}}}for(i nt i=1;ivco un t-1;i++){学院(系)信息学院 专业 计算机科学与技术 班级计科 学生姓名 学号aver=aver+x[i];}if(cou nt>2)aver=aver/(co un t-2);elseaver=0;retur n aver;}}class School impi eme nts Compu rerAverage{p ublic double average(double[] x){int coun t=x.le ngth;double sum=0;for(i nt i=0;i<co un t;i++){sum+=x[i];}retu rn sum/co unt;}}p ublic class Estimator{p ublic static void main( Stri ng[] args){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};Compu rerAverage compu ter;computer=new Gymn astics();double result=co mpu ter.average(a);System.out .prin tf("% n");System.out .prin tf(" 体操选手最后得分:%5.3f\n",result);computer=new School();result=co mpu ter.average(b);System.out. prin tf(" 班级考试平均分数:%-5.2f\n",result);}}运行结果:-------- JAVAC -----------Estimator.java:27: 错误:School不是抽象的,并且未覆盖CompurerAverage中的抽象方法average(double[])class School impi eme nts Compu rerAverage{A1个错误实验2代码如下:CheckCarWeight.javain terface Compu terWeight{p ublic double compu terWeight();}class Televisi on imp leme nts Comp uterWeight{ p ublic double compu terWeight(){ return 10.0;} }class Compu ter imp leme nts Compu terWeight{ p ublic double compu terWeight(){ return 8.0;}}class WashMach ine imp leme nts Compu terWeight{ p ublic double compu terWeight(){ return 35.5;}}class Truck{Compu terWeight[] goods;double totalWeihts=0;Truck(Co mpu terWeight[] goods){ this.goods=goods;}p ublic void setGoods(Co mpu terWeight[] goods){ this.goods=goods;}public double getTotalWeigths(){ totalWeihts=0;for(i nt i=0;i<goods.le ngth;i++){ totalWeihts+=goods[i].co mpu terWeight();}return totalWeihts;}}public class CheckCarWeight{p ublic static void main( Stri ng[] args){Compu terWeight[] goods=new Compu terWeight[650];for(i nt i=0;i<goods.le ngth;i++){if(i%3==0)goods[i]=new Televisi on();if(i%3==1)goods[i]=new Compu ter();if(i%3==2)goods[i]=new WashMachi ne();}Truck truck=new Truck(goods);System.out .prin tf("\n 货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeigths());goods=new Compu terWeight[68];for(i nt i=0;i<goods.le ngth;i++){if(i%2==0)goods[i]=new Televisi on();elsegoods[i]=new WashMachi ne();货车装载的货物重量:%-8.5f kg\n", truck.getTotalWeigthsO); }}运行结果:练习:class Refrigerrator imp leme nts Compu terWeight{ p ublic double compu terWeight(){return 10.5;}}不需修改Truck 类 实验3代码如下:CheckDogState.javain terface DogState{p ublic void showState();}class SoftlyState imp leme nts DogState{}truck.setGoods(goods);System.out. printf (”p ublic void showState(){System.out.println(" 听主人的命令");}}class MeetE nemyState impi eme nts DogState{p ublic void showState(){System.out.printing 狂叫,并冲向去狠咬敌人");}}class MeetFrie ndState imp leme nts DogState{p ublic void showState(){System.out.println(" 晃动尾巴,表示欢迎");}}class MeetA no therDog imp leme nts DogState{p ublic void showState(){System.out. prin tl n(" 嬉戏");}}class Dog{DogState state;p ublic void show(){state.showState();}p ublic void setState(DogState s){state=s;}}p ublic class CheckDogState{p ublic static void main( Stri ng[] args){Dog yellowDog=new Dog();System.out. prin t("狗在主人面前:"); yellowDog.setState( new SoftlyState());yellowDog.showQ;System.out .prin t("狗遇到敌人:"); yellowDog.setState( new MeetE nemyState());yellowDog.show();System.out .prin t("狗遇到朋友:"); yellowDog.setState( new MeetFrie ndState());yellowDog.show();System.out .prin t("狗遇到同伴:"); yellowDog.setState( new MeetA no therDog());yellowDog.show();练习:CheckWaterState.javain terface WaterState{p ublic void showState();}class ColdState imp leme nts WaterState{p ublic void showState(){System.out. prin tl n(" 固态");}}class Ordin aryState imp leme nts WaterState{ p ublic void showState(){ System.out. prin tl n(" 液态");}}class HotState imp leme nts WaterState{p ublic void showState(){System.out. prin tl n(" 气态");}}class Water{Waterstate state;p ublic void show(){state.showStateO;}p ublic void setState(WaterState s){ state=s; }}public class CheckWaterState{p ublic static void main( Stri ng[] args){Water water= new Water();System.out.print("低于 0° C:"); water.setState( new ColdState()); water.show(); System.out.print("高于 0° C 且低于 100° C:"); water.setState( new Ordi naryState()); water.show();System.out.print(" 高于 100° C "); water.setState( new HotState()); water.show();注:请用A4纸书写,不够另附纸。