收藏 分销(赏)

JAVA课程设计.doc

上传人:仙人****88 文档编号:8506811 上传时间:2025-02-16 格式:DOC 页数:19 大小:183KB
下载 相关 举报
JAVA课程设计.doc_第1页
第1页 / 共19页
JAVA课程设计.doc_第2页
第2页 / 共19页
JAVA课程设计.doc_第3页
第3页 / 共19页
JAVA课程设计.doc_第4页
第4页 / 共19页
JAVA课程设计.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

1、本科实验报告课程名称:JAVA实验报告姓 名:学 院:计算机科学与技术系:计算机科学与技术专 业:计算机科学与技术年 级:学 号:指导教师:2006年 11月23日实验项目列表序号实验项目名称成绩1JAVA语言基础2面向对象高级编程3JAVA核心API4类和对象5异常处理6多线程7APPLET8GUI与事件处理9输入与输出处理10综合实验-计算器111213一.实验要求通过上机实验加深对java语言的认识和运用,进一步熟悉语言,并能逐渐独立开发程序.二.实验过程 南一楼及寝室上机逐个调试三.实验结果与分析以下为第三章到第十章实验手册上题目的实验报告.3.1 a.将long l=12345678

2、变成long l=12345678l,程序输出没有改变,因为没有加L则为INT类型,运行时自动转化成LONG类型. b.将float类型省略后缀f将编译报错,因为无法自动转化成float类型.需要强制类型转化或者加上f.3.2 a.将int 类型改成float类型除法运算除不尽时将保留7位小数,int类型时直接舍去.其他运算保留一位小数.结果都为float类型.3.2 b.实验结果与预测一致,do-while是首先执行然后询问while条件是否成立.所以两个程序区别在于前者先看条件是否满足,因为步满足所以不会执行循环,而后者则先执行一次while然后看条件步满足退出循环.3.3 c.程序代码c

3、lass JLab0303_3public static void main(String args)int i=0;for(int j=1;j101;j+)i+=j;程序while和do-while类似,不再列出.3.3 d.输出三行星号代码 class xingpublic static void main(String args)for(int i=0;i3;i+)for(int j=0;j6;j+)System.out.print(*);System.out.println( );3.4 a.改成if(num=0&num1=0)3.4 b.两个程序输出不一样是因为每个case后面有个b

4、reak,遇到break程序自动退出switch,否则将继续执行下面的case,不管条件是否满足.3.4 c.水仙花数程序class JLab0304_3public static void main(String args)int i,a,b,c;for(i=100;i1000;i+)a=i%10;b=i/100;c=i/10-b*10;if(i=a*a*a+b*b*b+c*c*c)System.out.println(i);3.4 d.成绩等级class chengjipublic static void main(String args)int a=79/10;switch(a)case

5、 6:System.out.println(jige);break;case 7:System.out.println(zhongdeng);break;case 8:System.out.println(lianghao);break;case 9:System.out.println(youxiu);break;default:System.out.println(bujige);4.1 编写个circle类,并调用其方法.19public class ljian41public static void main(String args)Rectangle myRect = new Rec

6、tangle(20,30);double theArea;myRect.setwidth(3.6);myRect.setlength(10.8);theArea = myRect.area();System.out.println(my rectangles length: + myRect.length);System.out.println(my rectangles width: + myRect.width);System.out.println(my rectangle has area: + theArea);yuan yuan1=new yuan(2);yuan yuan2=ne

7、w yuan();System.out.println(my yuan has area: + yuan1.mianji();System.out.println(my yuan has area: + yuan2.zhouchang();yuan1.setr(1);yuan1.mianji();System.out.println(my yuan has area: + yuan1.mianji();class Rectangledouble width,length;public Rectangle(double width2, double length2)width=width2;le

8、ngth=length2; public void setwidth(double width2) width=width2; public void setlength(double length2) length=length2; public double area() double a;a=length*width;return a;class yuandouble r;public yuan(double r)this.r=r;public yuan()r=1;double mianji()return 3.14*r*r;double zhouchang()return 6.28*r

9、public String tostring()return yuan r is +r;public void setr(double r)this.r=r;public double fanhuir()return r;4.2 由实验验证可知:类变量和实例变量的区别是:类变量是一个公共变量,一旦修改则全局有效,对所有该类的引用有效,而实例变量只能通过实例变量来引用,每次修改只对本实例有效,不影响其他实例中的该变量.5.1 修改后的程序如下class pet protected String name;public pet(String n) name = n;public String g

10、etname() return name;public String move() return run;public String speak()return name+ +name;public String tostring() return my pet: + name;class dog extends pet protected int weight;public dog(String s)super(s); /子类访问父类构造器weight = 10;public void setweight(int w)weight = w;public String speak()retur

11、n “Woof Woof”;class ljian51 public static void main(String args) dog snoopy = new dog(snoopy);pet mypet = new pet(george);dog mydog = new dog(spot);dog ljian = new dog(woof);mydog.setweight(3);System.out.println(mypet.tostring()+n+Speak:+mypet.speak()+n+mypet.move()+ +mypet.getname()+n);System.out.p

12、rintln(mydog.tostring() + n + Speak: + mydog.speak() + n + mydog.move() + + mydog.getname() + n);System.out.println(snoopy.tostring() + n + Speak: + snoopy.speak() + n + snoopy.move() + + snoopy.getname() + n+snoopy.weight);System.out.println(mydog.weight);System.out.println(ljian.speak();程序简单就不作过多说

13、明!5.2 加入那段代码后,程序将无法通过编辑,原因是参数和名称都一样的方法已经存在了,方法重载不能方法名称和参数都一样.5.3 子类构造器中如果没有显示调用父类构造器则会调用父类无参构造器,无论怎么样,子类都要调用父类构造器,然后才是使用自身构造器.所以两个输出会不同.5.4 抽象类不需要一定含抽象方法,但是如果含有抽象方法,则一定要定义会抽象类,如果将pet定义为一个接口,那么它只能含有抽象方法,其方法的实现在子类中.代码和5.1基本类似,这里不写了.5.5 包的定义和使用在使用包的时候首先要导入包,否则将无法使用包,使用import,程序如下: import mypg.mypackage

14、public class ljian54 public static void main(String args)mypackage mg=new mypackage(); 如果将class文件放到同一个目录,则会报错.5.6 综合实验做个计算面积和周长的程序abstract class Shapepublic abstract double area();public abstract double perimeter(); class Square extends Shapedouble width,length;public Square()this(1,1);public Squar

15、e(double width,double length)this.width=width;this.length=length;public double area()return (length*width);public double perimeter()return (2*(length+width);class Circle1 extends Shapedouble radius;public Circle1()this(1);public Circle1(double radius)this.radius=radius;public double area()return (3.

16、14*radius*radius); public double perimeter() return (2*3.14*radius); class ljian56 public static void main(String args) Circle1 ExCircle; Square ExSquare; ExCircle=new Circle1(5); ExSquare=new Square(6,8); System.out.println(The area of the new Square is:+ExSquare.area()+n+The perimeter of the new S

17、quare is:+ExSquare.perimeter(); System.out.println(The area of the new Circle is:+ExCircle.area()+n+The perimeter of the new Circle is:+ExCircle.perimeter(); 上面程序首先定义了一个抽象类shape,它含有2个抽象方法,然后又定义了它的两个子类,分别实验圆和矩形的计算,实验了父类中的两个抽象方法.6.1 程序用2个嵌套的for循环实验排序,定义了一个tmp中间变量用来临时保存数组元素值.class ljian61public static

18、void main(String arg) double a=new double6; double tmp=0; a0=12.6; a1=32.0; a2=2.0; a3=11.2; a4=0.5; a5=3.99; for(int k=0;ka.length;k+) for(int j=k+1;ja.length;j+) if(akaj) tmp=ak; ak=aj; aj=tmp; for(int i=0;ia.length;i+) System.out.println(ai); 6.2 该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符

19、转换城大写,程序通过. class ljian62public static void main(String arg) String a=cat,fish,mouse; String x; for(int j=0;ja.length ;j+) for(int k=j+1;ka.length ;k+) if(aj.length()ak.length() x=aj;aj=ak;ak=x; for(int i=0;ia.length ;i+) System.out .print(ai.toUpperCase()+ ); 6.3 程序运行后表明string大小是不可以改变的,而stringbuffe

20、r则可以根据需要改变大小,往里面加字符串,验证想法.6.4 程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.import java.text.DateFormat;import java.util.*;class ljian64public static void main(String arg)Date now = new Date(); DateFormat d = DateFormat.getDateInstance(); String str = d.format(n

21、ow); String str2=str.split(-);Date date1=new Date(Integer.valueOf(str20).intValue(),(Integer.valueOf(str21).intValue(),(Integer.valueOf(str22).intValue();int week=date1.getDay();int day=date1.getDate();int year=date1.getYear();int month=date1.getMonth();char ch;switch(week)case 1:ch=一;break;case 2:c

22、h=二;break;case 3:ch=三;break;case 4:ch=四;break;case 5:ch=五;break;case 6:ch=六;break;case 7:ch=天;break;default:ch=一;break;System.out.println(今天是+year+年+month+月+day+日星期+ch);System.out.println(今天是+str);7.1 程序中使用switch分别会抛出4种异常,所以cahtch(exception e)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果

23、后面还有finally,则会执行里面的程序7.1_5 程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1 a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同. b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifya

24、ll方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序. class producer extends Threadprivate HoldInteger sharedObject;public producer(HoldInteger shared)super(producer);sharedObject=shared;public void run()for(int i=1;i=10;i+)tryThread.sleep(int)(Math.random()*3000);catch(InterruptedException e)System.out.println(

25、e.toString();sharedObject.setsharedint(i);System.out.println(getName()+finished producer value+nterminating+getName(); class consume extends Threadprivate HoldInteger sharedObject;public consume(HoldInteger shared)super(consume);sharedObject=shared;public void run()int value,sum=0;dotryThread.sleep(

26、int)(Math.random()*3000);catch(InterruptedException e)System.out.println(e.toString();value=sharedObject.getsharedint();sum+=value;while(value!=10); System.out.println(getName()+retrieved values totaling:+sum+nterminating+getName(); class HoldIntegerprivate int sharedint=1;private boolean yi=false;/

27、已经读取但没放入public synchronized void setsharedint(int value)while(yi=true)trywait();catch(Exception ex)System.out.println(Thread.currentThread().getName ()+setting sharedint to+value);sharedint=value;yi=true;notifyAll();public synchronized int getsharedint()while(yi=false)trywait();catch(Exception ex)yi

28、false;System.out.println(Thread.currentThread().getName()+retrieving sharedint value+sharedint);notifyAll();return sharedint;public class ljian82public static void main(String arg) HoldInteger sharedObject=new HoldInteger(); producer produce=new producer(sharedObject); consume consum=new consume(sh

29、aredObject); produce.start(); consum.start(); 9.1 applet的生存周期.init()方法是指初始化窗口的时候执行,所以每次启动都会加载该方法,窗口加载完成就不会再调用.与之相对的是destory()方法, 销毁窗口时执行,他们2个都只执行一次.而stop和start则每次启动applet都会执行一次,一旦停止然后重新启动就会重复调用该方法.paint方法则每次进行操作都会重新调用一次.9.2 程序会调用网页中设置的s参数,然后输出.10.1 程序界面如下:代码如下: import java.awt.*;import java.awt.even

30、t.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;class ljian101public static void main(String arg) new jisuan2();class jisuan2 extends JFrame implements ActionListener int i=0;JTextField shuchu22;jisuan2()super(小程序);JPanel zhu=new JPanel();zhu.setPreferredSize(new Dimension(19

31、0, 150);super.setLocation(300, 300);super.getContentPane().add(zhu);super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);super.pack();super.setVisible(true);/zhu.setLayout(new FlowLayout();JButton inc = new JButton( inc );inc.setForeground(new Color(255, 0, 0); inc.addActionListener(this);shuchu22=n

32、ew JTextField(8);zhu.add(shuchu22); zhu.add(inc); public void actionPerformed(ActionEvent e)if(e.getActionCommand()= inc )i+; shuchu22.setText(String.valueOf(i);10.2 代码和界面将在实验12里贴出,计算器界面和功能都已经写好.11.1 代码:import java.io.*;public class ljian112 public static void main(String args) int i = 0;String t=;f

33、loat f = 0,r=0;BufferedReader din=new BufferedReader(new InputStreamReader(System.in);System.out.println(input i:);try i=Integer.parseInt(din.readLine(); catch (NumberFormatException e1) / TODO 自动生成 catch 块e1.printStackTrace(); catch (IOException e1) / TODO 自动生成 catch 块e1.printStackTrace();System.ou

34、t.println(input 运算符号:);try t=din.readLine(); catch (NumberFormatException e) / TODO 自动生成 catch 块e.printStackTrace(); catch (IOException e) / TODO 自动生成 catch 块e.printStackTrace();System.out.println(input f:);try f=Float.parseFloat(din.readLine(); catch (NumberFormatException e) / TODO 自动生成 catch 块e.p

35、rintStackTrace(); catch (IOException e) / TODO 自动生成 catch 块e.printStackTrace();if(t.charAt(0)=+)System.out.println(t.toString();r=i+f;else if(t.charAt(0)=-)r=i-f;else if(t.charAt(0)=/)if(f!=0)r=i/f;elseSystem.out.println(出错);elser=i*f;System.out.println(i+t+f:+r);程序的运算符通过t来获得,字符串类型用chatat方法来获取它第一个符号

36、这样进行计算后就得到最终的结果.我原先用t.substring0方法来获取符号,一直不行,这里应该比较字符才对.11.2 a.往文本文件里写数据import java.io.*;public class JLab1102A public static void main(String args) File f=new File(q1.txt);try DataOutputStream dos=new DataOutputStream(new FileOutputStream(f);for(int ii=0;ii20;ii+)dos.writeDouble(Math.random()*1000

37、);dos.close(); catch (IOException e) e.printStackTrace();b.读取最大的双精度浮点数首先读取第一个数赋给max,然后循环比较出较大值参与下次比较.import java.io.*;public class JLab1102B public static void main(String args) File f=new File(q1.txt);double max=0;try DataInputStream dis=new DataInputStream(new FileInputStream(f); max=dis.readDoubl

38、e();for(int i=1;imax)max=tmp;dis.close(); catch (IOException e) e.printStackTrace();System.out.println(max);c.如果修改了文件,则会出现异常.10.3 testwrite和testread分别是往q2.dat文件里随即写入和读取数据,最后输出最大值.注意都要使用异常处理,否则无法通过编译.代码如下:import java.io.*;public class JLab1102C public static void main(String args) testWrite();testRead();public static void testWrite() try RandomAccessFile raf=new RandomAccessFile(q2.dat,rw);for(int ii=0;ii20;ii+)raf.writeDouble(Math.random()*1000);raf.close(); catch (FileNotFoundException e)

展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服