ImageVerifierCode 换一换
格式:DOCX , 页数:31 ,大小:89.16KB ,
资源ID:10898227      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10898227.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java上机实验答案与解析.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java上机实验答案与解析.docx

1、java上机实验答案与解析java上机实验答案与解析JAVA上机实验题答案与解析实验一 Java程序编程1.编写一个Java应用程序,输出内容为Hello!。注:文件位置位于e:2:Hello.java 编译:(1)e:(2)cd 2 (3)javac Hello.java(4)java Hello2.编写一个Java小应用程序,输出内容为我一边听音乐,一边学Java。第一步编写import java.awt.*;import java.applet.*;public class MyApplet extends Applet public void paint(Graphics g) g.d

2、rawString(我一边听音乐,我一边做java,25,25); 第二步 在DOS环境中编译(.javac MyApplet.java)第三步 使用记事本编写第四步 将记事本文件名命名为MyApplet.html public static void main(String args) Circle c1=new Circle(3); Circle c2=new Circle(5.5); System.out.println(c1.getArea (); System.out.println(c2.getArea(); 实验三 类的继承和多态性1.(1)编写一个接口ShapePara,要求:

3、 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长(2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。该类包含有成员变量:radius:public 修饰的double类型radius,表示圆的半径。x:private修饰的double型变量x,表示圆心的横坐标。y:protected修饰的double型变量y,表示圆心的纵坐标。包含的方法有:Circle(double radius) 有参构造方法。以形参表中的参数初始化半径,圆心为坐标原点。 double getRadius():获取半径为

4、方法的返回值。void setCenter(double x, double y):利用形参表中的参数设置类Circle的圆心坐标。void setRadius(double radius):利用形参表中的参数设置类Circle的radius域。在主方法中产生半径为5的圆。 interface ShapePara double getArea(double r); double getCircumference(double r);/注: Circle是在接口中建立的calss,即先建立接口,在建立接口的类 class Circle implements ShapePara private d

5、ouble x; protected double y; public double r; Circle(double r) this.r=r; void setRadius(double r) this.r=r; double getRadius() return r; double getArea() return (3.14*r*r); double getCircumference() return 3.14*2*r; void setCenter(double x,double y) this.x=x; this.y=y; double getCenterx() return x;

6、double getCentery() return y; public class A public static void main(String args) Circle ci=new Circle(5); ci.setRadius(5); ci.setCenter(0, 0); System.out.println(ci.getArea(); System.out.println(ci.getCircumference(); System.out.println(ci.getCenterx(); System.out.println(ci.getCentery(); 答案:78.531

7、.4000000000000020.00.02.定义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量double a,double b,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。class Shape double getArea(double r) return 0; public class Rect extends Shape double a,b,area; Rect(double width,double

8、heigh) a=width; b=height; void setWidth(double width) a=width; void setHeight(double height) b=height; double getWidth() return a; double getHeight() return b; double getArea() area=a*b; return area; public class A public static void main(String args) Rect rect=new Rect(); double w=12.76,h=25.28; re

9、ct.setWidth(w); rect.setHeight(h); System.out.println(矩形对象的宽:+rect.getWidth()+ 高:+rect.getHeight(); System.out.println(矩形的面积:+rect.getArea(); 答案:圆的的面积:78.5矩形对象的宽:12.76 高:25.28矩形的面积:322.572800000000033. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方

10、法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。public class Animal String name; int legs; static int

11、count; Animal()count+; void setLegs(int legs) this.legs=legs; int getLegs() return legs; void setKind(String name) this.name=name; String getKind() return name; int getCount() return count; public class Fish extends Animalstatic int countFish; Fish()countFish+;int getCount() return countFish; public

12、 class Tiger extends Animalstatic int countTiger; Tiger()countTiger+;int getCount() return countTiger; public class SouthEastTiger extends Tigerpublic class A public static void main(String args) Fish f=new Fish(); System.out.println(f.getCount(); Tiger t=new Tiger(); System.out.println(t.getCount()

13、; SouthEastTiger st=new SouthEastTiger(); System.out.println(st.getCount(); 实验四 异常处理1.建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。for(inti=0;i4;i+) intk; switch(i) case0:intzero=0;k=911/zero;break; case1:intb=null;k?=b0;break; case2:int c=new int2;k=c9;break; case3:charch=abc.ch

14、arAt(99);break; package Exception;public class TestException public static void main(String args) for(int i=0;iba) throw new InsufficientFundsException(取款余额不足); else if(dAmount0) throw new NagativeFundsException(取款为负数); else System.out.print(银行里还剩余:+(ba-dAmount); package Exception;import java.util.*

15、;public class A public static void main(String args ) Bank b=new Bank(100); Scanner sc=new Scanner(System.in); try System.out.println(请输入一个数); b.withDrawal(sc.nextInt(); catch(InsufficientFundsException e) System.out.println(e.getMassage1(); catch(NagativeFundsException e) System.out.println(e.getMa

16、ssage(); 运行结果:请输入一个数80银行里还剩余:20.0实验五 输入输出1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。import java.io.*;public class TextRw public static void main(String args) throws IOException File f=new File(f:TextRw.txt); FileWriter fw=new FileWriter(f); fw.write(学号 姓名); f

17、w.close(); FileReader fr=new FileReader(f); int i; while(i=fr.read()!=-1) System.out.print(char)i); fr.close(); 2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取IoDemo.java源程序文件,再通过键盘输入文件的名称为iodemo.txt,把IoDemo.java的源程序存入import java.io.*;public class TextRw public static void main(String args) throws IOExceptio

18、n String f=e:hello.java; FileReader r=new FileReader(f); int i; while(i=r.read()!=-1) System.out.print(char)i); r.close(); BufferedReader w=new BufferedReader(new InputStreamReader(System.in); System.out.print(请目录和输入文本名); String f1=w.readLine(); FileWriter w1=new FileWriter(f1); FileReader r1=new Fi

19、leReader(f); int j; while(j=r1.read()!=-1) w1.write(char)j); w1.close(); 运行结果:public class Hellopublic static void main(String args)System.out.println(hello);请目录和输入文本名e:Hello.text且会在e目录下产生Hello.text3.编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。import java.io.*;public class

20、 BinIoDemo public static void main(String args) throws IOException File f1=new File(e:1.doc); FileInputStream in=new FileInputStream(f1); File f2 = new File(e:“学号”+“姓名”.doc); FileOutputStream out=new FileOutputStream(f2); byte buf = new byte2048; int num = in.read(buf); while (num != (-1) out.write(

21、buf, 0, num); out.flush(); num = in.read(buf); in.close(); out.close();实验六 多线程编程1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。通过Threadpublic class Hw1Thread extends Thread String a; private int sleepTime; public Hw1Thread(String a) super(a);

22、this.a=a; sleepTime=(int)(Math.random()*6000); public void run() int count1=0,count2=0; if(Thread.currentThread().getName().equalsIgnoreCase(ShangHai) for(int i=0;icount2 & count1=10) /判断哪个城市的输出次数先达到10,达到则终止输出 System.out.println(I decide to go to shanghai!);break; else if(count2count1 & count2=10) S

23、ystem.out.println(I decide to go to BeiJing!);break; /判断哪个城市的输出次数先达到10,达到则终止输出 catch(InterruptedException exception);/使用sleep方法需要抛出中断异常 if(Thread.currentThread().getName().equalsIgnoreCase(BeiJIng) for(int i=0;icount2 & count1=10) System.out.println(I decide to go to shanghai!);break; else if(count2count1 & count2=10)

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1