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()); } }
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、异常处理可以定义在方法体、自由块或构造方法中。
JAVA语言程序设计第六版编程答案第二章

Double tax = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入提成:"));
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num<0 || num>127){
System.out.println("输入有误!程序终止运行");
double futureInvestmentValue = investmentAmount*Math.pow((monthlyInverestRate+1.0),numberOfYear*12);
String output = "将来的资金额为:" + futureInvestmentValue;
", 高度为:" + height +
"的圆柱体积是:" + volume);
}
}
2_3
import javax.swing.JOptionPane;
}
}
2_2
import javax.swing.JOptionPane;
public class Exercise2_2 {
public static void main(String[] args) {
double radius, height;
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
广东海洋大学学生实验报告书(学生用表)实验名称实验六. Java的接口与实现课程名称Java程序设计与开发技术课程号16232204学院(系) 信息学院专业计算机科学与技术班级计科学生姓名学号实验地点钟海楼04019实验日期2015年10月26日一、实验目的(1)学习掌握Java中类怎样实现接口、接口回调技术;(2)学习掌握Java 程序中面向接口的编程思想。
二、实验任务完成实验六指导上实验1、实验2、实验3的实验任务。
三、实验仪器设备和材料安装有J2SE开发工具的PC机。
四、实验内容和步骤实验1代码如下:Estimator.javainterface CompurerAverage{public double average(double x[]);}class Gymnastics implements CompurerAverage{public double average(double x[]){int count=x.length;double aver=0,temp=0;for(int i=0;i<count;i++){for(int j=i;j<count;j++){if(x[j]<x[i]){temp=x[j];x[j]=x[i];x[i]=temp;}}}GDOU-B-11-112for(int i=1;i<count-1;i++){aver=aver+x[i];}if(count>2)aver=aver/(count-2);elseaver=0;return aver;}}class School implements CompurerAverage{public double average(double[] x){int count=x.length;double sum=0;for(int i=0;i<count;i++){sum+=x[i];}return sum/count;}}public class Estimator{public static void main(String[] 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};CompurerAverage computer;computer=new Gymnastics();double result=computer.average(a);System.out.printf("%n");System.out.printf("体操选手最后得分:%5.3f\n",result);computer=new School();result=computer.average(b);System.out.printf("班级考试平均分数:%-5.2f\n",result);}}运行结果:练习:---------- JAVAC ----------Estimator.java:27: 错误: School不是抽象的, 并且未覆盖CompurerAverage中的抽象方法average(double[])class School implements CompurerAverage{^1 个错误实验2代码如下:CheckCarWeight.javainterface ComputerWeight{public double computerWeight();}class Television implements ComputerWeight{public double computerWeight(){return 10.0;}}class Computer implements ComputerWeight{public double computerWeight(){return 8.0;}}class WashMachine implements ComputerWeight{public double computerWeight(){return 35.5;}}class Truck{ComputerWeight[] goods;double totalWeihts=0;Truck(ComputerWeight[] goods){this.goods=goods;}public void setGoods(ComputerWeight[] goods){this.goods=goods;}public double getTotalWeigths(){totalWeihts=0;for(int i=0;i<goods.length;i++){totalWeihts+=goods[i].computerWeight();}return totalWeihts;}}public class CheckCarWeight{public static void main(String[] args){ComputerWeight[] goods=new ComputerWeight[650];for(int i=0;i<goods.length;i++){if(i%3==0)goods[i]=new Television();if(i%3==1)goods[i]=new Computer();if(i%3==2)goods[i]=new WashMachine();}Truck truck=new Truck(goods);System.out.printf("\n货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeigths());goods=new ComputerWeight[68];for(int i=0;i<goods.length;i++){if(i%2==0)goods[i]=new Television();elsegoods[i]=new WashMachine();}truck.setGoods(goods);System.out.printf("货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeigths());}}运行结果:练习:class Refrigerrator implements ComputerWeight{public double computerWeight(){return 10.5;}}不需修改Truck类实验3代码如下:CheckDogState.javainterface DogState{public void showState();}class SoftlyState implements DogState{public void showState(){System.out.println("听主人的命令");}}class MeetEnemyState implements DogState{public void showState(){System.out.println("狂叫,并冲向去狠咬敌人");}}class MeetFriendState implements DogState{public void showState(){System.out.println("晃动尾巴,表示欢迎");}}class MeetAnotherDog implements DogState{public void showState(){System.out.println("嬉戏");}}class Dog{DogState state;public void show(){state.showState();}public void setState(DogState s){state=s;}}public class CheckDogState{public static void main(String[] args){Dog yellowDog=new Dog();System.out.print("狗在主人面前:");yellowDog.setState(new SoftlyState());yellowDog.show();System.out.print("狗遇到敌人:");yellowDog.setState(new MeetEnemyState());yellowDog.show();System.out.print("狗遇到朋友:");yellowDog.setState(new MeetFriendState());yellowDog.show();System.out.print("狗遇到同伴:");yellowDog.setState(new MeetAnotherDog());yellowDog.show();}}运行结果:练习:CheckWaterState.javainterface WaterState{public void showState();}class ColdState implements WaterState{public void showState(){System.out.println("固态");}}class OrdinaryState implements WaterState{ public void showState(){System.out.println("液态");}}class HotState implements WaterState{public void showState(){System.out.println("气态");}}class Water{WaterState state;public void show(){state.showState();}public void setState(WaterState s){state=s;}}public class CheckWaterState{public static void main(String[] 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 OrdinaryState());water.show();System.out.print("高于100°C:");water.setState(new HotState());water.show();}}成绩指导教师孙兵日期2015年10月30日注:请用A4纸书写,不够另附纸。