实习二.docx
- 文档编号:5880039
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:12
- 大小:151.42KB
实习二.docx
《实习二.docx》由会员分享,可在线阅读,更多相关《实习二.docx(12页珍藏版)》请在冰豆网上搜索。
实习二
基础实验三:
Java多线程
专业班级:
遥感科学与技术
学号:
********
******************
日期:
2015年5月3日
(1)用Math类中的随机函数产生10000个0~999范围的随机数并存于数组。
从键盘接收一个0~999的数x,然后用5-8个线程并发查找x的所有下标位置。
提示:
查找线程的构造函数形参如下所示:
数组名,查找范围(起始下标、结束下标)
每个线程体在查找范围内顺序查找,并将该范围内所有找到的x的下标记录到共享的一个内存缓冲区。
源程序代码:
importjava.io.*;
importjava.util.Scanner;
importjava.util.Random;
publicclasstest3
{
publicstaticvoidmain(Stringargs[])
{
intrandom[]=newint[10000];
Resultresult0=newResult();
Resultresult1=newResult();
Resultresult2=newResult();
Resultresult3=newResult();
for(inti=0;i<10000;i++)
{
random[i]=(int)(Math.random()*10000);
System.out.print(""+random[i]+"");
if((i+1)%10==0)System.out.println();
}
System.out.print("\n请输入要查找的数:
");
Scannerin=newScanner(System.in);
intn=in.nextInt();
Findth0=newFind(0,2499,n,random,result0);
Findth1=newFind(2500,4900,n,random,result1);
Findth2=newFind(5000,7400,n,random,result2);
Findth3=newFind(7500,9999,n,random,result3);
th0.start();
th1.start();
th2.start();
th3.start();
}
}
classFindextendsThread
{
publicintstart,end,n,random[];
publicResultresult;
publicFind(intstart,intend,intn,intrandom[],Resultresult)
{
this.start=start;
this.end=end;
this.n=n;
this.random=random;
this.result=result;
}
publicvoidrun()
{
synchronized(result)
{
booleanx=false;
for(inti=start;i<=end;i++)
{
if(random[i]==n)
{
result.write(Integer.toString(i+1));
x=true;
}
}
if(x==false)System.out.println("线程"+getName()+"查询失败,此区间内无此数字!
");
else
{
System.out.println("线程"+getName()+"查询到的位置:
"+result.read());
}
}
}
}
classResult
{
privateStringresult=newString();
voidwrite(Stringrandom){result+=random;result+="";}
publicStringread(){returnresult;}
}
运行结果界面截图:
测试结论:
程序用在主函数创建两个日期实例并用uk,ac,cn三种格式显示这两个日期,符合要求
(1)定义基类Vehicle,它具有私有属性wheels(车轮数)和weight(车载重)。
Vehicle类派生出Car类,表示小轿车,其中包含私有成员passengers(载人数)和brand(品牌名称)。
为Vehicle类和Car类定义构造函数和输出函数。
在主函数中创建一个小轿车对象实例,品牌为宝马,载人数为5人,车载重为1吨,车轮数为4个。
调用Car的输出函数,输出这些信息。
要求:
基类和派生类的输出函数同名。
源程序代码:
importjava.util.*;
classVehicle{
privateintwheels;
privatefloatweight;
privateintpeople;
protectedVehicle(intwheels,floatweight){
this.wheels=wheels;
this.weight=weight;
}
publicintgetWheels(){
returnwheels;
}
publicfloatgetWeight(){
returnweight;
}
publicintgetPeople(){
returnpeople;
}
publicvoidprint(){
System.out.println("汽车:
");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
}
}
classCarextendsVehicle{
privateintpassenger_load;
privateStringbrand;
publicCar(intwheels,floatweight,intpassenger_load,Stringbrand){
super(wheels,weight);
this.passenger_load=passenger_load;
this.brand=brand;
}
publicintgetPassenger_load(){
returnpassenger_load;
}
publicStringgetBrand(){
returnbrand;
}
publicvoidprint(){
System.out.println("品牌:
"+this.getBrand());
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
System.out.println("载人数为"+this.getPassenger_load()+"人");
}
}
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Strings="宝马";
Vehiclecar=newCar(4,1,5,s);
System.out.println("*****************************");
car.print();
System.out.println("*****************************");
}
}
运行结果界面截图:
测试结论:
程序定义基类Vehicle,它具有私有属性wheels(车轮数)和weight(车载重)。
Vehicle类派生出Car类,其中包含私有成员passengers(载人数)和brand(品牌名称),并为Vehicle类和Car类定义构造函数和输出函数,符合要求
(3)一个简单的航班查询系统。
定义时间类,成员包括:
实例属性:
时、分
实例方法:
适当的构造方法;输出方法。
定义航班类,成员包括:
实例属性:
航班号(如”CA1424”)、到港(如”北京”)、离港(如”成都”)、起飞时间、到达时间(内嵌时间对象);
实例方法:
适当形式的构造方法;
比对航班号是否相等的boolean方法;
输出航班信息的方法。
源程序代码:
importjava.io.*;
importjava.util.Scanner;
classCtime
{
protectedinth,m;
publicCtime(inth,intm){this.h=h;this.m=m;}
publicCtime(){}
publicCtime(Ctimet){h=t.h;m=t.m;}
publicvoidoutput(){System.out.println("航班时间:
"+h+":
"+m);}
}
classAirplane
{
protectedStringid,start,end;
protectedCtimestartT=newCtime();
protectedCtimeendT=newCtime();
publicAirplane(){}
publicAirplane(Ctimea,Ctimeb,Stringc,Stringd,Stringe)
{startT.h=a.h;
startT.m=a.m;
endT.h=b.h;
endT.m=b.m;
id=newString(c);
start=newString(d);
end=newString(e);
}
publicbooleansearch(Stringa)
{Stringb=newString(a);
intm=pareTo(id);
if(m==0)returntrue;
elsereturnfalse;
}
publicvoidoutput()
{System.out.println("航班号:
"+id);
System.out.println("离港:
"+start);
System.out.println("到港:
"+end);
System.out.println("起飞时间:
"+startT.h+":
"+startT.m);
System.out.println("到达时间:
"+endT.h+":
"+endT.m);
}
}
publicclasstest2_3
{
publicstaticvoidmain(String[]args)
{Airplane[]a=newAirplane[3];
a[0]=newAirplane(newCtime(3,10),newCtime(6,15),"CA1424","成都","北京");
a[1]=newAirplane(newCtime(6,13),newCtime(9,20),"SB250","成都","乌鲁木齐");
a[2]=newAirplane(newCtime(17,55),newCtime(19,34),"A380","成都","郑州");
inti;booleanj;
Scannerin=newScanner(System.in);
System.out.print("输入航班号:
");
Strings=in.next();
while(pareTo("000")!
=0)
{for(i=0;i<3;i++)
{j=a[i].search(s);
if(j==false&&i==2){System.out.println("查询失败!
");}
if(j==true){a[i].output();break;};
}
System.out.print("输入航班号:
");
s=in.next();
}
}
}
运行结果界面截图:
测试结论:
程序使用了比对航班号是否相等的boolean方法输出航班信息,符合要求。
(4)编写程序,从键盘输入两个双精度实数a和b,计算并输出a/b的值。
a.自定义异常类处理输入数据超出规定范围:
0~100;
b.处理b为0时的除数为0异常。
源程序代码:
importjava.io.*;
importjava.util.Scanner;
publicclasstest2_4
{publicstaticvoiddiv(doubleb)throwsArithmeticException
{
if(b==0)
{
ArithmeticExceptione=newArithmeticException("除数不能为0!
");
throwe;
}
}
publicstaticvoidcheck(doublea,doubleb)throwsException
{
if(a>100||a<0||b>100||b<0)
{
Exceptione=newException("输入数据超出规定范围!
");
throwe;
}
elseif(b!
=0){System.out.println("a/b="+a/b);}
}
publicstaticvoidmain(Stringargs[])
{
System.out.print("输入a,b的值:
");
doublea,b;
Scannerin=newScanner(System.in);
a=in.nextDouble();
b=in.nextDouble();
try{div(b);}
catch(ArithmeticExceptione){System.out.println(e.getMessage());}
try{check(a,b);}
catch(Exceptione){System.out.println(e.getMessage());}
}
}
运行结果界面截图:
测试结论:
程序自定义异常类处理输入数据超出规定范围:
0~100;处理b为0时的除数为0异常,符合要求。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实习