JAVA实验6答案

合集下载

Java实验指导书答案

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异常(答案)

最新实验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程序设计实践教程实验指导课后答案

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程序设计第六章课后答案

解析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答案第六章

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 综合实验及练习(参考答案)

Java实验综合实验及练习第一部分:编程题1、编写程序实现输入整数n,输出如下所示由数字组成的菱形。

(图中n=5)11 2 11 2 3 2 11 2 3 4 3 2 11 2 3 4 5 4 3 2 11 2 3 4 3 2 11 2 3 2 11 2 112.给出年、月、日,计算该日是该年的第几天?3、利用求素数的方法,就6~2000000之间的所有偶数验证歌德巴赫猜想:任何一个大于6的偶数可以分解为两个素数之和。

4、现有15位选手参加比赛,有6个评委每个评委都要给每位选手打分,分数为60~100分,现需要定义二维数组存储选手成绩,每行存储一位选手打分情况,要求输出选手得分以及选手最后得分,以及选手名次。

评分规则:分数为60~100分。

选手最后得分为:去掉一个最高分和一个最低分后其余4个分数的平均值。

5、设计一个描述二维平面上点的类Position,该类需要描述点的横坐标和纵坐标,并提供属性的set/get访问器方法和计算两点间距离的方法。

写一个程序测试这个类6、设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x 和y值的public方法。

设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。

7、编写一个完整的Java Application 程序。

包含接口Shape,MyRectangle类,MyTriangle 类及Test类,具体要求如下:⑴、接口Shape:double area():求一个形状的面积double perimeter ():求一个形状的周长⑵、类 MyRectangle :实现Shape 接口,并有以下属性和方法:① 属性width : double 类型,表示矩形的长height : double 类型,表示矩形的高② 方法MyRectangle(double w, double h):构造函数ToString()方法 :输出矩形的描述信息,如“width=1.0,height=2.0, perimeter=6.0,area=2.0”⑶、类MyTriangle :实现Shape 接口,并有以下属性和方法:① 属性x,y,z: double 型,表示三角形的三条边s: 周长的1/2(注:求三角形面积公式为))()((z s y s x s s ---,s=(x+y+z)/2 ,开方可用Math.sqrt(double)方法)② 方法MyTriangle(double x, double y, double z):构造函数,给三条边和s 赋初值。

Java实战经典(第六章课后题答案)

super(name,age,sex); this.yearsalary=yearsalary; this.post=post; } public String Info() { return ()+",工资:"+this.yearsalary+",职位:"+this.post; } } class Staff extends Employee { private float monthsalary; private String post; public Staff(String name,int age,String sex,float monthsalary,String post) { super(name,age,sex); this.monthsalary=monthsalary; this.post=post; } public String Info() { return ()+",工资:"+this.monthsalary+",职位: "+this.post; } } public class Six04
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

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 pay = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入费用:"));
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;

习题答案6-Java面向对象程序设计(第3版)-赵生慧-清华大学出版社

第六章【练习6.1 】[思考题] 请思考Java程序中编译时错误、逻辑错误和异常之间区别。

编译时的错误常常是语法上的错误。

通过编译器错误提示,能较容易修改。

程序逻辑上的错误是能被编译器编译通过,并能顺利运行,但运行出来的结果往往不是我们预期的结果。

异常是在程序运行过程中出现了错误导致程序终止,常常是运行时的错误。

练习6.21.C2.A练习6.31.C2.D3.A4.B5.B6.C练习6.41.B2.D练习6.51.C2.捕获异常;继续声明异常练习6.61.A2.C3.Exception;throws4.It’s caught!It’s finally caught!习题61.test2.不会3.[编程题]定义Triangle 类,其中包含一个方法void sanjiao(int a,int b,int c),用来判断三个参数是否能构成一个三角形。

如果不能则抛出异常IllegalArgumentException,显示异常信息a,b,c+”不能构成三角形”,如果可以构成则显示三角形三个边长,在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。

import java.util.Arrays;import java.util.Scanner;public class Triangle {public static void sanjiao(int a,int b,int c)throws Exception {if(a+b>c && c-a<b){System.out.println("能构成三角形");System.out.println("a="+a+","+"b="+b+","+"c="+c);}else {throw new IllegalArgumentException(a+","+b+","+c+" 不能构成三角形");}}public static void main(String[] args){int a[]=new int[3];System.out.println("输入三个整数");for(int i=0;i<3;i++){Scanner sc=new Scanner(System.in);a[i]=sc.nextInt();}Arrays.sort(a);//数组默认的从小到大排序,以保证三角形判定准则有效try{sanjiao(a[0],a[1],a[2]);}catch(IllegalArgumentException e){e.printStackTrace();}catch(Exception e){e.printStackTrace();}}}4.编写三个自定义异常类,分别为空异常类、年龄小异常类和年龄大异常类。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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.javainterface CompurerAverage{public double average(double x[]);}class Gymnastics implementsCompurerAverage{ public doubleaverage(double x[]){int count=x.length;double aver=0,temp=0;for(int i=0;i<count;i++){for(intj=i;j<count;j++){ if(x[j]<x[i]){temp=x[j];x[j]=x[i];x[i]=temp;}}.. }for(int i=1;i<count-1;i++){aver=aver+x[i];}if(count>2)aver=aver/(count-2);elseaver=0;return aver;}}class School implementsCompurerAverage{ public doubleaverage(double[] x){int count=x.length;double sum=0;for(inti=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 implementsComputerWeight{ public doublecomputerWeight(){return 10.0;}}class Computer implementsComputerWeight{ public doublecomputerWeight(){return 8.0;}}class WashMachine implementsComputerWeight{ public doublecomputerWeight(){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 doublegetTotalWeigths(){ totalWeihts=0;for(inti=0;i<goods.length;i++){ totalWeihts+=go ods[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 implementsComputerWeight{ public doublecomputerWeight(){return 10.5;}}不需修改Truck 类实验 3代码如下:CheckDogState.javainterface DogState{public void showState();}class SoftlyState implementsDogState{ public void showState(){System.out.println(" 听主人的命令");}}class MeetEnemyState implementsDogState{ public void showState(){System.out.println(" 狂叫,并冲向去狠咬敌人");}}class MeetFriendState implementsDogState{ public void showState(){System.out.println(" 晃动尾巴,表示欢迎");}}class MeetAnotherDog implementsDogState{ public void showState(){System.out.println(" 嬉戏");}}class Dog{DogState state;public void show(){state.showState();}public void setState(DogStates){ state=s;}}public class CheckDogState{public static void main(String[]args){ Dog yellowDog=newDog();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 implementsWaterState{ public voidshowState(){System.out.println(" 固态");}}class OrdinaryState implementsWaterState{ public void showState(){System.out.println(" 液态");}}class HotState implementsWaterState{ public voidshowState(){System.out.println(" 气态");} }..;.. class Water{WaterState state;public void show(){state.showState();}public void setState(WaterStates){ 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 纸书写,不够另附纸。

相关文档
最新文档