JAVA课程设计.docx
《JAVA课程设计.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计.docx(38页珍藏版)》请在冰豆网上搜索。
JAVA课程设计
本科实验报告
课程名称:
JAVA实验报告
姓名:
学院:
计算机科学与技术
系:
专业:
年级:
学号:
指导教师:
2006年11月23日
实验项目列表
序号
实验项目名称
成绩
1
JAVA语言基础
2
面向对象高级编程
3
JAVA核心API
4
类和对象
5
异常处理
6
多线程
7
APPLET
8
GUI与事件处理
9
输入与输出处理
10
综合实验-计算器
11
12
13
一.实验要求
通过上机实验加深对java语言的认识和运用,进一步熟悉语言,并能逐渐独立开发程序.
二.实验过程
南一楼及寝室上机逐个调试
三.实验结果与分析
以下为第三章到第十章实验手册上题目的实验报告.
3.1a.将longl=12345678变成longl=12345678l,程序输出没有改变,因为没有加L则为INT类型,运行时自动转化成LONG类型.
b.将float类型省略后缀f将编译报错,因为无法自动转化成float类型.需要强制类型转化或者加上f.
3.2a.将int类型改成float类型除法运算除不尽时将保留7位小数,int类型时直接舍去.其他运算保留一位小数.结果都为float类型.
3.2b.实验结果与预测一致,do-while是首先执行然后询问while条件是否成立.所以两个程序区别在于前者先看条件是否满足,因为步满足所以不会执行循环,而后者则先执行一次while然后看条件步满足退出循环.
3.3c.程序代码
classJLab0303_3{
publicstaticvoidmain(Stringargs[]){
inti=0;
for(intj=1;j<101;j++){
i+=j;
}
程序while和do-while类似,不再列出.
3.3d.输出三行星号代码classxing{
for(inti=0;i<3;i++){
for(intj=0;j<6;j++){
System.out.print("*");
System.out.println("");
3.4a.改成if(num==0&num1==0)
3.4b.两个程序输出不一样是因为每个case后面有个break,遇到break程序自动退出switch,否则将继续执行下面的case,不管条件是否满足.
3.4c.水仙花数程序
classJLab0304_3{
inti,a,b,c;
for(i=100;i<1000;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.4d.成绩等级
classchengji{
inta=79/10;
switch(a){
case6:
System.out.println("jige");break;
case7:
System.out.println("zhongdeng");break;
case8:
System.out.println("lianghao");break;
case9:
System.out.println("youxiu");break;
default:
System.out.println("bujige");
4.1编写个circle类,并调用其方法.
publicclassljian41{
publicstaticvoidmain(String[]args)
{
RectanglemyRect=newRectangle(20,30);
doubletheArea;
myRect.setwidth(3.6);
myRect.setlength(10.8);
theArea=myRect.area();
System.out.println("myrectangle'slength:
"+myRect.length);
System.out.println("myrectangle'swidth:
"+myRect.width);
System.out.println("myrectanglehasarea:
"+theArea);
yuanyuan1=newyuan
(2);
yuanyuan2=newyuan();
System.out.println("myyuanhasarea:
"+yuan1.mianji());
"+yuan2.zhouchang());
yuan1.setr
(1);
yuan1.mianji();
classRectangle{
doublewidth,length;
publicRectangle(doublewidth2,doublelength2)
width=width2;
length=length2;
publicvoidsetwidth(doublewidth2){
publicvoidsetlength(doublelength2){
publicdoublearea()
doublea;
a=length*width;
returna;
classyuan{
doubler;
publicyuan(doubler){
this.r=r;
publicyuan(){
r=1;
doublemianji()
return3.14*r*r;
doublezhouchang(){
return6.28*r;
publicStringtostring(){
return"yuanris"+r;
publicvoidsetr(doubler){
publicdoublefanhuir(){
returnr;
4.2由实验验证可知:
类变量和实例变量的区别是:
类变量是一个公共变量,一旦修改则全局有效,对所有该类的引用有效,而实例变量只能通过实例变量来引用,每次修改只对本实例有效,不影响其他实例中的该变量.
5.1修改后的程序如下
classpet{
protectedStringname;
publicpet(Stringn){
name=n;
publicStringgetname(){
returnname;
publicStringmove(){
return"run";
publicStringspeak()
returnname+""+name;
return"mypet:
"+name;
classdogextendspet{
protectedintweight;
publicdog(Strings)
super(s);//子类访问父类构造器
weight=10;
publicvoidsetweight(intw){
weight=w;
publicStringspeak(){
return“WoofWoof”;
classljian51{
publicstaticvoidmain(String[]args){
dogsnoopy=newdog("snoopy");
petmypet=newpet("george");
dogmydog=newdog("spot");
dogljian=newdog("woof");
mydog.setweight(3);
System.out.println(mypet.tostring()+"\n"+"Speak:
"+mypet.speak()+"\n"+mypet.move()+""+mypet.getname()+"\n");
System.out.println(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());
程序简单就不作过多说明!
!
5.2加入那段代码后,程序将无法通过编辑,原因是参数和名称都一样的方法已经存在了,方法重载不能方法名称和参数都一样.
5.3子类构造器中如果没有显示调用父类构造器则会调用父类无参构造器,无论怎么样,子类都要调用父类构造器,然后才是使用自身构造器.所以两个输出会不同.
5.4抽象类不需要一定含抽象方法,但是如果含有抽象方法,则一定要定义会抽象类,如果将pet定义为一个接口,那么它只能含有抽象方法,其方法的实现在子类中.代码和5.1基本类似,这里不写了.
5.5包的定义和使用
在使用包的时候首先要导入包,否则将无法使用包,使用import,程序如下:
importmypg.mypackage;
publicclassljian54{
mypackagemg=newmypackage();
}如果将class文件放到同一个目录,则会报错.
5.6综合实验做个计算面积和周长的程序
abstractclassShape{
publicabstractdoublearea();
publicabstractdoubleperimeter();
classSquareextendsShape{
publicSquare(){
this(1,1);
publicSquare(doublewidth,doublelength){
this.width=width;
this.length=length;
publicdoublearea(){
return(length*width);
publicdoubleperimeter(){
return(2*(length+width));
classCircle1extendsShape{
doubleradius;
publicCircle1(){
this
publicCircle1(doubleradius){
this.radius=radius;
return
(3.14*radius*radius);
return(2*3.14*radius);
classljian56{
Circle1ExCircle;
SquareExSquare;
ExCircle=newCircle1(5);
ExSquare=newSquare(6,8);
System.out.println("TheareaofthenewSquareis:
"+ExSquare.area()+"\n"+"TheperimeterofthenewSquareis:
"+ExSquare.perimeter());
System.out.println("TheareaofthenewCircleis:
"+ExCircle.area()+"\n"+"TheperimeterofthenewCircleis:
"+ExCircle.perimeter());
上面程序首先定义了一个抽象类shape,它含有2个抽象方法,然后又定义了它的两个子类,分别实验圆和矩形的计算,实验了父类中的两个抽象方法.
6.1程序用2个嵌套的for循环实验排序,定义了一个tmp中间变量用来临时保存数组元素值.
classljian61{
publicstaticvoidmain(String[]arg){
double[]a=newdouble[6];
doubletmp=0;
a[0]=12.6;
a[1]=32.0;
a[2]=2.0;
a[3]=11.2;
a[4]=0.5;
a[5]=3.99;
for(intk=0;k{for(intj=k+1;j{if(a[k]tmp=a[k];a[k]=a[j];a[j]=tmp;}}}for(inti=0;iSystem.out.println(a[i]);}}}6.2该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符转换城大写,程序通过.classljian62{publicstaticvoidmain(String[]arg){String[]a={"cat","fish","mouse"};Stringx;for(intj=0;j{for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
for(intj=k+1;j{if(a[k]tmp=a[k];a[k]=a[j];a[j]=tmp;}}}for(inti=0;iSystem.out.println(a[i]);}}}6.2该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符转换城大写,程序通过.classljian62{publicstaticvoidmain(String[]arg){String[]a={"cat","fish","mouse"};Stringx;for(intj=0;j{for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
if(a[k]tmp=a[k];a[k]=a[j];a[j]=tmp;}}}for(inti=0;iSystem.out.println(a[i]);}}}6.2该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符转换城大写,程序通过.classljian62{publicstaticvoidmain(String[]arg){String[]a={"cat","fish","mouse"};Stringx;for(intj=0;j{for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
tmp=a[k];
a[k]=a[j];
a[j]=tmp;
for(inti=0;iSystem.out.println(a[i]);}}}6.2该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符转换城大写,程序通过.classljian62{publicstaticvoidmain(String[]arg){String[]a={"cat","fish","mouse"};Stringx;for(intj=0;j{for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
System.out.println(a[i]);
6.2该程序同样使用2个for循环先对对象数组按要求进行排序,然后再输出,toUpperCase表示将字符转换城大写,程序通过.
classljian62{
String[]a={"cat","fish","mouse"};
Stringx;
for(intj=0;j{for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
for(intk=j+1;k{if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
if(a[j].length(){x=a[j];a[j]=a[k];a[k]=x;}}}for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
x=a[j];
a[j]=a[k];
a[k]=x;
for(inti=0;i{System.out.print(a[i].toUpperCase()+"");}}}6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.importjava.text.DateFormat;importjava.util.*;classljian64{publicstaticvoidmain(String[]arg){Datenow=newDate();DateFormatd=DateFormat.getDateInstance();Stringstr=d.format(now);String[]str2=str.split("-");Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());intweek=date1.getDay();intday=date1.getDate();intyear=date1.getYear();intmonth=date1.getMonth();charch;switch(week){case1:ch='一';break;case2:ch='二';break;case3:ch='三';break;case4:ch='四';break;case5:ch='五';break;case6:ch='六';break;case7:ch='天';break;default:ch='一';break;}System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);System.out.println("今天是"+str);}}7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.classproducerextendsThread{privateHoldIntegersharedObject;publicproducer(HoldIntegershared){super("producer");sharedObject=shared;}publicvoidrun(){for(inti=1;i<=10;i++){try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}sharedObject.setsharedint(i);}System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());}}classconsumeextendsThread{privateHoldIntegersharedObject;publicconsume(HoldIntegershared){super("consume");sharedObject=shared;}publicvoidrun(){intvalue,sum=0;do{try{Thread.sleep((int)(Math.random()*3000));}catch(InterruptedExceptione){System.out.println(e.toString());}value=sharedObject.getsharedint();sum+=value;}while(value!=10);System.out.println(getName()+"retrievedvaluestotaling:"+sum+"\nterminating"+getName());}}classHoldInteger{privateintsharedint=1;privatebooleanyi=false;//已经读取但没放入publicsynchronizedvoidsetsharedint(intvalue){while(yi==true){try{wait();}catch(Exceptionex){}}System.out.println(
System.out.print(a[i].toUpperCase()+"");
6.3程序运行后表明string大小是不可以改变的,而stringbuffer则可以根据需要改变大小,往里面加字符串,验证想法.
6.4程序通过getDateInstance方法获取当前时间并格式化,然后通过split分割字符串,构造Date类型变量分别获取年月日,用switch来获取是周几,输出中文.
importjava.text.DateFormat;
importjava.util.*;
classljian64{
Datenow=newDate();
DateFormatd=DateFormat.getDateInstance();
Stringstr=d.format(now);
String[]str2=str.split("-");
Datedate1=newDate((Integer.valueOf(str2[0]).intValue()),(Integer.valueOf(str2[1]).intValue()),(Integer.valueOf(str2[2])).intValue());
intweek=date1.getDay();
intday=date1.getDate();
intyear=date1.getYear();
intmonth=date1.getMonth();
charch;
switch(week){
case1:
ch='一';break;
case2:
ch='二';break;
case3:
ch='三';break;
case4:
ch='四';break;
case5:
ch='五';break;
ch='六';break;
ch='天';break;
System.out.println("今天是"+year+"年"+month+"月"+day+"日星期"+ch);
System.out.println("今天是"+str);
7.1程序中使用switch分别会抛出4种异常,所以cahtch(exceptione)会捕获所有异样,但是一旦改为ArithmeticException则因为不能捕获其他三种异常而报错,捕获异常后如果后面还有finally,则会执行里面的程序
7.1_5程序先调用throwexception()抛出一个异常,然后通过catch捕获,他们同是exception异常,所以程序执行后显示抛出异常同时又被捕获.
8.1a.两个线程运行结构是不一样的,因为线程是一起并发运行的,线程运行时间也是随即给的,每次时间到后才开始执行,每次启动线程睡眠时间也是不同,所以结果也不相同.
b.要使两个线程能够有顺序的执行,必须控制他们的访问条件,生产者和消费者的问题则必须生产者放数后消费者才能访问,否则出错,在线程中我们给放数和取数的方法加上synchronized属性,然后可以对线程使用wait和notifyall方法,同时设置一个共享变量用来判断是否已经放数和是否已经读数,见下面原程序.
classproducerextendsThread
privateHoldIntegersharedObject;
publicproducer(HoldIntegershared)
super("producer");
sharedObject=shared;
publicvoidrun()
for(inti=1;i<=10;i++)
try{
Thread.sleep((int)(Math.random()*3000));
}catch(InterruptedExceptione){
System.out.println(e.toString());
sharedObject.setsharedint(i);
System.out.println(getName()+"finishedproducervalue"+"\nterminating"+getName());
classconsumeextendsThread
publicconsume(HoldIntegershared)
super("consume");
intvalue,sum=0;
do{
value=sharedObject.getsharedint();
sum+=value;
}while(value!
=10);
System.out.println(getName()+"retrievedvaluestotaling:
"+sum+"\nterminating"+getName());
classHoldInteger
privateintsharedint=1;
privatebooleanyi=false;//已经读取但没放入
publicsynchronizedvoidsetsharedint(intvalue)
while(yi==true)
wait();
}catch(Exceptionex)
System.out.println(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1